《Android 自定义控件:选中状态的自定义动画按钮》

一、先看简单效果,制作GIF图太麻烦,现在直接贴图,然后说明效果。

效果:

1. 当点击按钮的时候,完成一个被点击的动画效果;

2. 动画效果一次是:圆-->勾。

 

二、分析

1. 要完成这个功能,肯定需要我们自定义控件;

2. 这是一个图象,我们肯定要画出来。那么安装常规,我们应该会先画圆,然后划勾,勾又需要分成两端,先由西北向东南画,然后由西南向东北画。

3. 动画效果,10毫秒刷新一次控件;

 

三、代码

1.自定义的View:

public class DrawHookView extends View {

    //绘制圆弧的进度值
    private int progress = 0;
    //线1的x轴
    private int line1_x = 0;
    //线1的y轴
    private int line1_y = 0;
    //线2的x轴
    private int line2_x = 0;
    //线2的y轴
    private int line2_y = 0;

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

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

    public DrawHookView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void redraw() {
        progress = 0;
        line1_x = 0;
        line1_y = 0;
        line2_x = 0;
        line2_y = 0;
        invalidate();
    }
    //绘制


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        progress++;
        //绘制圆弧
        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(getResources().getColor(R.color.arc_blue));
        //设置圆弧的宽度
        paint.setStrokeWidth(5);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.STROKE);
        //消除锯齿
        paint.setAntiAlias(true);

        //获取圆心的x坐标
        int center = getWidth() / 2;
        int center1 = center - getWidth() / 5;
        //圆弧半径
        int radius = getWidth() / 2 - 5;

        //定义的圆弧的形状和大小的界限
        RectF rectF = new RectF(center - radius - 1, center - radius - 1, center + radius + 1, center + radius + 1);
        //根据进度画圆弧
        canvas.drawArc(rectF, 235, -360 * progress / 100, false, paint);

        /**
         * 绘制对勾
         */
        //先等圆弧画完,才画对勾
        if (progress >= 100) {
            if (line1_x < radius / 3) {
                line1_x++;
                line1_y++;
            }
            //画第一根线
            canvas.drawLine(center1, center, center1 + line1_x, center + line1_y, paint);

            if (line1_x == radius / 3) {
                line2_x = line1_x;
                line2_y = line1_y;
                line1_x++;
                line1_y++;
            }
            if (line1_x >= radius / 3 && line2_x <= radius) {
                line2_x++;
                line2_y--;
            }
            canvas.drawLine(center1 + line1_x - 1, center + line1_y, center1 + line2_x, center + line2_y, paint);
        }
        //每隔10毫秒界面刷新
        postInvalidateDelayed(3);
    }
}

 2. 布局 xml调用

<com.bql.rvcamp.rvcamp.view.DrawHookView
    android:id="@+id/dhView1"
    android:layout_width="90dp"
    android:layout_height="90dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="30dp"
    android:layout_marginEnd="150dp" />

 

此处需要注意:com.bql.rvcamp.rvcamp.view.DrawHookView这个是根据包名来得,别弄错了。先看看你的报名,然后< + 提示一般都是可以找到这个自定义控件的,千万别直接copy。

3. 最后:点击事件调用

//在初始化的时候可让其先隐藏
dhView1.setVisibility(View.GONE);
iv_yingyin.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //点击之后显示出来
        dhView1.setVisibility(View.VISIBLE);
        //动画
        dhView1.redraw();
    }
});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值