GetView中的AnimationDrawable

时间:2013-12-21 06:20:06

标签: android android-listview android-animation

我开发了ListView,其中有一个TextView和两个ImageView。我为每个ImageView设置了AnimationDrawable。我的问题是当我向下滚动然后到顶部时动画再次启动。我已经为画面设置了5张图像。例如,如果ImageView显示第三张图像,我向下滚动然后向上滚动。现在ImageView显示第一个图像,这意味着动画再次启动。我想在滚动后保留动画。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;
     if (vi == null) {
            vi = adapterInflater.inflate(R.layout.categoryadapter, null);
            holder = new ViewHolder();
            holder.lPic = (ImageView) vi.findViewById(R.id.imageView1);
            holder.rPic = (ImageView) vi.findViewById(R.id.imageView2);
            holder.category = (TextView) vi.findViewById(R.id.textView_category);
            vi.setTag(holder);
     }
     else
     {
        holder = (ViewHolder) vi.getTag();
     }
     fontStyle = Typeface.createFromAsset(adapterActivity.getAssets(),
                "fonts/Helvetica.TTF");


    final Categary assingValue = list.get(position);
    holder.category.setTypeface(fontStyle);
    holder.category.setText(assingValue.desc);

    leftImage=assingValue.leftPicture;
    rightImage=assingValue.rightPicture;

    AnimationDrawable animation = new AnimationDrawable();
    for (String image : leftImage) {
           ImageLoader obj=new ImageLoader(adapterActivity);    
           holder.lPic.setTag(image);
           Bitmap b=obj.getBitmap(image, holder.lPic);
           Drawable d = new BitmapDrawable(adapterActivity.getResources(),b);          
           animation.addFrame(d, 15 * 1000);
    }
    holder.lPic.setImageDrawable(animation);
    animation.setOneShot(false);


    AnimationDrawable animation1 = new AnimationDrawable();
    for (String image : rightImage) {

           ImageLoader obj=new ImageLoader(adapterActivity);    
           holder.rPic.setTag(image);
           Bitmap b=obj.getBitmap(image, holder.rPic);
           Drawable d = new BitmapDrawable(adapterActivity.getResources(),b);
           animation1.addFrame(d, 15 * 1000);
    }
    holder.rPic.setImageDrawable(animation1);
    animation1.setOneShot(false);

    animation.start();
    animation1.start();     

    return vi;
}

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

在适配器的构造函数中创建所有视图并存储在列表中,然后在get视图中返回该视图。

代表:

@Override
public View createView(final int position) {

    View vi = null;
    ViewHolder holder;

    vi = adapterInflater.inflate(R.layout.categoryadapter, null);
    holder = new ViewHolder();
    holder.lPic = (ImageView) vi.findViewById(R.id.imageView1);
    holder.rPic = (ImageView) vi.findViewById(R.id.imageView2);
    holder.category = (TextView) vi.findViewById(R.id.textView_category);
    fontStyle = Typeface.createFromAsset(adapterActivity.getAssets(),
                "fonts/Helvetica.TTF");
    final Categary assingValue = list.get(position);
    holder.category.setTypeface(fontStyle);
    holder.category.setText(assingValue.desc);

    leftImage=assingValue.leftPicture;
    rightImage=assingValue.rightPicture;

    AnimationDrawable animation = new AnimationDrawable();
    for (String image : leftImage) {
           ImageLoader obj=new ImageLoader(adapterActivity);    
           holder.lPic.setTag(image);
           Bitmap b=obj.getBitmap(image, holder.lPic);
           Drawable d = new BitmapDrawable(adapterActivity.getResources(),b);          
           animation.addFrame(d, 15 * 1000);
    }
    holder.lPic.setImageDrawable(animation);
    animation.setOneShot(false);
    AnimationDrawable animation1 = new AnimationDrawable();
    for (String image : rightImage) {

           ImageLoader obj=new ImageLoader(adapterActivity);    
           holder.rPic.setTag(image);
           Bitmap b=obj.getBitmap(image, holder.rPic);
           Drawable d = new BitmapDrawable(adapterActivity.getResources(),b);
           animation1.addFrame(d, 15 * 1000);
    }
    holder.rPic.setImageDrawable(animation1);
    animation1.setOneShot(false);
    animation.start();
    animation1.start();     
    return vi;
}

为每个项目调用以上函数,并将返回的视图存储在视图列表中:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    return views.get(position);
}

我没试过,但这可能会奏效。

让我知道会发生什么。