android canvas 画闹钟 圆弧

package com.example.hui;

import java.util.ArrayList;


import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.view.View;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);
		setContentView(new CustomView(this));
	}

	
	 class CustomView extends View{   
         
	        Paint paint;   
	        //private ArrayList<PointF> graphics = new ArrayList<PointF>();   
	       // PointF point;   
	                                                                                                                                  
	        public CustomView(Context context) {   
	            super(context);   
	            paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔   
	            paint.setColor(Color.RED);   
	            paint.setStrokeJoin(Paint.Join.ROUND);   
	            paint.setStrokeCap(Paint.Cap.ROUND);   
	            paint.setStrokeWidth(3);   
	                                                                                                                                  
	        }   
                                                                                                                        
	        //在这里我们将测试canvas提供的绘制图形方法   
	        @Override   
	        protected void onDraw(Canvas canvas) {   
	        	//绘制黑色背景
	        	Paint b = new Paint();
	        	b.setColor(Color.BLACK);
	        	RectF rect = new RectF(0, 0, 900, 900);             
	            canvas.drawRect(rect, b);   
	        	
	        	//画大圆圈
	        	paint.setAntiAlias(true);   
	            paint.setStyle(Style.STROKE);   
	            canvas.translate(canvas.getWidth()/2, 200); //将位置移动画纸的坐标点:150,150   
	            canvas.drawCircle(0, 0, 100, paint); //画圆圈   
	                                                                                                                                          
	            //使用path绘制路径文字   
	            canvas.save();   
	            canvas.translate(-75, -75);   
	            Path path = new Path();   
	            path.addArc(new RectF(0,0,150,150), -180, 180);   
	            Paint citePaint = new Paint(paint);   
	            citePaint.setTextSize(14);   
	            citePaint.setStrokeWidth(1);    //设置画笔的宽度
	            canvas.drawTextOnPath("ninngsnhenngcnai@2015080558s", path, 28, 0, citePaint);   
	            canvas.restore();   
	                      
	            //画刻度
	            Paint tmpPaint = new Paint(paint); //小刻度画笔对象   
	            tmpPaint.setStrokeWidth(1);    //设置画笔的宽度
	                                                                                                                                          
	            float  y=100;   
	            int count = 60; //总刻度数   
	                                                                                                                                          
	            for(int i=0 ; i <count ; i++){   
	                if(i%5 == 0){   
	                    canvas.drawLine(0f, y, 0, y+12f, paint);   
	                    canvas.drawText(String.valueOf(i/5+1), -4f, y+25f, tmpPaint);   
	                                                                                                                                          
	                }else{   
	                    canvas.drawLine(0f, y, 0f, y +5f, tmpPaint);   
	                }   
	                canvas.rotate(360/count,0f,0f); //旋转画纸   
	            }   
	                                                                                                                                          
	            //绘制指针   
	            tmpPaint.setColor(Color.GRAY);   //设置画笔颜色
	            tmpPaint.setStrokeWidth(4);   //设置画笔的宽度
	            canvas.drawCircle(0, 0, 7, tmpPaint);    //画圆
	            tmpPaint.setStyle(Style.FILL);   
	            tmpPaint.setColor(Color.YELLOW);   
	            canvas.drawCircle(0, 0, 5, tmpPaint);   
	            canvas.drawLine(0, 10, 0, -65, paint);   
	            
	            //画圆弧
	           // canvas.drawCircle(0, 0, 200, paint); //画圆圈   
	            RectF oval=new RectF();   
	            oval.set(80, 80, 200, 200);
	            canvas.drawArc(oval, 100, 100, true, paint);//true 连接半径线, false 不连接
	                
	            
//	              //画线
//	             canvas.drawLines(new float[]{
//				            		   10,10, //   线的起始坐标
//					                   20,20, //  线的终点坐标
//					                   
//					                   30,30, //....   
//					                   40,40,   
//					                   
//					                   50,50,   
//					                   60,60,   
//					                   
//					                   70,70,   
//					                   80,80,   
//					                   
//					                   120,120,   
//					                   150,150  
//	             }, tmpPaint);
	           
	          super.onDraw(canvas);   
	                                                                                                                                  
	        }   
	        
	 }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值