Spinning progress bar in every listview item

后端 未结 6 557
日久生厌
日久生厌 2021-02-01 08:30

I\'ve been scratching my head over this for a long time now and searched for an answer without any luck! It seems to be trivial, but as far as I know, it isn\'t.

I use a

6条回答
  •  难免孤独
    2021-02-01 08:58

    Here's how to make it work. Use just one AnimationDrawable, multiplex the callback. ProgressBar doesn't really add anything, you might as well stick with View. Make a subclass of View that bypasses the Drawable management.

    public class Whirly extends View
    {
       Drawable image = null;
    
       public Whirly(Context context, AttributeSet attr)
       {
          super(context, attr);
       }
    
       @Override
       public void draw(Canvas canvas)
       {
          if (image!=null)
             image.draw(canvas);
       }
    
       public void setImageDrawable(Drawable image)
       {
          this.image = image;
          invalidate();
       }
    }
    

    Then make your activity keep track of all the whirlies somehow.

    public class Demo extends Activity implements Drawable.Callback
    {
       private Handler h;
       private AnimationDrawable a;
    
       private Whirly w0;
       private Whirly w1;
       private Whirly w2;
       private Whirly w3;
    
       public void onCreate(Bundle bundle)
       {
          ...
    
          h = new Handler();
    
          a = (AnimationDrawable) getResources().getDrawable(R.drawable.mywhirly);
          int width = a.getIntrinsicWidth();
          int height = a.getIntrinsicHeight();
          a.setBounds(0, 0, width, height);
    
          w0 = (Whirly) findViewById(R.id.whirly0);
          w1 = (Whirly) findViewById(R.id.whirly1);
          w2 = (Whirly) findViewById(R.id.whirly2);
          w3 = (Whirly) findViewById(R.id.whirly3);
    
          w0.setImageDrawable(a);
          w1.setImageDrawable(a);
          w2.setImageDrawable(a);
          w3.setImageDrawable(a);
    
          a.setCallback(this);
          a.start();
       }
    
       public void invalidateDrawable (Drawable who)
       {
          w0.invalidate();
          w1.invalidate();
          w2.invalidate();
          w3.invalidate();
       }
    
       public void scheduleDrawable (Drawable who, Runnable what, long when)
       {
          h.postAtTime(what, who, when);
       }
    
       public void unscheduleDrawable (Drawable who, Runnable what)
       {
          h.removeCallbacks(what, who);
       }
    }
    

提交回复
热议问题