android如何自定义观点用路径画长方形

  介绍

这篇文章给大家分享的是有关android如何自定义观点用路径画长方形的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

android是什么

android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国谷歌公司和开放手机联盟领导及开发。

这次主要是练习一下安卓的自定义视图和路径的相关使用,所以做了一个简单的演示:自定义一个视图,并在用路径上面画一个可以动态改变圆角大小的长方形。

<>强自定义相关属性

自定义观点首先需要在价值观文件夹下建一个attrs文件,并在其中定义视图的相关属性,如下:

& lt; resources>   & lt;才能declare-styleable  name=癈ustomView"比;   ,,,& lt; attr  name=皉ound_position"比;   ,,,,,& lt; flag  name=發eft-top" https://www.yisu.com/zixun/, value=" 0 x1 ">   <国旗name="右上角" value=" 0 x4 ">   <国旗name=" left-bottom " value=" 0 x2 ">   <国旗name=" right-bottom " value=" 0×8 ">   =      

其中round_position指的是圆角的位置,这里属性类型定为国旗(位或运算)这样就可以在布局中同时使用多个属性了,类似于EditText中定义文字样式:android: textStyle=按蟮▅ italic"; round_radius指圆角大小,类型为维度。

<>强自定义视图类

新建一个类继承视图,如下:

public  class  CustomView  extends  View  {   private 才能Paint  Paint =, new 油漆(Paint.ANTI_ALIAS_FLAG);   private 才能;Path 路径;   private 才能int  color =, Color.GREEN;   private 才能final  int  LEFT_TOP =, 0 x1;   private 才能final  int  LEFT_BOTTOM =, 0 x2;   private 才能final  int  RIGHT_TOP =, 0 x4;   private 才能final  int  RIGHT_BOTTOM =, 0×8;   private 才能;boolean  drawLeftTop;   private 才能;boolean  drawLeftBottom;   private 才能;boolean  drawRightTop;   private 才能;boolean  drawRightBottom;   private 才能float 半径;      public 才能CustomView (Context 上下文),{   ,,,超级(上下文);   ,,,initDraw ();   ,,}      public 才能;CustomView (Context 上下文,@Nullable  AttributeSet  attrs), {   ,,,超级(上下文,attrs);   ,,,TypedArray  TypedArray =, context.obtainStyledAttributes (R.styleable.CustomView attrs也);   ,,,int  position =, typedArray.getInt (R.styleable.CustomView_round_position, 0);   ,,,radius =, typedArray.getDimension (R.styleable.CustomView_round_radius, 0);   ,,,drawLeftTop =, (position ,, LEFT_TOP),==, LEFT_TOP;   ,,,drawLeftBottom =, (position ,, LEFT_BOTTOM),==, LEFT_BOTTOM;   ,,,drawRightTop =, (position ,, RIGHT_TOP),==, RIGHT_TOP;   ,,,drawRightBottom =, (position ,, RIGHT_BOTTOM),==, RIGHT_BOTTOM;   ,,,typedArray.recycle ();   ,,,initDraw ();   ,,}      public 才能;CustomView (Context 上下文,@Nullable  AttributeSet  attrs,, int  defStyleAttr), {   ,,,超级(上下文,attrs, defStyleAttr);   ,,,initDraw ();   ,,}      private 才能;void  initDraw (), {   ,,,path =, new 路径();   ,,,paint.setColor (Color.GREEN);   ,,,paint.setAntiAlias(真正的);   ,,,paint.setStrokeWidth((浮动),5);   ,,,paint.setStyle (Paint.Style.STROKE);   ,,}      @Override才能   protected 才能;void  onDraw (Canvas 画布),{   ,,,path.reset();//这里很重要,如果不写这一行,则每次重绘视图后先前绘制的还会存在   ,,,path.moveTo(半径,0);   ,,,if  (drawRightTop), {   ,,,,,path.lineTo (getWidth(),安康;半径,,0);//,,,,,,path.cubicTo(时间+ radius  getWidth (),,, 3,, 0,, radius  +, getWidth (),/, 3, *, 2,, 0,, getWidth(),安康;半径,,0);   ,,,,,path.cubicTo (getWidth(),安康;radius /, 2,, 0,, getWidth (),, radius /, 2, getWidth(),半径);   ,,,},{else    ,,,,,path.lineTo (getWidth (),, 0);//,,,,,,path.cubicTo(时间+ radius  getWidth (),,, 3,, 0,, radius  +, getWidth (),/, 3, *, 2,, 0,, getWidth (),, 0);   ,,,}   ,,,path.lineTo (getWidth(),获得(),安康;半径);   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

android如何自定义观点用路径画长方形