如何为自定义视图设置动画?

时间:2018-06-04 19:47:43

标签: java android android-animation custom-view

我正在尝试为自定义视图制作动画。但是,当我启动我的应用程序时,我的自定义视图没有附带动画。

我从一些文章中尝试了一些代码。它工作,但当我自己做它冻结。我也试过这个网站的一些代码,也没有附带动画。

这里是我的代码

class MyView extends LinearLayout
       {

private float powerPos;
private RectF rect;
private Paint paint;


GameView(Context ctx){
    super(ctx);
    init();
    setBackgroundColor(Color.BLUE);
}

public void drawPower(float i){
    powerPos = i;
}

private void init(){
    rect = new RectF(100, 100, 400, 400);
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    paint.setStyle(Paint.Style.STROKE);
    paint.setTextSize(70);
    paint.setTypeface(Typeface.MONOSPACE);

}





@Override
protected void onDraw(Canvas canvas)
{

  canvas.drawText("i am tge power",powerPos,rect.centerY(),paint);


    super.onDraw(canvas);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
}

@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();




}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
}


private void animateArch() {
    final android.animation.ValueAnimator frontEndExtend = android.animation.ValueAnimator.ofFloat(0, 500);
    frontEndExtend.setDuration(5000);
    frontEndExtend.setInterpolator(new android.view.animation.LinearInterpolator());
    frontEndExtend.addUpdateListener(new android.animation.ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(android.animation.ValueAnimator animation) {


            }
        });
    frontEndExtend.start();
    frontEndExtend.addListener(new android.animation.ValueAnimator.AnimatorListener() {
            @Override
            public void onAnimationStart(android.animation.Animator animator) {
                float value = ((Float) (frontEndExtend.getAnimatedValue())).floatValue();
                powerPos = value;
            }

            @Override
            public void onAnimationEnd(android.animation.Animator  animator) {
                animateArch();
            }

            @Override
            public void onAnimationCancel(android.animation.Animator  animator) {

            }

            @Override
            public void onAnimationRepeat(android.animation.Animator  animator) {

            }
        });
}}

我离开了什么吗?

0 个答案:

没有答案