Android:自定义view实现动画

在做android APP开发时,我们需要自定义View和自定义动画,例如:
这里写图片描述

实现过程为
代码:

package com.example.administrator.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;

/**
 * Created by HBL on 2016/2/1.
 */

public class AmountView extends View {
    Paint paint;
    int count = 120; //总刻度数
    int tempcount = 0;
    private BarAnimation anim;

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setColor(0xff388e3c);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(3);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        anim = new BarAnimation();
    }
    public AmountView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(canvas.getWidth() / 2, canvas.getHeight() / 2);
        Paint tmpPaint = new Paint(paint); //小刻度画笔
        tmpPaint.setStrokeWidth(2);
        float y = 180;
        for (int i = 0; i < tempcount; i++) {
            canvas.drawLine(0f, y, 0f, y + 45f, tmpPaint);
            canvas.rotate(360 / count, 0f, 0f); //旋转画纸
            Log.d("AmountView", "canvas.restore();" + i);
        }
    }

    public void startAnim() {
        this.startAnimation(anim);
    }

    public class BarAnimation extends Animation {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (interpolatedTime < 1.0f) {
                tempcount = (int) (count * interpolatedTime);
                postInvalidate();
                Log.d("AmountView", "tempcount:" + tempcount);
            } else {
                tempcount=count;
            }
        }
        @Override
        public void initialize(int width, int height, int parentWidth, int parentHeight) {
            super.initialize(width, height, parentWidth, parentHeight);
            setDuration(700);
            //设置动画结束后保留效果
            setFillAfter(true);
            tempcount = 0;
            setInterpolator(new LinearInterpolator());
        }
    }
}

http://wang-peng1.iteye.com/blog/572886

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值