1. 程式人生 > 程式設計 >android自定義手錶效果

android自定義手錶效果

本文例項為大家分享了android自定義手錶效果的具體程式碼,供大家參考,具體內容如下

1.效果圖:

android自定義手錶效果

2.佈局

<?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"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >
 
 <Button
  android:id="@+id/btn_start"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="start" />
 .
 
 <Button
  android:id="@+id/btn_stop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="stop" />
 
 <ImageView
  android:id="@+id/iv_clock"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:src="@mipmap/ic_launcher" />
</LinearLayout>

3.自定義view,顯示

package com.example.administrator.testz;
 
 
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
 
import java.util.Calendar;
 
/**
 * 優化方案:
 * 錶盤課繪製一次
 * 在分執行緒中進行載入
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 private Button btnStart,btnStop;
 private ImageView mClockImageView;
 Bitmap.Config config = Bitmap.Config.ARGB_8888;
 int width = 500;
 int height = 500;
 
 private Calendar mCalendar;
 private int mHour,mMinute,mSecond;
 private float mDegrees;
 private float length;
 
 private boolean mIsRunning;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  btnStart = (Button) findViewById(R.id.btn_start);
  btnStop = (Button) findViewById(R.id.btn_stop);
  btnStop.setOnClickListener(this);
  btnStart.setOnClickListener(this);
 
  mClockImageView = (ImageView) findViewById(R.id.iv_clock);
  mClockImageView.setImageBitmap(drawClock());
 
 }
 
 /**
  * 畫表盤
  */
 private Bitmap drawClockFace() {
  Bitmap bm = Bitmap.createBitmap(width,height,config);
 
  Canvas canvas = new Canvas(bm);
  Paint paint = new Paint();
  paint.setAntiAlias(true); //鋸齒
  paint.setStyle(Paint.Style.STROKE); // 空心
  paint.setStrokeWidth(5);
  paint.setColor(Color.parseColor("#333333"));
 
  // 外層圓
  canvas.drawCircle(width / 2,height / 2,width / 2,paint);
 
  // 內層圓 --》圓心
  paint.setStyle(Paint.Style.FILL);
  canvas.drawCircle(width / 2,10,paint);
 
  // 迴圈畫刻度(旋轉畫刻度)
  for (int i = 0; i < 12; i++) {
   if (i % 3 == 0) {
    paint.setStrokeWidth(10);
    canvas.drawLine(width / 2,24,paint);
    canvas.rotate(30,height / 2);
   } else {
    canvas.drawLine(width / 2,height / 2);
   }
  }
 
 
  return bm;
 }
 
 private Bitmap drawClock() {
  Bitmap bm = drawClockFace();
 
  Canvas canvas = new Canvas(bm);
 
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setColor(Color.parseColor("#333333"));
 
  mCalendar = Calendar.getInstance();
  mHour = mCalendar.get(Calendar.HOUR);
  mMinute = mCalendar.get(Calendar.MINUTE);
  mSecond = mCalendar.get(Calendar.SECOND);
 
  //畫小時指標
  paint.setStrokeWidth(10);
  mDegrees = mHour * 30 + mMinute / 2;
  length = (width / 2) * 0.7f;
  canvas.save();
  canvas.rotate(mDegrees,height / 2);
  canvas.drawLine(width / 2,height - (height / 2) - length,paint);
  canvas.restore();
//  canvas.rotate(360 - mDegrees,height / 2);
 
  //畫分鐘指標
  paint.setStrokeWidth(4);
  mDegrees = mMinute * 6 + mSecond / 10;
  length = (width / 2) * 0.78f;
  canvas.save();
  canvas.rotate(mDegrees,height / 2);
 
  //畫分鐘指標
  paint.setStrokeWidth(2);
  mDegrees = mSecond * 6;
  length = (width / 2) * 0.92f;
  canvas.save();
  canvas.rotate(mDegrees,paint);
  canvas.restore();
 
  return bm;
 }
 
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.btn_start:
    mIsRunning = true;
    new ClockTask().execute("");
    break;
   case R.id.btn_stop:
    mIsRunning = false;
    break;
  }
 }
 
 @Override
 protected void onDestroy() {
  super.onDestroy();
  mIsRunning = false;
 }
 
 private class ClockTask extends AsyncTask<Object,Object,Object> {
 
  @Override
  protected Object doInBackground(Object... objects) {
   while (mIsRunning) {
    publishProgress("");
    try {
     Thread.sleep(1000);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
   return null;
  }
 
  @Override
  protected void onProgressUpdate(Object... values) {
   super.onProgressUpdate(values);
   mClockImageView.setImageBitmap(drawClock());
  }
 }
 
 
}

點選執行就可以了,這樣手機就可以當手表用了,真的神奇。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。