【问题标题】:Problem in AnimationDrawable android studioAnimationDrawable android studio中的问题
【发布时间】:2019-01-28 16:04:22
【问题描述】:

我有animationdrawable 我想在两个imageview 中使用相同的animationdrawable。 问题第一个不工作,第二个工作。

AnimationDrawable animation1 = new AnimationDrawable();
Bitmap bitmapba1 = BitmapFactory.decodeResource(getResources(),R.drawable.a);
Bitmap bitmapba2 = BitmapFactory.decodeResource(getResources(), R.drawable.b);
bitmapba1=Bitmap.createScaledBitmap(bitmapba1,x,x,false);
bitmapba2=Bitmap.createScaledBitmap(bitmapba2,x,x,false);
animation1.addFrame(new BitmapDrawable(bitmapba1), 20);
animation1.addFrame(new BitmapDrawable(bitmapba2), 20);
myimage1.setImageDrawable(animation1);
myimage2.setImageDrawable(animation1);

问题已解决但效率低下(原始)我声明第二个动画 2 与 bitmapba1 和 bitmapba2 相同:animation2.addFrame(...(bitmapba1), 20) 和 animation2.addFrame(...(bitmapba2), 20)。

问题是如果有 100 个 imageview 它们共享同一个 animationdrawable 怎么办?

【问题讨论】:

  • 你需要 2 个对象 AnimationDrawable 动画 1、动画 2。然后animation2.addFrame(new BitmapDrawable(bitmapba2), 20); myimage2.setImageDrawable(animation2);
  • 我这样做,但如果我想做声明 100 imageview,我想要另一个更有效。它不是逻辑动画 1、动画 2...动画 100 相同的位图 1、位图 2 这种重复(加载内存)。跨度>

标签: android animation animationdrawable


【解决方案1】:

正如Style-7 所写,对于ImageView 的每个实例,您应该创建自己的AnimationDrawable 实例。问题是AnimationDrawable 的实例有自己的状态。一旦您在多个视图之间共享此单个实例,此状态就会“流泪”。

但您不应该为每个动画保留位图副本。 加载一次,然后配置动画。

Bitmap bitmapba1 = BitmapFactory.decodeResource(getResources(),R.drawable.a);
Bitmap bitmapba2 = BitmapFactory.decodeResource(getResources(), R.drawable.b);
bitmapba1=Bitmap.createScaledBitmap(bitmapba1,x,x,false);
bitmapba2=Bitmap.createScaledBitmap(bitmapba2,x,x,false);

for(ImageView view : listOfViews){
    AnimationDrawable animation = new AnimationDrawable();
    animation.addFrame(new BitmapDrawable(bitmapba1), 20);
    animation.addFrame(new BitmapDrawable(bitmapba2), 20);
    view.setImageDrawable(animation);
}

我们还应该为每个动画实例创建BitmapDrawable 的新实例,因为它也有自己的状态。但是每个这样的新实例只保留对Bitmap 对象的引用,并且不会为每个新实例复制位图数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多