java - Android UI - 如何在使用 TransitionDrawable 时动态更改图像

标签 java android transitiondrawable

我想使用 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/

相关文章:

java - Android rom文件结构及调试

安卓 TransitionDrawable : image scaletype changes upon fragment resuming

java - 奇怪的探查器结果

java - 如何使用预签名 URL 将文件上传到 S3,而不将其加载到内存中

android - 房间库不接受字节数组

android - 超过 2 层的 Transition Drawable

java - 如何使用 Apache Avro 对 JSON 字符串进行 Avro 二进制编码?

java - 在 server.xml 之外定义上下文时遇到问题

java - Android API 中的 HexDump 在哪里?