Android怎么自定义View实现风车效果
本篇内容介绍了“Android怎么自定义View实现风车效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
效果图:
画杆
publicclassWindmillRodViewextendsView{privateintmWidth;privateintmHeight;privatePaintmPaint;publicWindmillRodView(Contextcontext){this(context,null);}publicWindmillRodView(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicWindmillRodView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);init();}privatevoidinit(){mPaint=newPaint();mPaint.setColor(Color.WHITE);mPaint.setStyle(Paint.Style.FILL);mPaint.setAntiAlias(true);mPaint.setDither(true);}@OverrideprotectedvoidonSizeChanged(intw,inth,intoldw,intoldh){super.onSizeChanged(w,h,oldw,oldh);mWidth=getMeasuredWidth();mHeight=getMeasuredHeight();}privateint_rod_width=dp2px(2);@OverrideprotectedvoidonDraw(Canvascanvas){intxCenter=mWidth/2;intyCenter=mHeight/3;intradius=mHeight/3*2;drawRod(canvas,xCenter,yCenter,radius);}privatevoiddrawRod(Canvascanvas,intxCenter,intyCenter,intradius){Pathpath=newPath();path.moveTo(xCenter-_rod_width,yCenter);path.lineTo(xCenter-2*_rod_width,radius-dp2px(5));path.lineTo((xCenter+2*_rod_width),radius-dp2px(5));path.lineTo(xCenter+_rod_width,yCenter);path.close();canvas.drawPath(path,mPaint);RectFrectF=newRectF(xCenter-2*_rod_width,radius-dp2px(8),xCenter+2*_rod_width,radius-dp2px(3));canvas.drawOval(rectF,mPaint);}privateintdp2px(intdp){return(int)(Resources.getSystem().getDisplayMetrics().density*dp+0.5);}}
先画风车的杆,再在底部画一个椭圆
画风车
publicclassWindmillViewextendsView{privateintmWidth;privateintmHeight;privatePaintmPaint;privateObjectAnimatormRotationAnim;publicWindmillView(Contextcontext){this(context,null);}publicWindmillView(Contextcontext,AttributeSetattrs){this(context,attrs,0);}publicWindmillView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);init();}privatevoidinit(){mPaint=newPaint();mPaint.setColor(Color.WHITE);mPaint.setStyle(Paint.Style.FILL);mPaint.setAntiAlias(true);mPaint.setDither(true);}@OverrideprotectedvoidonSizeChanged(intw,inth,intoldw,intoldh){super.onSizeChanged(w,h,oldw,oldh);mWidth=getMeasuredWidth();mHeight=getMeasuredHeight();}@OverrideprotectedvoidonDraw(Canvascanvas){intxCenter=mWidth/2;intyCenter=mHeight/3;intradius=mHeight/3*2;canvas.drawCircle(xCenter,yCenter-dp2px(7),dp2px(4),mPaint);setPivotX(xCenter);setPivotY(yCenter-dp2px(7));canvas.save();for(inti=0;i<3;i++){Pathpath=newPath();path.moveTo(xCenter,0);path.lineTo(xCenter,yCenter-dp2px(11));path.lineTo(xCenter+dp2px(8),yCenter-dp2px(26));path.close();//mPaint.setStrokeJoin(Paint.Join.ROUND);CornerPathEffectcornerPathEffect=newCornerPathEffect(30);mPaint.setPathEffect(cornerPathEffect);canvas.drawPath(path,mPaint);canvas.rotate(360/3,xCenter,yCenter-dp2px(7));}canvas.restore();startAnim();}privateintdp2px(intdp){return(int)(Resources.getSystem().getDisplayMetrics().density*dp+0.5);}publicvoidstartAnim(){if(mRotationAnim!=null&&mRotationAnim.isRunning())return;mRotationAnim=ObjectAnimator.ofFloat(this,"Rotation",360f).setDuration(3000);mRotationAnim.setRepeatCount(-1);mRotationAnim.setInterpolator(newLinearInterpolator());mRotationAnim.start();}publicvoidstopAnim(){if(mRotationAnim!=null&&mRotationAnim.isRunning()){mRotationAnim.cancel();mRotationAnim=null;}}}
这里使用画面的旋转方法,绘制扇页
旋转
使用属性动画来旋转
....publicvoidstartAnim(){if(mRotationAnim!=null&&mRotationAnim.isRunning())return;mRotationAnim=ObjectAnimator.ofFloat(this,"Rotation",360f).setDuration(3000);mRotationAnim.setRepeatCount(-1);mRotationAnim.setInterpolator(newLinearInterpolator());mRotationAnim.start();}publicvoidstopAnim(){if(mRotationAnim!=null&&mRotationAnim.isRunning()){mRotationAnim.cancel();mRotationAnim=null;}}....
在布局文件中使用
<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="schemas.android/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#000000"><.example.windmill.WindmillRodViewandroid:layout_width="200dp"android:layout_height="200dp"android:layout_centerInParent="true"/><.example.windmill.WindmillViewandroid:layout_width="200dp"android:layout_height="200dp"android:layout_centerInParent="true"/></RelativeLayout>
这里只是介绍了如何绘制类似的效果,很多计算都是写死的,如果要实际使用的话,最好写成自定义属性通过xml属性声明传进去。
“Android怎么自定义View实现风车效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注主机评测网网站,小编将为大家输出更多高质量的实用文章!
输入法切换键是哪个键?输入法切
冬月是哪一个月?冬月是什么意思
个人所得税退税金额怎么算出来的
输入法全角和半角有什么区别?输
a4纸尺寸是多少厘米?a4纸的由来
个人所得税扣除标准?个人所得税
输入法哪个好用?输入法介绍
卡拉OK是哪个国家发明的?卡拉OK
mikutools原神网页版入口链接?m
关机特别慢什么原因?电脑和手机