Android: Animate Custom Drawable Programmatically

I have a custom class extending Drawable as shown below which is for drawing Arc. From my MainActivity controlling Drawable, I redraw it based on some input values, which I translated into the appropriate "corner" for the shape. For example.

This is the initial state of the drawable: enter image description here

This is the second state: The enter image description here red arrow indicates the movement I am trying to achieve

I'm trying to "animate" a change from state 1 to state 2 with wide motion. Any ideas on how to do this? Should I redraw the shape several times, gradually transitioning between states one and two?

My Drawable Code:

public class CircleLoadingBar extends Drawable implements Drawable.Callback{

private Paint paint;
private Canvas canvas;
private float angle;
private RectF outterCircle;
private float padding=30;

public void invalidateDrawable(Drawable drawable){
    final Callback callback = getCallback();
    if (callback != null) {
        callback.invalidateDrawable(this);
    }
}

public void scheduleDrawable(Drawable drawable, Runnable runnable, long l){
    invalidateDrawable(drawable);
}

public void unscheduleDrawable(Drawable drawable,Runnable runnable){
    //empty
}

public CircleLoadingBar(){
    this(0);
}

public CircleLoadingBar( int angle){
    this.angle=angle;
    this.canvas=new Canvas();

    paint=new Paint();
    paint.setColor(Color.GREEN);
    paint.setStrokeWidth(padding);
    paint.setAntiAlias(true);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStyle(Paint.Style.STROKE);
    outterCircle = new RectF();
}

public void setAngle(float angle){
    this.angle=angle;
}

@Override
public void draw(Canvas canvas){

    canvas.save();

    Rect bounds = getBounds();

    outterCircle.set(bounds.left+padding,bounds.top+padding,bounds.right-padding,bounds.bottom-padding);

    int[] colors = {Color.RED, Color.GREEN, Color.RED};
    float[] positions = {0,0.2f,1.3f};
    SweepGradient gradient3 = new SweepGradient(innerCircle.centerX(), innerCircle.centerY(),colors,positions);

    paint.setShader(gradient3);

    canvas.drawArc(outterCircle,90,angle,true,paint);

}

@Override
public void setAlpha(int alpha) {
    // Has no effect
}

@Override
public void setColorFilter(ColorFilter cf) {
    // Has no effect
}

@Override
public int getOpacity() {
    // Not Implemented
    return 0;
}
}

      

My MainActivity Code:

public class MainActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate();
    setContentView(R.layout.main);
    textView= (TextView) findViewById(R.id.textview);
    circleLoadingBar= new CircleLoadingBar(10);
    textView.setBackgroundDrawable(circleLoadingBar);

}

  public void stateUpdate(float angle) {

        circleLoadingBar.setAngle(angle);
        textView.invalidate();
}
}

      

+3


source to share


1 answer


After searching a lot, I found the answer.

Summary: I needed the Custom Drawable class to implement the Drawable.Callback and Runnable interfaces (see code below).

CustomDrawable.class



public class CustomDrawable extends Drawable implements Drawable.Callback, Runnable{

    private Paint paint;
    private Canvas canvas;
    private int angle;
    private RectF circle;
    private float cx,cy;
    private float mHeight,mWidth=100;
    private float mRadius=20;
    private Drawable.Callback cb;
    private boolean running=false;


    public void invalidateDrawable(Drawable drawable){
        super.invalidateSelf(); //This was done for my specific example. I wouldn't use it otherwise
    }


    public void scheduleDrawable(Drawable drawable, Runnable runnable, long l){
        invalidateDrawable(drawable);
    }

    public void unscheduleDrawable(Drawable drawable,Runnable runnable){
        super.unscheduleSelf(runnable);
    }

    public CircleLoadingBar(){
        this(0);
    }

    public CircleLoadingBar(int angle){
        this.angle=angle;

        paint=new Paint();
        paint.setColor(Color.GREEN);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);

        circle= new RectF();
    }


    @Override
    public void draw(Canvas canvas){

        canvas.save();

        Rect bounds = getBounds();

        circle.set(bounds);
        canvas.drawArc(circle, 90, angle, true, paint);

    }

    public void nextFrame(){
        unscheduleSelf(this);
        scheduleSelf(this, SystemClock.uptimeMillis() + 250);
    }

    public void stop(){
        running=false;
        unscheduleSelf(this);
    }

    public void start(){
        if(!running){
            running=true;
            nextFrame();
        }
    }

    public void run(){
        angle++;
        invalidate();
        nextFrame();
    }

    @Override
    public void setAlpha(int alpha) {
        // Has no effect
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        // Has no effect
    }

    @Override
    public int getOpacity() {
        // Not Implemented
        return 0;
    }
  }

      

Now that your Drawable implements both, you can simply "start" it as a separate thread, initiated and controlled by your activity using start and stop functions.

+2


source







All Articles