Animation to increase the size of a rectangle in custom view

时间:2016-10-20 13:05:20

标签: android animation android-custom-view ondraw

I'm creating a custom view in which I have a rectangle RectF object that have a specific height. I would like to increase the bottom Y point coordinate to a specific value with a progressive animation.

I've tried the following. I've created a method setBatteryState() that is called on a onclicked method in the activity that holds the custom view:

public class BatteryView extends View {

public int mCanvasWidth;
public int mCanvasHeight;

public RectF mBatteryHead;
public RectF mBatteryBody;
public RectF mBatteryBodyVolume;
public Canvas mCanvas;

public BatteryView(Context context) {
    super(context);
}

public BatteryView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void init()
{

}

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);
    this.mCanvas = canvas;
    float batteryHeadDistanceFromLeft = mCanvasWidth / 3;
    float batteryHeadWidth = mCanvasWidth / 3;

    float batteryBodyDistanceFromTop = mCanvasHeight / 5;
    float batteryHeadHeight = mCanvasHeight / 5;
    mBatteryHead = new RectF(batteryHeadDistanceFromLeft,0,2*batteryHeadWidth,batteryHeadHeight+5);
    Paint batteryHeadPaint = new Paint();
    batteryHeadPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor));
    canvas.drawRect(mBatteryHead,batteryHeadPaint);

    mBatteryBody = new RectF(0,(int)batteryBodyDistanceFromTop,mCanvasWidth,mCanvasHeight);
    Paint batteryBodyPaint = new Paint();
    batteryBodyPaint.setStyle(Paint.Style.STROKE);
    batteryBodyPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor));
    batteryBodyPaint.setStrokeWidth(10);
    canvas.drawRect(mBatteryBody,batteryBodyPaint);

    mBatteryBodyVolume = new RectF(12,(int)batteryBodyDistanceFromTop + 10,mCanvasWidth-12,mCanvasHeight/2);
    Paint volumeBodyPaint = new Paint();
    volumeBodyPaint.setColor(ContextCompat.getColor(getContext(), R.color.batifyColor));
    canvas.drawRect(mBatteryBodyVolume,volumeBodyPaint);
}


public void setStateOnBattery(){
    ObjectAnimator animateBottom = ObjectAnimator.ofFloat(mBatteryBodyVolume, "bottom", mBatteryBodyVolume.bottom, mCanvasHeight);
    animateBottom.setDuration(1000).start();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    mCanvasWidth = MeasureSpec.getSize(widthMeasureSpec);
    mCanvasHeight = MeasureSpec.getSize(heightMeasureSpec);
}}

ObjectAnimator should translate the rect mBatteryBodyVolume to the size of the canvas but nothing change...

Any Idea ?

Thanks in advance !

1 个答案:

答案 0 :(得分:0)

Use an asynchronous task with 2 major functions, draw and update. Every time update is called, increase the height variable by a constant. Then, in draw, draw your rectangle with height as a param. If you need code, just ask. :D

UPDATE

Create a 'runner' async task:

public class Runner extends Thread {

    public volatile boolean running = true;
    private Environment env;

    public Runner(Environment E) {
        env = E;
    }

    @Override
    public void run() {
        long lastTime = System.currentTimeMillis();

        while(running) {
            long now = System.currentTimeMillis();
            long elapsed = now - lastTime;

            env.update(elapsed);
            env.draw();

            lastTime = now;
        }
    }

    public void shutdown() {
        running = false;
    }
}

In Environment, Do the following:

public void draw() {
    Canvas canvas = holder.lockCanvas();

    if (canvas != null) {
        canvas.drawRect(x-w, y-h, x+w, y+h, myPaint);
        holder.unlockCanvasAndPost(canvas);
    }
}

and the update method:

public void update(float elapsedTime) {
    h+=myKonstant*elpasedTime;
}

Hope I Helped :D