java - 如何在 xml 中绘制图像,但在 java 类中更改尺寸?

标签 java android xml

我希望能够更改bluerect的宽度和高度,但当我制作动画时它不会改变。尺寸保持不变。

<ImageView
    android:id="@+id/bluerect"
    android:layout_height="100dp"
    android:layout_width="50dp"
    android:src="@drawable/bluerect"

    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"/>

另外,我希望能够绘制一个矩形,但在 java 类中声明尺寸。我怎样才能做到这一点?

最佳答案

据我了解您的问题,您希望在运行时动态缩小/增大矩形。这是我的可能解决方案草稿:

在我的 Layout-Xml 文件中,我添加了两个增长和收缩按钮,我可以在其中触发 10% 增长/收缩动画:

<Button
    android:text="Grow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/grow"
    android:textSize="30sp" />

<Button
    android:text="Skrink"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/shrink"
    android:layout_below="@+id/grow"
    android:layout_alignLeft="@+id/grow"
    android:layout_alignStart="@+id/grow"
    android:layout_alignRight="@+id/grow"
    android:layout_alignEnd="@+id/grow"
    android:textSize="30sp" />

<ImageView
    android:id="@+id/bluerect"
    android:layout_height="100dp"
    android:layout_width="50dp"
    android:src="@drawable/bluerect"
    android:layout_below="@+id/shrink1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp" />

在具有相应 Activity 的类中,我声明以下字段:

private Button grow;
private Button shrink;
private ImageView img_animation;
private float startdim[] = {1f, 1f};
private float enddim[] = {1f, 1f};

这是三个字段,用于存储两个按钮和 ImageView 的句柄,以及两个数组,用于存储有关动画开始和结束时的尺寸的数据。

在 OnCreate 方法中,我添加以下几行:

grow = (Button) findViewById(R.id.grow);
shrink = (Button) findViewById(R.id.shrink);
img_animation = (ImageView) findViewById(R.id.bluerect);
if (grow != null) grow.setOnClickListener(this);
if (shrink != null) shrink.setOnClickListener(this);

在这里,我获取了按钮和 ImageView 的句柄,并将当前类设置为 OnClickListener 的实现者。

接下来我将你的动画代码封装在 makeanimation 方法中:

private void makeAnimation(boolean shrink){
    if (shrink){
        enddim[0] -= 0.1f;
        enddim[1] -= 0.1f;
    }
    else{
        enddim[0] += 0.1f;
        enddim[1] += 0.1f;
    }
    ScaleAnimation fade_in = new ScaleAnimation(startdim[0], enddim[0], startdim[1], enddim[1],
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    fade_in.setDuration(1000); // animation duration in milliseconds
    fade_in.setFillAfter(true); // If fillAfter is true, the transformation that this animation performed will persist when it is finished.
    if (img_animation != null) {
        img_animation.startAnimation(fade_in);
        startdim[0] = enddim[0];
        startdim[1] = enddim[1];
    }
}

最后我重写 OnClick 方法来获取按钮点击:

@Override
public void onClick(View v) {
    if(grow == v){
        makeAnimation(false);
    }
    if(shrink == v){
        makeAnimation(true);
    }
}

关于java - 如何在 xml 中绘制图像,但在 java 类中更改尺寸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447390/

相关文章:

java - 在方法之间传递信息

android - 从 getBuyIntent 返回的 PendingIntent 始终为空

android - 将 byteArray 转换为 android 中的位图?

c# - 是否可以向序列化集合添加属性?

java - 尝试添加进度时进度条变为空

c# - 使用内联对象方法调用与声明新变量

android - facebook sdk 的 Proguard 配置。剥离除分析之外的所有内容

json - REST 中的资源和资源表示有什么区别?

java - 如何用Java从xml文件中删除元素

java - 当rebel.load_embedded_plugins设置为false时,如何在JRebel中热部署.jsp?