我缺乏如何使用crossfading
动画使图像视图自动动画而无需onclick的知识。所以我创建了一个source,它是带有交叉渐变动画的imageview。但它需要在图像上单击以触发图像更改动画。
我希望使用crossfading
动画自动更改imageview。
我的代码如下:
private BitmapDrawable drawables[];
private int mCurrentDrawable = 0;
private int imagesToShow[] = { R.drawable.bg_carousel1,R.drawable.bg_carousel2,
R.drawable.bg_carousel3, R.drawable.bg_carousel4, R.drawable.bg_carousel5, R.drawable.bg_carousel6,
R.drawable.bg_carousel7, R.drawable.bg_carousel8};
// This app works by having two views, which get faded in/out for the cross-fade effect
final ImageView prevImageView = findViewById(R.id.prevImageView);
final ImageView nextImageView = findViewById(R.id.nextImageView);
prevImageView.setBackgroundColor(Color.TRANSPARENT);
nextImageView.setBackgroundColor(Color.TRANSPARENT);
// Setup default ViewPropertyAnimator durations for the two ImageViews
prevImageView.animate().setDuration(1000);
nextImageView.animate().setDuration(1000);
// NOte that a real app would do this more robustly, and not just load all possible
// bitmaps at onCreate() time.
drawables = new BitmapDrawable[imagesToShow.length];
for (int i = 0; i < imagesToShow.length; ++i) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
imagesToShow[i]);
drawables[i] = new BitmapDrawable(getResources(), bitmap);
}
prevImageView.setImageDrawable(drawables[0]);
nextImageView.setImageDrawable(drawables[1]);
prevImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Use ViewPropertyAnimator to fade the previous imageView out and the next one in
prevImageView.animate().alpha(0).withLayer();
nextImageView.animate().alpha(1).withLayer().
withEndAction(new Runnable() {
// When the animation ends, set up references to change the prev/next
// associations
@Override
public void run() {
mCurrentDrawable =
(mCurrentDrawable + 1) % drawables.length;
int nextDrawableIndex =
(mCurrentDrawable + 1) % drawables.length;
prevImageView.setImageDrawable(drawables[mCurrentDrawable]);
nextImageView.setImageDrawable(drawables[nextDrawableIndex]);
nextImageView.setAlpha(0f);
prevImageView.setAlpha(1f);
}
});
}
});