3

我想使用TransitionDrawable. 但是,TransitionDrawable只允许两个图像。如何动态更改图像?

4

1 回答 1

0

如果要使用 转换两个以上的图像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);
            }
        });
    }
}

我希望这与您正在寻找的内容相似。

于 2018-06-30T05:01:35.140 回答