android自定义圆盘时钟,带你玩转自定义view系列

52 阅读2分钟
    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();

            }



        }

    }

}

}


  

主布局文件:



<FrameLayout xmlns:android="schemas.android.com/apk/res/and…"

android:background="#000000"

android:layout_width="match_parent"

android:layout_height="match_parent"

新的开始

改变人生,没有什么捷径可言,这条路需要自己亲自去走一走,只有深入思考,不断反思总结,保持学习的热情,一步一步构建自己完整的知识体系,才是最终的制胜之道,也是程序员应该承担的使命。

如果有需要进阶Android高级工程师系统学习资料的,我可以免费分享给大家,需要完整版的朋友,【点这里可以看到全部内容】。

《系列学习视频》

《系列学习文档》

《我的大厂面试之旅》