android自定義手表效果
本文實(shí)例為大家分享了android自定義手表效果的具體代碼,供大家參考,具體內(nèi)容如下
1.效果圖:

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;
/**
* 優(yōu)化方案:
* 表盤課繪制一次
* 在分線程中進(jìn)行加載
*/
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);
// 內(nèi)層圓 --》圓心
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(width / 2, height / 2, 10, paint);
// 循環(huán)畫刻度(旋轉(zhuǎn)畫刻度)
for (int i = 0; i < 12; i++) {
if (i % 3 == 0) {
paint.setStrokeWidth(10);
canvas.drawLine(width / 2, 0, width / 2, 24, paint);
canvas.rotate(30, width / 2, height / 2);
} else {
canvas.drawLine(width / 2, 0, width / 2, 10, paint);
canvas.rotate(30, 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, width / 2, height / 2);
canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint);
canvas.restore();
// canvas.rotate(360 - mDegrees, width / 2, height / 2);
//畫分鐘指針
paint.setStrokeWidth(4);
mDegrees = mMinute * 6 + mSecond / 10;
length = (width / 2) * 0.78f;
canvas.save();
canvas.rotate(mDegrees, width / 2, height / 2);
canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, paint);
canvas.restore();
// canvas.rotate(360 - mDegrees, width / 2, height / 2);
//畫分鐘指針
paint.setStrokeWidth(2);
mDegrees = mSecond * 6;
length = (width / 2) * 0.92f;
canvas.save();
canvas.rotate(mDegrees, width / 2, height / 2);
canvas.drawLine(width / 2, height / 2, width / 2, height - (height / 2) - length, 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());
}
}
}
點(diǎn)擊運(yùn)行就可以了,這樣手機(jī)就可以當(dāng)手表用了,真的神奇。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android獲取設(shè)備CPU核數(shù)、時鐘頻率以及內(nèi)存大小的方法
- android實(shí)現(xiàn)widget時鐘示例分享
- Android多功能時鐘開發(fā)案例(實(shí)戰(zhàn)篇)
- Android 仿日歷翻頁、仿htc時鐘翻頁、數(shù)字翻頁切換效果
- android高仿小米時鐘(使用Camera和Matrix實(shí)現(xiàn)3D效果)
- Android多功能時鐘開發(fā)案例(基礎(chǔ)篇)
- Android實(shí)現(xiàn)簡單時鐘View的方法
- Android仿小米時鐘效果
- Android編程基于自定義控件實(shí)現(xiàn)時鐘功能的方法
- Android畫個時鐘玩玩
相關(guān)文章
android上的一個網(wǎng)絡(luò)接口和圖片緩存框架enif簡析
android上的一個網(wǎng)絡(luò)接口和圖片緩存框架enif詳細(xì)介紹:底層網(wǎng)絡(luò)接口采用apache的httpclient連接池框架、圖片緩存采用基于LRU的算法等等,需要了解的朋友可以詳細(xì)參考下2012-12-12
Android使用Volley實(shí)現(xiàn)上傳文件功能
這篇文章主要介紹了Android使用Volley實(shí)現(xiàn)上傳文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
AndroidHttpClient詳解及調(diào)用示例
本文給大家介紹AndroidHttpClient結(jié)構(gòu)、使用方式及調(diào)用示例詳解,需要的朋友可以參考下2015-10-10
flutter實(shí)現(xiàn)發(fā)送驗(yàn)證碼功能
這篇文章主要為大家詳細(xì)介紹了flutter發(fā)送驗(yàn)證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
Android webview手動校驗(yàn)https證書(by 星空武哥)
有些時候由于Android系統(tǒng)的bug或者其他的原因,導(dǎo)致我們的webview不能驗(yàn)證通過我們的https證書,最明顯的例子就是華為手機(jī)mate7升級到Android7.0后,手機(jī)有些網(wǎng)站打不開了,而更新了webview的補(bǔ)丁后就沒問題了2017-09-09
Android控件系列之RadioButton與RadioGroup使用方法
本文介紹了Android中如何使用RadioGroup和RadioButton,對比了RadioButton和CheckBox的區(qū)別,并實(shí)現(xiàn)了自定義的RadioGroup中被選中RadioButton的變更監(jiān)聽事件2012-11-11
Android封裝高德地圖定位工具類Util的詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于Android封裝高德地圖定位工具類Util的相關(guān)資料,封裝成工具類后非常方便以后的項(xiàng)目,可以直接使用,文中也給出了詳細(xì)的實(shí)例代碼,需要的朋友可以參考下2021-07-07

