Java 如何在Android的listview中设置单个imageview的动画?
我有一个自定义的listview,它在每个listview项中都有ImageView。我尝试在加载单个imageview后在其上应用alpha动画,但该动画应用于同一listview中的所有ImageViwe,这使得已加载的动画一次又一次闪烁 我在基本适配器类中的getView方法中设置ImageView动画:Java 如何在Android的listview中设置单个imageview的动画?,java,android,animation,Java,Android,Animation,我有一个自定义的listview,它在每个listview项中都有ImageView。我尝试在加载单个imageview后在其上应用alpha动画,但该动画应用于同一listview中的所有ImageViwe,这使得已加载的动画一次又一次闪烁 我在基本适配器类中的getView方法中设置ImageView动画: @Override public View getView(int arg0, View arg1, ViewGroup arg2) { View
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View v = arg1;
if(arg1 == null){
v = infalter.inflate(R.layout.home_main_list_view_row, null);
}
ImageView cover = (ImageView) v.findViewById(R.id.homeCoverImageView);
AlphaAnimation alpha = (AlphaAnimation) AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
cover.startAnimation(alpha);
return v;
}
我到处找,但还没有找到我想要的
编辑
异步任务:
class downloadImages extends AsyncTask<Void, Void, Void> {
Bitmap bitmap = null;
String link = null;
int index = 0;
@Override
protected Void doInBackground(Void... params) {
for(int x = covers.size(); x < covers_links.size(); x++){
index = x;
link = covers_links.get(x).trim();
if(downloadImages.isCancelled()){
break;
}else if(link.matches("")){
covers.add(null);
}else{
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true;
try {
BitmapFactory.decodeStream(new URL(link).openStream(), null, bfo);
int scale = calculateInSampleSize(bfo, 275, 300);
bfo = new BitmapFactory.Options();
bfo.inSampleSize = scale;
bitmap = BitmapFactory.decodeStream(new URL(link).openStream(), null, bfo);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
covers.add(x, bitmap);
}
publishProgress();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
listViewAdapter.notifyDataSetChanged();
}
}
Amigo,我认为您正在做的有点混乱,但是如果您想继续这样做,您可以使用以下方法在listview中获得孩子的特定ImageView:
ImageView cover = (ImageView) mListView.getChildAt(index).findViewById(R.id.homeCoverImageView);
然后,您可以将动画应用到我在旧答案中显示的ObjectAnimator封面
如果要这样做,我只需在适配器中使用getView()
方法中的。。您可以将所有代码缩减为一行,它将从url加载图像,调整其大小,然后淡入(在一条语句中!!)
加载图像后,图像将自动淡入封面
图像视图。。不要试图重新发明轮子,我的朋友
旧答案
您需要设置要设置动画的imageView的位置。。当为每个项目调用getView
方法时,将为每个视图调用动画
尝试这样做(对于imageview位置1)
在getView
...
ImageView cover = (ImageView) v.findViewById(R.id.homeCoverImageView);
if(arg0 == 1){
ObjectAnimator fade = ObjectAnimator.ofFloat(cover, "alpha", 0.0f, 1.0f);
fade.setDuration(400);
fade.start();
}
...
感谢您的贡献,但我看不到您在代码片段中指定了任何位置。它检查位置1,但在所有ImageView上应用动画。这和我面临的问题是一样的。你试过这个代码吗?我已经测试过了,它能工作。。它不能将动画应用于所有ImageView,只有pos==1(arg0变量是位置!!)中的一个,其余的将不会被动画,因为位置不是1时不会调用ObjectAnimator!尝试将持续时间设置为5000,您可以看到差异,但如果我想在所有ImageView中依次应用alpha动画,但在图像加载后会怎么样?您是从internet加载图像吗??那样的话,你用哪个图书馆?是的,我是。我使用这种方法:
BitmapFactory.decodeStream
Picasso.with(context)
.load(url)
.resize(50, 50)
.centerCrop()
.into(cover)
...
ImageView cover = (ImageView) v.findViewById(R.id.homeCoverImageView);
if(arg0 == 1){
ObjectAnimator fade = ObjectAnimator.ofFloat(cover, "alpha", 0.0f, 1.0f);
fade.setDuration(400);
fade.start();
}
...