本文主要實現(xiàn)功能,可能有不合理的地方
首先創(chuàng)建一個實現(xiàn)功能的工具里,直接上代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
import android.content.Context; import android.graphics.Bitmap; import android.os.Handler; import android.os.Message; import com.android.launcher3.ItemInfo; import com.android.launcher3.LauncherSettings; import com.android.launcher3.ShortcutInfo; import com.android.launcher3.util.LogUtil; public class DeskClockUtil { private OnDeskClockIconChangeListener mListener; private ItemInfo mItemInfo; private boolean mIsResume; private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super .handleMessage(msg); if (msg.what == 100 ) { Message msg1 = new Message(); msg1.what = 100 ; msg1.obj = msg.obj; mHandler.sendMessageDelayed(msg1, 60000 ); if (mListener != null && mItemInfo != null ) { mListener.onChange(IconUtil.getDeskClockIcon((Context) msg.obj), mItemInfo); } } } }; private static DeskClockUtil sInstance; private DeskClockUtil() { } public static DeskClockUtil getInstance() { if (sInstance == null ) { sInstance = new DeskClockUtil(); } return sInstance; } private void refresh(Context context) { if (mListener != null && mItemInfo != null ) { mListener.onChange(IconUtil.getDeskClockIcon(context), mItemInfo); } if (mHandler.hasMessages( 100 )) { mHandler.removeMessages( 100 ); } Message msg = new Message(); msg.what = 100 ; msg.obj = context; mHandler.sendMessageDelayed(msg, 1000 * ( 60 - Integer.parseInt(DateUtils.getCurrentSecond()))); } public void onResume(Context context) { mIsResume = true ; refresh(context); } public void onPause() { mIsResume = false ; mHandler.removeMessages( 100 ); } public void setListener(OnDeskClockIconChangeListener listener, ItemInfo info, Context context) { if (!(info instanceof ShortcutInfo)) { return ; } String pkg = null ; if (info.getIntent() != null && info.getIntent().getComponent() != null ) { pkg = info.getIntent().getComponent().getPackageName(); } if (! "com.android.deskclock" .equals(pkg) || info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) { return ; } mListener = listener; mItemInfo = info; if (mIsResume) { refresh(context); } } public interface OnDeskClockIconChangeListener { void onChange(Bitmap icon, ItemInfo info); } } |
畫出動態(tài)時鐘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import android.content.Context; import android.graphics.*; import com.android.launcher3.R; public class IconUtil { private static final String TAG = "IconUtil" ; private static Bitmap getBitmap(Context context, int res) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_4444; return BitmapFactory.decodeResource(context.getResources(), res, options); } public static Bitmap getDeskClockIcon(Context context) { // 添加一個帶表盤的背景圖 Bitmap empty = getBitmap(context, R.drawable.icon_time); int x = empty.getWidth(); int y = empty.getHeight(); Bitmap deskClock = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_4444); Canvas canvas = new Canvas(deskClock); Paint paint = new Paint(); paint.setAntiAlias( true ); canvas.drawBitmap(empty, 0 , 0 , paint); //設(shè)置圓角 paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth( 5 ); paint.setColor(context.getResources().getColor(R.color.deskclock_time)); // 時針的長度 int radius = 35 ; // 圓心的x y 坐標(biāo) int cx = x / 2 ; int cy = y / 2 ; int hour = Integer.parseInt(DateUtils.getCurrentHour()); int min = Integer.parseInt(DateUtils.getCurrentMin()); //時針的角度,這里是整點的角度。因為0°是從3點開始,所以這里減90°,從9點開始計算角度 int drgeeHour = hour * 30 - 90 ; if (drgeeHour < 0 ) { drgeeHour += 360 ; } // 加上時針在兩個整點之間的角度,一分鐘在分針上是6°,在時針上是min * 6 / 12 drgeeHour += min * 6 / 12 ; //時針 針尖的x y坐標(biāo),相當(dāng)于已知圓心坐標(biāo)和半徑,求圓上任意一點的坐標(biāo) int xHour = ( int ) (cx + radius * Math.cos(drgeeHour * 3.14 / 180 )); int yHour = ( int ) (cy + radius * Math.sin(drgeeHour * 3.14 / 180 )); canvas.drawLine(xHour, yHour, cx, cy, paint); //分針的長度 radius = 45 ; paint.setStrokeWidth( 3 ); paint.setColor(Color.RED); //分針的角度 int drgeeMin = min * 6 - 90 ; if (drgeeMin < 0 ) { drgeeMin += 360 ; } //分針 針尖的x y坐標(biāo) int x1 = ( int ) (cx + radius * Math.cos(drgeeMin * Math.PI / 180 )); int y1 = ( int ) (cy + radius * Math.sin(drgeeMin * Math.PI / 180 )); canvas.drawLine(x1, y1, cx, cy, paint); return deskClock; } } |
時間工具類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import java.text.SimpleDateFormat; public class DateUtils { public static String getCurrentDay() { SimpleDateFormat format = new SimpleDateFormat( "dd" ); Long t = new Long(System.currentTimeMillis()); String d = format.format(t); return d; } public static String getCurrentSecond() { SimpleDateFormat format = new SimpleDateFormat( "ss" ); Long t = new Long(System.currentTimeMillis()); String d = format.format(t); return d; } public static String getCurrentMin() { SimpleDateFormat format = new SimpleDateFormat( "mm" ); Long t = new Long(System.currentTimeMillis()); String d = format.format(t); return d; } public static String getCurrentHour() { SimpleDateFormat format = new SimpleDateFormat( "HH" ); Long t = new Long(System.currentTimeMillis()); String d = format.format(t); return d; } } |
下面就比較簡單了,我是在BubbleTextView.java中添加listener,我這里偷懶了,應(yīng)該給時鐘單獨創(chuàng)建一個view,繼承BubbleTextView。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
private void applyIconAndLabel(Bitmap icon, ItemInfo info) { /* begin */ setDeskClockIcon(info); /* end */ applyIcon(icon, info); setText(info.title); if (info.contentDescription != null ) { setContentDescription(info.isDisabled() ? getContext().getString(R.string.disabled_app_label, info.contentDescription) : info.contentDescription); } } private void setDeskClockIcon(ItemInfo info) { DeskClockUtil.getInstance().setListener( new DeskClockUtil.OnDeskClockIconChangeListener() { @Override public void onChange(Bitmap icon, ItemInfo info) { applyIcon(icon, info); } }, info, getContext()); } private void applyIcon(Bitmap icon, ItemInfo info) { FastBitmapDrawable iconDrawable = DrawableFactory.get(getContext()).newIcon(icon, info); iconDrawable.setIsDisabled(info.isDisabled()); setIcon(iconDrawable); } |
在Launcher.java的onResume()和onPause()中分別開始和暫停
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@Override protected void onResume() { ...... /* begin */ DeskClockUtil.getInstance().onResume( this ); /* end */ if (mLauncherCallbacks != null ) { mLauncherCallbacks.onResume(); } } @Override protected void onPause() { // Ensure that items added to Launcher are queued until Launcher returns InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED); super .onPause(); mPaused = true ; mDragController.cancelDrag(); mDragController.resetLastGestureUpTime(); // We call onHide() aggressively. The custom content callbacks should be able to // debounce excess onHide calls. if (mWorkspace.getCustomContentCallbacks() != null ) { mWorkspace.getCustomContentCallbacks().onHide(); } if (mLauncherCallbacks != null ) { mLauncherCallbacks.onPause(); } /* begin */ DeskClockUtil.getInstance().onPause(); /* end */ } |
這樣就可以了,如果想要加秒針,在IconUtil中再把秒針畫出來就行。
還有日歷的動態(tài)圖標(biāo)也可以用同樣的方法實現(xiàn)
總結(jié)
以上所述是小編給大家介紹的Android 8.1 Launcher3實現(xiàn)動態(tài)指針時鐘功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/qq_30552095/article/details/81021625