欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View實(shí)現(xiàn)時(shí)鐘效果

 更新時(shí)間:2022年01月12日 08:00:54   作者:仙氣兒飄飄w  
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android自定義View實(shí)現(xiàn)時(shí)鐘效果的具體代碼,供大家參考,具體內(nèi)容如下

自定義時(shí)鐘

初學(xué)自定義View,先畫(huà)一個(gè)不太成熟的時(shí)鐘(甚至只有秒針)

時(shí)鐘效果

@SuppressLint("DrawAllocation")
public class ClockView extends View {

? ? private final Context mContext;
? ? private Canvas mCanvas;// 畫(huà)布
? ? private Paint clockPaint;// 表盤(pán)畫(huà)筆
? ? private Paint textPaint;// 文字畫(huà)筆
? ? private Paint secondPaint;// 秒針畫(huà)筆
? ? private int x,y;// 表中心坐標(biāo)
? ? private Thread refreshThread;
? ? private boolean isStop = false;
? ? // 用于獲取當(dāng)前秒數(shù)
? ? private ?Date currentDate;
? ? private SimpleDateFormat sp = new SimpleDateFormat("ss");

? ? @SuppressLint("HandlerLeak")
? ? private final Handler mHandler = new Handler(){
? ? ? ? @Override
? ? ? ? public void handleMessage(@NonNull Message msg) {
? ? ? ? ? ? super.handleMessage(msg);
? ? ? ? ? ? if(msg.what == 0){
? ? ? ? ? ? ? ? invalidate();//每隔一秒刷新一次
? ? ? ? ? ? }
? ? ? ? }
? ? };

? ? public ClockView(Context context) {
? ? ? ? this(context,null);
? ? }

? ? public ClockView(Context context, @Nullable AttributeSet attrs) {
? ? ? ? this(context,null,0);
? ? }

? ? public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
? ? ? ? this(context,null,0,0);
? ? }

? ? public ClockView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
? ? ? ? super(context, attrs, defStyleAttr, defStyleRes);
? ? ? ? this.mContext = context;
? ? }


? ? @Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
? ? ? ? int widthMeasure;
? ? ? ? // 設(shè)置寬高一致
? ? ? ? if(widthMeasureSpec > heightMeasureSpec){
? ? ? ? ? ? super.onMeasure(heightMeasureSpec, heightMeasureSpec);
? ? ? ? ? ? widthMeasure ?= getMeasuredHeight();
? ? ? ? }else{
? ? ? ? ? ? super.onMeasure(widthMeasureSpec, widthMeasureSpec);
? ? ? ? ? ? widthMeasure = getMeasuredWidth();
? ? ? ? }
? ? ? ? initPaint(widthMeasure);
? ? }

? ? private void initPaint(int width){
? ? ? ? // 表盤(pán)畫(huà)筆
? ? ? ? clockPaint = new Paint();
? ? ? ? clockPaint.setColor(mContext.getColor(R.color.black));// 顏色
? ? ? ? clockPaint.setAntiAlias(true);// 抗鋸齒
? ? ? ? clockPaint.setStyle(Paint.Style.STROKE);// 樣式
? ? ? ? clockPaint.setStrokeWidth(width/80f);// 寬度

? ? ? ? //字體畫(huà)筆
? ? ? ? textPaint = new Paint();
? ? ? ? textPaint.setColor(mContext.getColor(R.color.black));
? ? ? ? textPaint.setAntiAlias(true);
? ? ? ? textPaint.setStyle(Paint.Style.FILL);
? ? ? ? textPaint.setTextSize(width/16f);
? ? ? ? textPaint.setTextAlign(Paint.Align.CENTER);

? ? ? ? // 秒針畫(huà)筆
? ? ? ? secondPaint = new Paint();
? ? ? ? secondPaint.setColor(mContext.getColor(R.color.red));
? ? ? ? secondPaint.setAntiAlias(true);
? ? ? ? secondPaint.setStyle(Paint.Style.FILL);
? ? ? ? secondPaint.setStrokeWidth(5f);
? ? }

? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);

? ? ? ? mCanvas = canvas;

? ? ? ? // 獲取畫(huà)布中心坐標(biāo)
? ? ? ? x = getWidth() / 2;
? ? ? ? y = getHeight() / 2;

? ? ? ? // 繪制表盤(pán)
? ? ? ? mCanvas.drawCircle(x, y,getWidth()/40f, clockPaint);// 表盤(pán)中心
? ? ? ? mCanvas.drawCircle(x, y, x -getWidth()/40f, clockPaint);// 表盤(pán)邊框

? ? ? ? // 繪制刻度
? ? ? ? for(int i = 1;i <= 60;i++){
? ? ? ? ? ? mCanvas.rotate(6, x, y);
? ? ? ? ? ? if(i%5 == 0){
? ? ? ? ? ? ? ? // 繪制大刻度
? ? ? ? ? ? ? ? mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()*5/80f, clockPaint);
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? // 繪制小刻度
? ? ? ? ? ? ? ? mCanvas.drawLine(x, getWidth()*3/80f, x, getWidth()/20f, clockPaint);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? // 繪制 1-12小時(shí) 字體
? ? ? ? for(int i = 1;i <= 60;i++){
? ? ? ? ? ? if(i%5 == 0){
? ? ? ? ? ? ? ? float x1 = (float) Math.sin(Math.toRadians(6 * i)) * (y * 3 / 4f) + x;
? ? ? ? ? ? ? ? float y1 = y - (float) Math.cos(Math.toRadians(6 * i)) * (y * 3 / 4f) + getWidth()/40f;
? ? ? ? ? ? ? ? mCanvas.drawText(String.valueOf(i/5), x1, y1, textPaint);
? ? ? ? ? ? }
? ? ? ? }

? ? ? ? // 繪制秒針
? ? ? ? currentDate = new Date(System.currentTimeMillis());
? ? ? ? int ss = Integer.parseInt(sp.format(currentDate));// 獲取當(dāng)前秒數(shù)
? ? ? ? // 根據(jù)當(dāng)前秒數(shù) 計(jì)算出秒針的 start 及 end 坐標(biāo)
? ? ? ? float sin = (float) Math.sin(Math.toRadians(6 * ss));
? ? ? ? float cos = (float) Math.cos(Math.toRadians(6 * ss));
? ? ? ? float x0 = x - sin * (y / 10f);
? ? ? ? float y0 = y + cos * (y / 10f);
? ? ? ? float x1 = x + sin * (y * 13 / 20f);
? ? ? ? float y1 = y - cos * (y * 13 / 20f);
? ? ? ? mCanvas.drawLine(x0, y0, x1, y1, secondPaint);
? ? ? ? mCanvas.drawCircle(x, y,getWidth()/80f, secondPaint);
? ? }

? ? @Override
? ? protected void onAttachedToWindow() {
? ? ? ? super.onAttachedToWindow();
? ? ? ? refreshThread = new Thread(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? super.run();
? ? ? ? ? ? ? ? while (!isStop){
? ? ? ? ? ? ? ? ? ? SystemClock.sleep(1000);
? ? ? ? ? ? ? ? ? ? mHandler.sendEmptyMessage(0);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? refreshThread.start();
? ? }

? ? @Override
? ? protected void onDetachedFromWindow() {
? ? ? ? super.onDetachedFromWindow();
? ? ? ? mHandler.removeCallbacksAndMessages(null);
? ? ? ? isStop = true;
? ? }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論