gpt4 book ai didi

java - 显示有动画问题的 ImagesViews

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:26:49 24 4
gpt4 key购买 nike

我想使用动画在 GridView 中显示 ImageViews。我可以显示它们并正确应用动画。

但问题是,如果我在 ImageView1 上应用动画,然后在 ImageView2 上应用动画(在 ImageView1 上的动画结束之前),那么 ImageView1 上的动画会中断,ImageView1 会直接设置为最终状态。

请注意,我使用具有 2 个线程的 ThreadPoolExecutor 下载图像。每当图像下载完成时,我调用 ImageAdapter 下面的 addImage(image) 方法,它又将图像添加到 GridViewGridView 刷新以显示最新的图像集。在这里,在渲染时,新图像与动画一起显示,这个动画(我猜)会中断当前动画图像的动画。

图像适配器:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private List<Bitmap> images;
private ArrayList<Integer> colors;
private boolean[] alreadyLoadedIndexes;
Animation animation;
AnimatorSet animator;

public void addImage(Bitmap img) {
images.add(img);
notifyDataSetChanged();
}

public void setAnimation(Animation animation) {
this.animator = null;
this.animation = animation;
}

public void setAnimator(AnimatorSet animation) {
this.animation = null;
this.animator = animation;
}

public void resetImages() {
images.clear();
notifyDataSetChanged();
alreadyLoadedIndexes = null;
alreadyLoadedIndexes = new boolean[20];
}

// Constructor
public ImageAdapter(Context c) {
mContext = c;
images = new ArrayList<Bitmap>();
colors = new ArrayList<Integer>();
colors.add(Color.CYAN);
colors.add(Color.BLUE);
colors.add(Color.GRAY);
colors.add(Color.YELLOW);
colors.add(Color.MAGENTA);
alreadyLoadedIndexes = new boolean[20];
}

@Override
public int getCount() {
return 20;
}

@Override
public Object getItem(int position) {
return 20;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);

imageView
.setBackgroundColor(colors.get((int) ((Math.sqrt(position) + 2 * position) % 5)));

// If image at index 'position' is available
if (images.size() > position) {

// If loading this image first time then only apply animation
if (!alreadyLoadedIndexes[position]) {

// If animation is specified
if (this.animation != null) {
imageView.setImageBitmap(images.get(position));
imageView.startAnimation(animation);

}
// If animator set is specified
else if (this.animator != null) {
imageView.setImageBitmap(null);
imageView.setBackgroundColor(colors.get((int) ((Math
.sqrt(position) + 2 * position) % 5)));
animator.setTarget(imageView);
animator.start();
Log.e("", "setting image after " + animator.getDuration()
/ 2);
new Handler().postDelayed(
new runnable(imageView, images.get(position)), 500);

} else {
// Use default animation if nothing is specified.
imageView.setImageBitmap(images.get(position));
animation = AnimationUtils.loadAnimation(mContext,
R.anim.fade_in);
imageView.startAnimation(animation);
}

} else {
imageView.setImageBitmap(images.get(position));
}

alreadyLoadedIndexes[position] = true;

}

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
return imageView;
}

class runnable implements Runnable {

ImageView image;
Bitmap bitmap;

public runnable(ImageView img, Bitmap bitmap) {
this.image = img;
this.bitmap = bitmap;
}

@Override
public void run() {
Log.e("", "setting image.");
image.setImageBitmap(bitmap);
}

}

}

最佳答案

试试 GridView 的布局动画 Controller ,它将根据持续时间逐项动画 View 。检查这个link ,

关于java - 显示有动画问题的 ImagesViews,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19997663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com