自定义一个时钟的显示效果
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                自定义一个时钟的显示效果
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                
 直接看圖上代碼
 
 
 
定義一個View
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View;import com.zhong.circleimageview.R;/*** Created by zhongrc on 2016/9/19. 19:47* Email:zhongrc@skyth-tek.com*/public class CircleImageView extends View {private float mBorderWidth;//線條的寬度private int mBorderColor;//線條的顏色private RectF mBounds;//矩形區域private Paint mPaint;// 畫筆private float width;private float height;float radius;//半徑float smallLength;float largeLength;private int timeHours=100;private int timeMin=200;private int timeSecond=250;public CircleImageView(Context context) {super(context);init(context, null, 0);}public CircleImageView(Context context, AttributeSet attrs) {super(context, attrs);//獲取布局定義的屬性TypedArray typedArray =context.getTheme().obtainStyledAttributes(attrs,R.styleable.circleView,0, 0);try {//獲取線頭的顏色mBorderColor = typedArray.getColor(R.styleable.circleView_border_color, 0xff000000);//獲取線條的寬度mBorderWidth = typedArray.getDimension(R.styleable.circleView_border_width, 2);} finally {//回收typedArray.recycle();}init(context, attrs, 0);}public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs, defStyleAttr);}/*** 初始化** @param context* @param attrs* @param defStyleAttr*/private void init(Context context, AttributeSet attrs, int defStyleAttr) {//初始化畫筆mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(mBorderWidth);mPaint.setColor(mBorderColor);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);//根據當前的布局大小確定矩形區域mBounds = new RectF(getLeft(), getTop(), getRight(), getBottom());width = mBounds.right - mBounds.left; //矩形的寬height = mBounds.bottom - mBounds.top;//矩形的高if (width < height) {radius = width / 4;} else {radius = height / 4;}smallLength = 10;largeLength = 20;}public void setTime(int h,int m,int s){this.timeHours=h;this.timeMin=m;this.timeSecond=s;invalidate();}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(0xffffffff);mPaint.setColor(0x66555555);canvas.drawRoundRect( //繪制圓角矩形new RectF(mBounds.centerX() - (float) 0.9 * width / 2,mBounds.centerY() - (float) 0.9 * height / 2,mBounds.centerX() + (float) 0.9 * width / 2,mBounds.centerY() + (float) 0.9 * height / 2),30,30,mPaint);mPaint.setColor(mBorderColor);canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), radius, mPaint);//畫圓float start_x, start_y;float end_x, end_y;for (int i = 0; i < 60; ++i) {start_x = radius * (float) Math.cos(Math.PI / 180 * i * 6);start_y = radius * (float) Math.sin(Math.PI / 180 * i * 6);if (i % 5 == 0) {mPaint.setColor(0xFFff0000);end_x = start_x + largeLength * (float) Math.cos(Math.PI / 180 * i * 6);end_y = start_y + largeLength * (float) Math.sin(Math.PI / 180 * i * 6);} else {mPaint.setColor(mBorderColor);end_x = start_x + smallLength * (float) Math.cos(Math.PI / 180 * i * 6);end_y = start_y + smallLength * (float) Math.sin(Math.PI / 180 * i * 6);}start_x += mBounds.centerX();end_x += mBounds.centerX();start_y += mBounds.centerY();end_y += mBounds.centerY();canvas.drawLine(start_x, start_y, end_x, end_y, mPaint);}canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), 10, mPaint);//畫時鐘mPaint.setColor(0xFF00F055);mPaint.setStrokeWidth(6);canvas.rotate(timeHours, mBounds.centerX(), mBounds.centerY());canvas.drawLine(mBounds.centerX(), mBounds.centerY(), mBounds.centerX(), mBounds.centerY() - radius *2/ 3, mPaint);//畫分鐘mPaint.setColor(0xFF00F055);mPaint.setStrokeWidth(4);canvas.rotate(timeMin-timeHours, mBounds.centerX(), mBounds.centerY());canvas.drawLine(mBounds.centerX(), mBounds.centerY(), mBounds.centerX(), mBounds.centerY() - radius * 4 / 5, mPaint);//畫秒鐘mPaint.setColor(0xFF00F055);mPaint.setStrokeWidth(2);canvas.rotate(timeSecond-timeHours-timeMin, mBounds.centerX(), mBounds.centerY());canvas.drawLine(mBounds.centerX(), mBounds.centerY(), mBounds.centerX(), mBounds.centerY() - radius, mPaint);mPaint.setStyle(Paint.Style.FILL);canvas.drawCircle(mBounds.centerX(), mBounds.centerY(), 8, mPaint);mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setStyle(Paint.Style.STROKE);mPaint.setStrokeWidth(mBorderWidth);mPaint.setColor(mBorderColor);}}
 
定義屬性
 
xml布局
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.zhong.circleimageview.MainActivity"><com.zhong.view.CircleImageViewandroid:id="@+id/civ"android:layout_width="match_parent"android:layout_height="300dp"app:border_color="#25175e"app:border_width="2dp"></com.zhong.view.CircleImageView></android.support.constraint.ConstraintLayout>MainActivity.java
 
 
總結
以上是生活随笔為你收集整理的自定义一个时钟的显示效果的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 递归算法《M个苹果放入N个盘子》
 - 下一篇: 公益中国系列活动进社区之 “健康进社区