我想使用 TransitionDrawable
对一些图像进行转换。但是,TransitionDrawable
只允许两个图像。如何动态更改图像?
最佳答案
如果你想使用 TransitionDrawable
在两个以上的图像之间进行过渡,你可以执行如下操作。
这是我做的一个测试程序作为例子:
public class MainActivity extends AppCompatActivity {
private ImageView image;
private TransitionDrawable trans;
private Resources res;
private int images[] = {R.drawable.yourImage1, R.drawable.yourImage2, R.drawable.yourImage3};
private int imageIndex1;
private int imageIndex2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ConstraintLayout layout = new ConstraintLayout(this);
setContentView(layout);
imageIndex1 = 0;
imageIndex2 = 1;
image = new ImageView(this);
image.setImageResource(images[0]);
layout.addView(image);
res = this.getResources();
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
trans = new TransitionDrawable(new Drawable[]{res.getDrawable(images[imageIndex1]), res.getDrawable(images[imageIndex2])});
if (imageIndex1 == images.length-1) {
imageIndex1 = 0;
} else {
imageIndex1++;
}
if (imageIndex2 == images.length-1) {
imageIndex2 = 0;
} else {
imageIndex2++;
}
image.setImageDrawable(trans);
// Set the parameter value to whatever time you want
trans.reverseTransition(2000);
}
});
}
}
我希望这与您正在寻找的内容类似。
关于java - Android UI - 如何在使用 TransitionDrawable 时动态更改图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51111133/