首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GetView中的AnimationDrawable

GetView中的AnimationDrawable
EN

Stack Overflow用户
提问于 2013-12-21 14:20:06
回答 1查看 352关注 0票数 0

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

代码语言:javascript
复制
@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;
}
EN

回答 1

Stack Overflow用户

发布于 2013-12-21 15:09:25

尝试以下操作:

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

对于ex:

代码语言:javascript
复制
@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;
}

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

代码语言:javascript
复制
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    return views.get(position);
}

我还没试过,但这可能行得通。

让我知道会发生什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20716161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档