android开发之实现动态打勾效果(DrawHookView)

今天产品中要实现这样的需求,想了想还是把它给整出来了! 
这里写图片描述

第一步:自定义View 
实现步骤: 
1、先画好圆弧 
2、再画第一根线 
3、最后再画第二根线

[java]  view plain copy
  1. /** 
  2.  * DrawHook 
  3.  * Created by Zane on 2015/3/4. 
  4.  */  
  5. public class DrawHookView extends View {  
  6.     //绘制圆弧的进度值  
  7.     private int progress = 0;  
  8.     //线1的x轴  
  9.     private int line1_x = 0;  
  10.     //线1的y轴  
  11.     private int line1_y = 0;  
  12.     //线2的x轴  
  13.     private int line2_x = 0;  
  14.     //线2的y轴  
  15.     private int line2_y = 0;  
  16.   
  17.     public DrawHookView(Context context) {  
  18.         super(context);  
  19.     }  
  20.   
  21.     public DrawHookView(Context context, AttributeSet attrs) {  
  22.         super(context, attrs);  
  23.     }  
  24.   
  25.     public DrawHookView(Context context, AttributeSet attrs, int defStyle) {  
  26.         super(context, attrs, defStyle);  
  27.     }  
  28.   
  29.     //绘制  
  30.   
  31.     @Override  
  32.     protected void onDraw(Canvas canvas) {  
  33.         super.onDraw(canvas);  
  34.   
  35.         progress++;  
  36.   
  37.         /** 
  38.          * 绘制圆弧 
  39.          */  
  40.         Paint paint = new Paint();  
  41.         //设置画笔颜色  
  42.         paint.setColor(getResources().getColor(R.color.arc_blue));  
  43.         //设置圆弧的宽度  
  44.         paint.setStrokeWidth(5);  
  45.         //设置圆弧为空心  
  46.         paint.setStyle(Paint.Style.STROKE);  
  47.         //消除锯齿  
  48.         paint.setAntiAlias(true);  
  49.   
  50.         //获取圆心的x坐标  
  51.         int center = getWidth() / 2;  
  52.         int center1 = center - getWidth() / 5;  
  53.         //圆弧半径  
  54.         int radius = getWidth() / 2 - 5;  
  55.   
  56.         //定义的圆弧的形状和大小的界限  
  57.         RectF rectF = new RectF(center - radius -1, center - radius -1 ,center + radius + 1, center + radius + 1);  
  58.   
  59.         //根据进度画圆弧  
  60.         canvas.drawArc(rectF, 235, -360 * progress / 100false, paint);  
  61.   
  62.         /** 
  63.          * 绘制对勾 
  64.          */  
  65.         //先等圆弧画完,才话对勾  
  66.         if(progress >= 100) {  
  67.             if(line1_x < radius / 3) {  
  68.                 line1_x++;  
  69.                 line1_y++;  
  70.             }  
  71.             //画第一根线  
  72.             canvas.drawLine(center1, center, center1 + line1_x, center + line1_y, paint);  
  73.   
  74.             if (line1_x == radius / 3) {  
  75.                 line2_x = line1_x;  
  76.                 line2_y = line1_y;  
  77.                 line1_x++;  
  78.                 line1_y++;  
  79.             }  
  80.             if (line1_x >= radius / 3 && line2_x <= radius) {  
  81.                 line2_x++;  
  82.                 line2_y--;  
  83.             }  
  84.             //画第二根线  
  85.             canvas.drawLine(center1 + line1_x - 1, center + line1_y, center1 + line2_x, center + line2_y, paint);  
  86.         }  
  87.   
  88.         //每隔10毫秒界面刷新  
  89.         postInvalidateDelayed(10);  
  90.     }  
  91. }  

第二步:布局文件引用自定义View

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:orientation="vertical"  
  4.               android:layout_width="fill_parent"  
  5.               android:layout_height="fill_parent"  
  6.               android:background="@color/color_fff"  
  7.         >  
  8.     <com.offcn.DrawHookViewDemo.DrawHookView  
  9.             android:layout_width="90dp"  
  10.             android:layout_height="90dp"  
  11.             android:layout_centerInParent="true"  
  12.             />  
  13. </RelativeLayout>  

附colors.xml:

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="arc_blue">#10a679</color>  
  4.     <color name="color_fff">#ffffff</color>  
  5. </resources>  

示例代码戳Here

https://github.com/ZaneLove/DrawHookView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Paint和Canvas上绘制动态打勾和打叉效果,可以按照以下步骤进行: 1. 创建一个自定义的View,重写它的onDraw()方法。 2. 在onDraw()方法中,使用Canvas绘制一个圆圈,并设置圆圈的颜色和边框宽度。 3. 绘制打勾或打叉的动画。对于打勾效果,可以使用Path对象来绘制一条斜线和一条水平线组成的打勾形状。对于打叉效果,可以使用Path对象来绘制两条斜线组成的打叉形状。 4. 在onDraw()方法中,使用Paint绘制动画效果,设置绘制的颜色和宽度。 5. 在View中设置动画效果,使用ValueAnimator或ObjectAnimator来控制动画的进度,然后在onDraw()方法中使用动画进度来绘制动画效果。 以下是一个简单的示例代码,可以绘制一个打勾动态效果: ```java public class TickView extends View { private Paint mPaint; private Path mPath; private float mProgress; public TickView(Context context) { super(context); init(); } public TickView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public TickView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mPaint.setStrokeWidth(8); mPaint.setColor(Color.GREEN); mPath = new Path(); mPath.moveTo(50, 150); mPath.lineTo(100, 200); mPath.lineTo(200, 100); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制圆圈 canvas.drawCircle(150, 150, 100, mPaint); // 绘制打勾效果 Path dst = new Path(); PathMeasure measure = new PathMeasure(mPath, false); measure.getSegment(0, measure.getLength() * mProgress, dst, true); canvas.drawPath(dst, mPaint); } public void startAnimation() { ValueAnimator animator = ValueAnimator.ofFloat(0, 1); animator.setDuration(1000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mProgress = (float) animation.getAnimatedValue(); invalidate(); } }); animator.start(); } } ``` 这个示例代码中,TickView继承自View,并重写了它的onDraw()方法。在init()方法中,我们创建了一个Paint对象和一个Path对象,用来绘制打勾的形状。在onDraw()方法中,我们首先绘制了一个圆圈,然后使用PathMeasure对象来截取Path对象的一部分,根据动画进度来绘制打勾效果。最后,在startAnimation()方法中,我们创建了一个ValueAnimator对象来控制动画的进度,并在onAnimationUpdate()方法中更新动画进度,然后调用invalidate()方法来重绘View

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值