Android进阶-自定义view

源码方式

[java]  view plain  copy
  1. public class BanView extends View {  
  2.   
  3.   
  4.     Paint paint = new Paint();  
  5.   
  6.     public BanView(Context context) {  
  7.         super(context);  
  8.     }  
  9.   
  10.     public BanView(Context context, @Nullable AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.     }  
  13.   
  14.     public BanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  
  15.         super(context, attrs, defStyleAttr);  
  16.     }  
  17.   
  18.     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  
  19.     public BanView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {  
  20.         super(context, attrs, defStyleAttr, defStyleRes);  
  21.     }  
  22.   
  23.     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)  
  24.     @Override  
  25.     protected void onDraw(Canvas canvas) {  
  26.         super.onDraw(canvas);  
  27.         /** 
  28.          Canvas 类下的所有 draw- 打头的方法,例如 drawCircle() drawBitmap()。 
  29.          Paint 类的几个最常用的方法。具体是: 
  30.          Paint.setStyle(Style style) 设置绘制模式 
  31.          Paint.setColor(int color) 设置颜色 
  32.          Paint.setStrokeWidth(float width) 设置线条宽度 
  33.          Paint.setTextSize(float textSize) 设置文字大小 
  34.          Paint.setAntiAlias(boolean aa) 设置抗锯齿开关 
  35.          */  
  36.   
  37.         /*-------------------------------圆-------------------------------------------*/  
  38.   
  39.         // 基本简单使用 绘制一个圆  
  40.         canvas.drawCircle(300300200, paint);  
  41.   
  42.         //1、设置颜色  Paint.setColor(int color)  
  43.         //Paint.setColor(int color) 是 Paint 最常用的方法之一,用来设置绘制内容的颜色。  
  44.         // 你不止可以用它画红色的圆,也可以用它来画红色的矩形、红色的五角星、红色的文字。  
  45.         paint.setColor(getResources().getColor(R.color.colorAccent));  
  46.   
  47.         //2、设置样式  Paint.setStyle(Paint.Style style)  
  48.         //Style 具体来说有三种: FILL, STROKE 和  FILL_AND_STROKE 。  
  49.         // FILL 是填充模式  
  50.         // STROKE 是画线模式(即勾边模式)  
  51.         // FILL_AND_STROKE 是两种模式一并使用:既画线又填充。它的默认值是 FILL,填充模式。  
  52.         paint.setStyle(Paint.Style.STROKE);  
  53.   
  54.         //3、Paint.setStrokeWidth(float width)  
  55.         // 在 STROKE 和 FILL_AND_STROKE 下,还可以使用 paint.setStrokeWidth(float width) 来设置线条的宽度:  
  56.         paint.setStyle(Paint.Style.STROKE);  
  57.         paint.setStrokeWidth(20); // 线条宽度为 20 像素  
  58.         canvas.drawCircle(300300200, paint);  
  59.   
  60.         //4、抗锯齿  
  61.         // 在绘制的时候,往往需要开启抗锯齿来让图形和文字的边缘更加平滑。  
  62.         // 1)开启抗锯齿很简单,只要在  new Paint() 的时候加上一个 ANTI_ALIAS_FLAG 参数就行:  
  63.         // 2)也可以使用 Paint.setAntiAlias(boolean aa) 来动态开关抗锯齿。  
  64.         Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);  
  65.   
  66.         //drawOval(float left, float top, float right, float bottom, Paint paint) 画椭圆  
  67.         paint.setStyle(Paint.Style.STROKE);  
  68.         paint.setStrokeWidth(20); // 线条宽度为 20 像素  
  69.         canvas.drawOval(1008005001000, paint);  
  70.         /*-------------------------------圆-------------------------------------------*/  
  71.   
  72.   
  73.   
  74.         /*-------------------------------矩形-------------------------------------------*/  
  75.         //drawRect(float left, float top, float right, float bottom, Paint paint) 画矩形  
  76.   
  77.         paint.setStyle(Paint.Style.FILL);  
  78.         canvas.drawRect(100100500500, paint);  
  79.   
  80.         paint.setStyle(Paint.Style.STROKE);  
  81.         canvas.drawRect(7001001000500, paint);  
  82.   
  83.         //另外,它还有两个重载方法 drawRect(RectF rect, Paint paint) 和  drawRect(Rect rect, Paint paint)  
  84.         // ,让你可以直接填写 RectF 或 Rect 对象来绘制矩形。  
  85.   
  86.         //drawRoundRect(float left, float top, float right, float bottom,  
  87.         //float rx, float ry, Paint paint) 画圆角矩形  
  88.         canvas.drawRoundRect(100110050013005050, paint);  
  89.         /*-------------------------------矩形-------------------------------------------*/  
  90.   
  91.   
  92.   
  93.         /*-------------------------------点-------------------------------------------*/  
  94.         //drawPoint(float x, float y, Paint paint) 画点  
  95.   
  96.         Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);  
  97.         paint1.setStrokeWidth(20);  
  98.         paint1.setStrokeCap(Paint.Cap.SQUARE);  
  99.         canvas.drawPoint(5050, paint1);  
  100.   
  101.         //drawPoints(float[] pts, int offset, int count, Paint paint)  
  102.         //drawPoints(float[] pts, Paint paint) 画点(批量)  
  103.   
  104.         float[] points = {005050501001005010010015050150100};  
  105.         //绘制四个点:(50, 50) (50, 100) (100, 50) (100, 100)  
  106.         canvas.drawPoints(points, 2 /* 跳过两个数,即前两个 0 */,  
  107.                 8 /* 一共绘制 8 个数(4 个点)*/, paint1);  
  108.         /*-------------------------------点-------------------------------------------*/  
  109.   
  110.   
  111.   
  112.         /*-------------------------------线-------------------------------------------*/  
  113.         //drawLine(float startX, float startY, float stopX, float stopY, Paint paint) 画线  
  114.         canvas.drawLine(400600800800, paint);  
  115.   
  116.         //drawLines(float[] pts, int offset, int count, Paint paint)  
  117.         //drawLines(float[] pts, Paint paint) 画线(批量)  
  118.   
  119.         float[] pts = {505040050,  
  120.                 40050400600,  
  121.                 40060050600,  
  122.                 606005050};//数据  
  123.         paint1.setStrokeWidth((float5.0);//线宽  
  124.         canvas.drawLines(pts, paint1);  
  125.         /*-------------------------------线-------------------------------------------*/  
  126.   
  127.   
  128.         /*-------------------------------特殊-------------------------------------------*/  
  129.         //drawArc(float left, float top, float right, float bottom,  
  130.         // float startAngle, float sweepAngle, boolean useCenter, Paint paint) 绘制弧形或扇形  
  131.         paint.setStyle(Paint.Style.FILL); // 填充模式  
  132.         // 绘制扇形  
  133.         canvas.drawArc(20014008001600, -110100true, paint);  
  134.         // 绘制弧形  
  135.         canvas.drawArc(2001400800160020140false, paint);  
  136.   
  137.         paint.setStyle(Paint.Style.STROKE); // 画线模式  
  138.         // 绘制不封口的弧形  
  139.         canvas.drawArc(2001400800160018060false, paint);  
  140.         /*-------------------------------特殊-------------------------------------------*/  
  141.     }  
  142. }  

布局

[java]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical">  
  8.   
  9.     <com.templates.ban.diy.BanView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="0dp"  
  12.         android:layout_weight="1"  
  13.     />  
  14. </LinearLayout>  

效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值