实例介绍
【实例截图】
【核心代码】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | package com.example.onclick; import java.util.Calendar; 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.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; public class MainActivity extends Activity { private Paint mPaint; private float mCenterX; private float mHourLength; private float mMinuteLength; private float mSecondLength; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView( new CustomView1(MainActivity. this )); } class CustomView1 extends View implements Runnable{ Paint paint; public CustomView1(Context context) { super (context); paint = new Paint(); //设置一个笔刷大小是3的黄色的画笔 paint.setColor(Color.RED); handler.postDelayed( this , 1000 ); } //在这里我们将测试canvas提供的绘制图形方法 @Override protected void onDraw(Canvas canvas) { paint.setStrokeWidth( 5 ); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias( true ); paint.setStyle(Paint.Style.STROKE); //将要画的位置移动到屏幕中间 canvas.translate(canvas.getWidth()/ 2 , canvas.getHeight()/ 2 ); //将位置移动画纸的坐标点:150,150 //以半径为150和180画圆 canvas.drawCircle( 0 , 0 , 150 , paint); canvas.drawCircle( 0 , 0 , 180 , paint); //使用path绘制路径文字 canvas.save(); //移动绘制文字的位置 canvas.translate( 0 , 0 ); Path path = new Path(); //绘制的时候要注意左上不能大于右下,否则不会显示 RectF rect = new RectF(- 100 ,- 100 , 100 , 100 ); path.addArc(rect, - 220 , 280 ); Paint citePaint = new Paint(paint); citePaint.setTextSize( 28 ); //设置画笔的粗细 citePaint.setStrokeWidth( 3 ); //float hOffset, float vOffset// 设置水平位置 vOffset 设置垂直位置 // 如果hOffset为0 说明开始位置在path.addArc设置的startAngle开始角度 // 如果vOffset 为0说明经过的位置是在与RectF的顶部相切处 // canvas.drawTextOnPath("http://blog.csdn.net/u014452224", path, 14, 0, citePaint); //为了方便一些转换操作,Canvas 还提供了保存和回滚属性的方法(save和restore), // 比如你可以先保存目前画纸的位置(save), // 然后旋转90度,向下移动100像素后画一些图形,画完后调用restore方法返回到刚才保存的位置 canvas.restore(); Paint smallPaint = new Paint(paint); //非数字刻度画笔对象 smallPaint.setStrokeWidth( 2 ); smallPaint.setColor(Color.GRAY); float y= 150 ; int count = 60 ; //总刻度数 Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setStrokeWidth( 6 ); paint.setColor(Color.RED); paint.setTextSize( 24 ); paint.setStrokeWidth( 3 ); for ( int i= 0 ; i <count ; i ){ if (i% 5 == 0 ){ //绘制数字刻度 canvas.drawText(i == 0 ? "12" : String.valueOf(i / 5 ),((i / 5 )> 9 ||i== 0 )?-15f:-6f , -y-5f, paint); } else { //绘制非数字的刻度 canvas.drawLine(0f, y, 0f, y 15f, smallPaint); } canvas.rotate( 360 /count,0f,0f); //旋转画纸 } Calendar calendar = Calendar.getInstance(); int currentMinute = calendar.get(Calendar.MINUTE); int currentHour = calendar.get(Calendar.HOUR); int currentSecond = calendar.get(Calendar.SECOND); // 计算分针和时间的角度 double secondRadian = Math.toRadians(( 360 - ((currentSecond * 6 ) - 90 )) % 360 ); double minuteRadian = Math.toRadians(( 360 - ((currentMinute * 6 ) - 90 )) % 360 ); double hourRadian = Math.toRadians(( 360 - ((currentHour * 30 ) - 90 ))% 360 - ( 30 * currentMinute / 60 )); // 设置实针为6个象素粗 paint.setStrokeWidth( 6 ); // 在表盘上画时针 mCenterX = 0 ; mHourLength = 100 ; canvas.drawLine(mCenterX, mCenterX, ( int ) (mCenterX mHourLength * Math.cos(hourRadian)), ( int ) (mCenterX - mHourLength * Math.sin(hourRadian)), paint); // 设置分针为4个象素粗 paint.setStrokeWidth( 4 ); mMinuteLength = 120 ; // 在表盘上画分针 canvas.drawLine(mCenterX, mCenterX, ( int ) (mCenterX mMinuteLength* Math.cos(minuteRadian)), ( int ) (mCenterX - mMinuteLength* Math.sin(minuteRadian)), paint); // 设置分针为2个象素粗 paint.setStrokeWidth( 2 ); // 在表盘上画秒针 mSecondLength = 145 ; int centerY = 30 ; canvas.drawLine(( int ) (mCenterX - centerY* Math.cos(secondRadian)),( int ) (mCenterX centerY* Math.sin(secondRadian)), ( int ) (mCenterX mSecondLength* Math.cos(secondRadian)), ( int ) (mCenterX - mSecondLength* Math.sin(secondRadian)), paint); paint.setStyle(Paint.Style.FILL); canvas.drawCircle( 0 , 0 , 5 , paint); } @Override public void run() { // 重新绘制View this .invalidate(); // 重新设置定时器,在60秒后调用run方法 handler.postDelayed( this , 1000 ); } } } |
标签: 时钟
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论