Android-ClockView
来源:互联网 发布:中文翻译拼音软件下载 编辑:程序博客网 时间:2024/06/07 13:29
public class ClockView extends View implements Runnable{private Drawable mDrawable;private int mWidth;private float mCenterX;private float mHourLength;private float mMinuteLength;private float mSecondLength;private Paint mPaint;private Handler handler = new Handler();public ClockView(Context context) {this(context, null);}public ClockView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ClockView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init(context, attrs);}private void init(final Context context, final AttributeSet attrs){final TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ClockView);mDrawable = array.getDrawable(R.styleable.ClockView_src);array.recycle();mPaint = new Paint();mPaint.setAntiAlias(true);mPaint.setColor(getResources().getColor(R.color.black));handler.postDelayed(this, 1000);}@Overridepublic void run() {// 重新绘制Viewinvalidate();// 重新设置定时器,在60秒后调用run方法handler.postDelayed(this, 1000);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = getMeasuredWidth(); int height = getMeasuredHeight();mWidth = Math.min(width, height);setMeasuredDimension(mWidth, mWidth);LogUtil.d("mWidth:"+mWidth);mHourLength = mWidth/6;mMinuteLength = mWidth/5;mSecondLength = mWidth/4;mCenterX = mWidth/2;}@Override protected void onDraw(Canvas canvas) {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)); // 设置bounds,相当于缩放图片了mDrawable.setBounds(0, 0, mWidth, mWidth);// 1. 先把图绘制上mDrawable.draw(canvas); // 2. 表盘中心点画一个半径为5的实心圆圈 canvas.drawCircle(mWidth/2, mWidth/2, 5, mPaint);// 3.// 设置实针为4个象素粗mPaint.setStrokeWidth(4);// 在表盘上画时针canvas.drawLine(mCenterX, mCenterX,(int) (mCenterX + mHourLength * Math.cos(hourRadian)),(int) (mCenterX - mHourLength * Math.sin(hourRadian)), mPaint);// 设置分针为3个象素粗mPaint.setStrokeWidth(3);// 在表盘上画分针canvas.drawLine(mCenterX, mCenterX, (int) (mCenterX + mMinuteLength* Math.cos(minuteRadian)), (int) (mCenterX - mMinuteLength* Math.sin(minuteRadian)), mPaint);// 设置分针为2个象素粗mPaint.setStrokeWidth(2);// 在表盘上画秒针canvas.drawLine(mCenterX, mCenterX, (int) (mCenterX + mSecondLength* Math.cos(secondRadian)), (int) (mCenterX - mSecondLength* Math.sin(secondRadian)), mPaint);}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();// 删除回调类handler.removeCallbacks(this);}}
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="ClockView"> <attr name="src" format="reference" /> </declare-styleable></resources>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="fill_parent"android:layout_height="fill_parent" android:orientation="vertical"><com.xjs.view.ClockViewandroid:layout_width="200dp" android:layout_height="200dp"android:layout_gravity="center_horizontal"app:src="@drawable/clock3"/></LinearLayout>
最终的实现效果:
very beautiful!
1 0
- Android-ClockView
- android自定义ClockView
- android自定义ClockView
- Android自定义View—ClockView
- Android 自定义View(四) 时钟clockView
- Android自定义View实现时钟效果ClockView
- Android自定义ClockView实现时钟效果
- Android自定义控件之ClockView时钟效果
- ClockView——简单的Android时钟控件
- ClockView 时钟
- ClockView时钟类
- SurfaceView打造自定义时钟ClockView
- 安卓开发 自定义控件 奥米噶 ClockView,打造自己的奥米噶送给你的女神
- Android
- android
- Android
- android
- android
- 大神们的博客
- ListView上拉加载更多数据
- 杭电ACM1003
- js字符串常用方法
- 问题error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. 的解决
- Android-ClockView
- git常用命令汇总
- iOS URLEcode,URLDecode
- freemarker+spring中文乱码的解决
- 【df】显示磁盘分区使用情况
- Tyvj_P1002
- 树状数组&二维树状数组模板
- 【dumpe2fs】显示文件系统属性信息(更加详细)
- Linux特殊权限:SUID、SGID、SBIT