android自定義圓盤時鐘

   

自定義圓盤時鐘的大概流程:因爲圓盤時鐘的圓盤是不需要動的,所以不必要加在自定義的view裏面,在view裏面只需要繪製秒針和分針時針並控其轉動即可

以下就是自定義view的主要代碼:

package com.example.chl.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by chl on 16-3-30.
 */
public class TimeVIew extends View {
    private Paint mPaint;
    // private Bitmap bitmap = null;
    private Bitmap ssBitmap = null;
    private Bitmap sssBitmap = null;
    private Bitmap mmBitmap = null;
    private Bitmap mmmBitmap = null;
//    private int x;
//    private int y;
    private int ssx;
    private int ssy;
    private int mmx;
    private int mmy;
    private Context mContext;
    private Matrix matrix = null;
    private Matrix mmatrix = null;
    private float angle = 0;//秒針每秒偏移的角度
    private float mangle = 0;//分針每秒偏移的角度
    private MyThread myThread;

    public TimeVIew(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initBitmap();

    }

    public TimeVIew(Context context) {
        super(context);
        this.mContext = context;
        initBitmap();
    }

    public TimeVIew(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initBitmap();

    }

    private void initBitmap() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        // bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.global_dial_day_bg,options);
        ssBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.global_second_day_small);
        mmBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.global_minute_day_small);
        // x = bitmap.getWidth();
        // y = bitmap.getHeight();
        ssx = ssBitmap.getWidth();//獲取bitmap的寬度
        ssy = ssBitmap.getHeight();
        mmx = mmBitmap.getWidth();
        mmy = mmBitmap.getHeight();
        matrix = new Matrix();
        matrix.setRotate(angle);//設置圖片旋轉角度
        mmatrix = new Matrix();
        matrix.setRotate(mangle);
        sssBitmap = Bitmap.createBitmap(ssBitmap, 0, 0, ssx, ssy, matrix, true);//創建新的bitmap
        mmmBitmap = Bitmap.createBitmap(mmBitmap, 0, 0, mmx, mmy, mmatrix, true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawBitmap(bitmap, Util.getDeviceWidth(mContext) / 2 - x / 2, Util.getDeviceHeight(mContext) / 2 - y / 2, mPaint);

        if (myThread == null) {
            myThread = new MyThread();
            myThread.start();
        }
        canvas.drawBitmap(mmmBitmap, getWidth() / 2 - mmmBitmap.getWidth() / 2, getHeight() / 2 - mmmBitmap.getHeight() / 2, mPaint);
        canvas.drawBitmap(sssBitmap, getWidth() / 2 - sssBitmap.getWidth() / 2, getHeight() / 2 - sssBitmap.getHeight() / 2, mPaint);

    }


    class MyThread extends Thread {
        private int num=0;

        @Override
        public void run() {
            while (true) {
                if (angle == 360) {
                    angle = 0;
                }
                if (mangle == 360) {
                    mangle = 0;
                }

                matrix.setRotate(angle);
                sssBitmap = Bitmap.createBitmap(ssBitmap, 0, 0, ssx, ssy, matrix, true);
                mmatrix.setRotate(mangle);
                mmmBitmap = Bitmap.createBitmap(mmBitmap, 0, 0, mmx, mmy, mmatrix, true);
                angle += 6;
                if (num%5==0) {//控制分針五秒移動一個角度,也可以每秒都讓其移動
                    mangle += 0.5;

                }
                num++;
                if (num==200){
                    num=0;
                }
                postInvalidate();//重新繪製
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }
    }


}

主佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/global_dial_day_bg"
        />
    <com.example.chl.myapplication.TimeVIew

        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>



圖片資源下載:http://download.csdn.net/detail/cao185493676/9482843

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章