Android 自定义图形实例

  1. public class CV1 extends View {  
  2.     private Paint paint;  
  3.     private int rect_width = 30;  
  4.     private int rect_space = 20;  
  5.     private int[][] rect_array = {{Color.RED, 300},  
  6.             {Color.GREEN, 400}, {Color.BLUE, 250}, {Color.YELLOW, 500},  
  7.             {Color.DKGRAY, 600}};  
  8.   
  9.     public CV1(Context context) {  
  10.         super(context);  
  11.     }  
  12.   
  13.     public CV1(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         init();  
  16.     }  
  17.   
  18.     public void init() {  
  19.         paint = new Paint();  
  20.         paint.setColor(Color.BLACK);  
  21.         paint.setAntiAlias(true);  
  22.         paint.setStrokeWidth(3);  
  23.     }  
  24.   
  25.     @Override  
  26.     protected void onDraw(Canvas canvas) {  
  27.         super.onDraw(canvas);  
  28.         paint.setTextSize(15);  
  29.         canvas.drawText("300"10, getHeight() - 300 - 50, paint);  
  30.         canvas.drawCircle(50, getHeight() - 3505, paint);  
  31.         paint.setStyle(Paint.Style.FILL_AND_STROKE);  
  32.         for (int i = 0; i < rect_array.length; i++) {  
  33.             int left = 50 + (rect_space + rect_width) * i + rect_space;  
  34.             int top = getHeight() - 50 - rect_array[i][1];  
  35.             paint.setColor(rect_array[i][0]);  
  36.             canvas.drawRect(left, top, left + rect_width, getHeight() - 50, paint);  
  37.         }  
  38.         //绘制虚线  :DashPathEffect虚线的对象  
  39.         /** 
  40.          * float[]: 
  41.          * 代码当中的数组要求必须是偶数数组,而且必须>=2,指定了多少长度的实线之后在画多少长度的空白, 
  42.          * 举例:如下代码绘制长度为1的实线,在绘制长度为2的空白,绘制长度为3的实线,在绘制长度为4的空白。 
  43.          * float: 代表其实位置的偏移量 
  44.          * */  
  45.         //绘制x轴:  
  46. //        Path path = new Path();  
  47. //        path.moveTo(50, getHeight() - 50);  
  48. //        path.lineTo(getWidth() - 50, getHeight() - 50);  
  49. //        PathEffect effects = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);  
  50. //        paint.setPathEffect(effects);  
  51. //        canvas.drawPath(path, paint);  
  52.   
  53.          canvas.drawLine(50, getHeight() - 50, getWidth() - 50, getHeight() - 50, paint);  
  54.         //绘制x轴的箭头  
  55.         canvas.drawLine(getWidth() - 50, getHeight() - 50, getWidth() - 75, getHeight() - 75, paint);  
  56.         canvas.drawLine(getWidth() - 50, getHeight() - 50, getWidth() - 75, getHeight() - 25, paint);  
  57.         //绘制y轴  
  58.   
  59.         canvas.drawLine(50, getHeight() - 505050, paint);  
  60.         //绘制y轴的箭头  
  61.         canvas.drawLine(50502575, paint);  
  62.         canvas.drawLine(50507575, paint);  
  63.   
  64.         //绘制x轴y轴焦点  
  65.         canvas.drawCircle(50, getHeight() - 502, paint);  
  66.   
  67.   
  68.     }  
  69.   
  70.     @Override  
  71.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  72.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  73.     }  
  74. }  

[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <com.example.thinkpad.customview.CustomView1.CV1  
  2.        android:layout_width="match_parent"  
  3.        android:layout_height="match_parent"  
  4.        android:id="@+id/rect"/>  

效果:



[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class CV2 extends View {  
  2.     private Paint paint;  
  3.     private int hours, minutes, seconds;  
  4.     private int defaultLength = 200;  
  5.   
  6.     Handler handler = new Handler() {  
  7.         @Override  
  8.         public void handleMessage(Message msg) {  
  9.             super.handleMessage(msg);  
  10.        //     getTime();  
  11.             invalidate();  
  12.             handler.sendEmptyMessageDelayed(11000);  
  13.         }  
  14.     };  
  15.   
  16.     private void initPaint() {  
  17.         paint = new Paint();  
  18.         paint.setColor(Color.BLACK);  
  19.         paint.setAntiAlias(true);  
  20.     }  
  21.   
  22.     public CV2(Context context) {  
  23.         super(context);  
  24.     }  
  25.   
  26.     public CV2(Context context, AttributeSet attrs) {  
  27.         super(context, attrs);  
  28.         initPaint();  
  29.         getTime();  
  30.         handler.sendEmptyMessageDelayed(11000);  
  31.     }  
  32.   
  33.     public void getTime() {  
  34.         Calendar calendar = Calendar.getInstance();  
  35.         hours = calendar.get(Calendar.HOUR);  
  36.         minutes = calendar.get(Calendar.MINUTE);  
  37.         seconds = calendar.get(Calendar.SECOND);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void draw(Canvas canvas) {  
  42.         super.draw(canvas);  
  43.         //绘制钟表  
  44.         //绘制圆心  
  45.         canvas.drawCircle(getWidth() / 2, getHeight() / 28, paint);  
  46.         //绘制外围表盘  
  47.         paint.setStrokeWidth(5);  
  48.         paint.setStyle(Paint.Style.STROKE);  
  49.         canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 10, paint);  
  50.         //绘制内表盘  
  51.         paint.setStrokeWidth(3);  
  52.         canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2 - 22, paint);  
  53.         //绘制刻度  
  54.         //绘制钟表上的刻度  
  55.         paint.setColor(Color.GRAY);  
  56.         for (int i = 0; i < 12; i++) {  
  57.             //保存画布旋转之前的状态  
  58.             canvas.save();  
  59.             canvas.rotate(30 * hours + minutes * 0.5f, getWidth() / 2, getHeight() / 2);  
  60.             canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2 - getHeight() / 5, paint);  
  61.             canvas.restore();  
  62.         }  
  63.         //绘制时分秒针  
  64.         //绘制时针  
  65.         canvas.save();  
  66.         canvas.rotate(30 * hours + minutes * 0.5f, getWidth() / 2, getHeight() / 2);  
  67.         paint.setStrokeWidth(6);  
  68.         canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2 - getHeight() / 5, paint);  
  69.         canvas.restore();  
  70.   
  71.         //绘制分针  
  72.         canvas.save();  
  73.         canvas.rotate((360 / 60) * minutes, getWidth() / 2, getHeight() / 2);  
  74.         paint.setStrokeWidth(3);  
  75.         canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2 - getHeight() / 4, paint);  
  76.         canvas.restore();  
  77.   
  78.         //绘制秒针  
  79.         canvas.save();  
  80.         canvas.rotate(6 * seconds, getWidth() / 2, getHeight() / 2);  
  81.         paint.setStrokeWidth(1);  
  82.         canvas.drawLine(getWidth() / 2, getHeight() / 2, getWidth() / 2, getHeight() / 2 - getHeight() / 3, paint);  
  83.         canvas.restore();  
  84.     }  
  85.   
  86.     @Override  
  87.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  88.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  89.   
  90.         int wMode = MeasureSpec.getMode(widthMeasureSpec);  
  91.         int hMode = MeasureSpec.getMode(heightMeasureSpec);  
  92.         //获取到可以设置的最大的尺寸  
  93.         int wSize = MeasureSpec.getSize(widthMeasureSpec);  
  94.         int hSize = MeasureSpec.getSize(heightMeasureSpec);  
  95.   
  96.         switch (wMode) {  
  97.             case MeasureSpec.EXACTLY:  
  98.                 wSize = hSize = Math.min(wSize, hSize);  
  99.                 break;  
  100.             case MeasureSpec.AT_MOST:   //宽:wrap_content  高:100    谁小按照谁画  
  101.                 if (hMode == MeasureSpec.AT_MOST || hSize > defaultLength) {  
  102.                     wSize = hSize = defaultLength;  
  103.                 } else {  
  104.                     wSize = hSize;    //高比宽小,按照小的画  
  105.                 }  
  106.                 break;  
  107.         }  
  108.         setMeasuredDimension(wSize, hSize);  
  109.     }  
  110. }  
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <com.example.thinkpad.customview.CustomView2.CV2  
  2.       android:layout_width="match_parent"  
  3.       android:layout_height="match_parent" />  
效果:


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. public class CV4 extends View {  
  2.     private Paint paint;  
  3.     private int sweepAngle = 0;   //扫描的起始角度  
  4.     private int sweepStep = 5;   //每次扫描的角度  
  5.     private int defalut_width = 100;  
  6.     private int default_height = 100;  
  7.   
  8.     private int circleColor = Color.GRAY;  
  9.     private int sweepColor = Color.GREEN;  
  10.     private int padding = 5;  
  11.     private int startAngle = -90;  
  12.     private void initPaint(){  
  13.         paint = new Paint();  
  14.         paint.setAntiAlias(true);  
  15.         paint.setColor(circleColor);  
  16.     }  
  17.     public CV4(Context context) {  
  18.         super(context);  
  19.     }  
  20.   
  21.     public CV4(Context context, AttributeSet attrs) {  
  22.         super(context, attrs);  
  23.         initPaint();  
  24.     }  
  25.   
  26.     @Override  
  27.     protected void onDraw(Canvas canvas) {  
  28.         super.onDraw(canvas);  
  29.         paint.setColor(circleColor);  
  30.         canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2-padding,paint);  
  31.   
  32.         paint.setColor(sweepColor);  
  33.         /** 
  34.          * 绘制扇形: 
  35.          * 1.rectf:指代扇形的容器,矩形对象, 
  36.          * 2,startAngle:起始的角度 
  37.          * 3,sweepAngle 扫描的角度 
  38.          * 4:userCenter  : 
  39.          * */  
  40.         canvas.drawArc(new RectF(padding,padding,getWidth()  
  41.                         -padding,getHeight()-padding),  
  42.                 startAngle,sweepAngle,true,paint);  
  43.   
  44.         sweepAngle =sweepAngle+sweepStep;  
  45.         sweepAngle = sweepAngle>360?0:sweepAngle;  
  46.   
  47.         //重新绘制图形  
  48.         invalidate();  
  49.     }  
  50.   
  51.     @Override  
  52.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  53.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  54.   
  55.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);  
  56.         int heightMode = MeasureSpec.getMode(heightMeasureSpec);  
  57.   
  58.         int wSize = MeasureSpec.getSize(widthMeasureSpec);  
  59.         int hSize = MeasureSpec.getSize(heightMeasureSpec);  
  60.   
  61.         switch (widthMode) {  
  62.             case MeasureSpec.AT_MOST:  
  63.                 wSize = defalut_width;  
  64.                 hSize = default_height;  
  65.                 break;  
  66.             case MeasureSpec.EXACTLY:  
  67.                 wSize = hSize = Math.min(wSize,hSize);  
  68.                 break;  
  69.         }  
  70.   
  71.         setMeasuredDimension(wSize,hSize);  
  72.     }  
  73. }  
[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <com.example.thinkpad.customview.CustomView4.CV4  
  2.        android:layout_width="200dp"  
  3.        android:layout_height="200dp" />  

效果:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值