如何使用 Java 代码为 ImageView 制作图像变化动画
How to make animation of image change for ImageView from Java code
我想用 TransitionDrawable class 制作它,但它需要一个单独的文件 transition.xml。在那里我定义了我要更改的图像。
我需要在 Java 代码中定义它们,因为我也不知道要更改哪些图像。我有很多图像,但我不小心只得到了两张图像,它们之间会发生变化。我能做什么?也许我需要另一个 class。
代码 transition.xml:
public class TransitionActivity extends Activity
implements OnClickListener {
private ImageView image;
private TransitionDrawable mTransition;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
image.setOnClickListener(this);
Resources res = this.getResources();
mTransition = (TransitionDrawable)res.getDrawable(R.drawable.transition);
}
@Override
public void onClick(View v) {
image.setImageDrawable(mTransition);
mTransition.startTransition(1000);
}
}
您可以通过编程方式创建 TransitionDrawable
using the class' constructor。您不需要从 XML 获取它。这使您可以灵活地动态分配在其间转换的 Drawable。
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);
我想用 TransitionDrawable class 制作它,但它需要一个单独的文件 transition.xml。在那里我定义了我要更改的图像。
我需要在 Java 代码中定义它们,因为我也不知道要更改哪些图像。我有很多图像,但我不小心只得到了两张图像,它们之间会发生变化。我能做什么?也许我需要另一个 class。
代码 transition.xml:
public class TransitionActivity extends Activity
implements OnClickListener {
private ImageView image;
private TransitionDrawable mTransition;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
image.setOnClickListener(this);
Resources res = this.getResources();
mTransition = (TransitionDrawable)res.getDrawable(R.drawable.transition);
}
@Override
public void onClick(View v) {
image.setImageDrawable(mTransition);
mTransition.startTransition(1000);
}
}
您可以通过编程方式创建 TransitionDrawable
using the class' constructor。您不需要从 XML 获取它。这使您可以灵活地动态分配在其间转换的 Drawable。
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);