Android 8.1 Launcher3实现动态指针时钟功能
本文主要实现功能,可能有不合理的地方
首先创建一个实现功能的工具里,直接上代码:
importandroid.content.Context; importandroid.graphics.Bitmap; importandroid.os.Handler; importandroid.os.Message; importcom.android.launcher3.ItemInfo; importcom.android.launcher3.LauncherSettings; importcom.android.launcher3.ShortcutInfo; importcom.android.launcher3.util.LogUtil; publicclassDeskClockUtil{ privateOnDeskClockIconChangeListenermListener; privateItemInfomItemInfo; privatebooleanmIsResume; privateHandlermHandler=newHandler(){ @Override publicvoidhandleMessage(Messagemsg){ super.handleMessage(msg); if(msg.what==100){ Messagemsg1=newMessage(); msg1.what=100; msg1.obj=msg.obj; mHandler.sendMessageDelayed(msg1,60000); if(mListener!=null&&mItemInfo!=null){ mListener.onChange(IconUtil.getDeskClockIcon((Context)msg.obj),mItemInfo); } } } }; privatestaticDeskClockUtilsInstance; privateDeskClockUtil(){ } publicstaticDeskClockUtilgetInstance(){ if(sInstance==null){ sInstance=newDeskClockUtil(); } returnsInstance; } privatevoidrefresh(Contextcontext){ if(mListener!=null&&mItemInfo!=null){ mListener.onChange(IconUtil.getDeskClockIcon(context),mItemInfo); } if(mHandler.hasMessages(100)){ mHandler.removeMessages(100); } Messagemsg=newMessage(); msg.what=100; msg.obj=context; mHandler.sendMessageDelayed(msg, 1000*(60-Integer.parseInt(DateUtils.getCurrentSecond()))); } publicvoidonResume(Contextcontext){ mIsResume=true; refresh(context); } publicvoidonPause(){ mIsResume=false; mHandler.removeMessages(100); } publicvoidsetListener(OnDeskClockIconChangeListenerlistener,ItemInfoinfo,Contextcontext){ if(!(infoinstanceofShortcutInfo)){ return; } Stringpkg=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); } } publicinterfaceOnDeskClockIconChangeListener{ voidonChange(Bitmapicon,ItemInfoinfo); } }
画出动态时钟
importandroid.content.Context; importandroid.graphics.*; importcom.android.launcher3.R; publicclassIconUtil{ privatestaticfinalStringTAG="IconUtil"; privatestaticBitmapgetBitmap(Contextcontext,intres){ BitmapFactory.Optionsoptions=newBitmapFactory.Options(); options.inPreferredConfig=Bitmap.Config.ARGB_4444; returnBitmapFactory.decodeResource(context.getResources(),res,options); } publicstaticBitmapgetDeskClockIcon(Contextcontext){ //添加一个带表盘的背景图 Bitmapempty=getBitmap(context,R.drawable.icon_time); intx=empty.getWidth(); inty=empty.getHeight(); BitmapdeskClock=Bitmap.createBitmap(x,y,Bitmap.Config.ARGB_4444); Canvascanvas=newCanvas(deskClock); Paintpaint=newPaint(); paint.setAntiAlias(true); canvas.drawBitmap(empty,0,0,paint); //设置圆角 paint.setStrokeCap(Paint.Cap.ROUND); paint.setStrokeWidth(5); paint.setColor(context.getResources().getColor(R.color.deskclock_time)); //时针的长度 intradius=35; //圆心的xy坐标 intcx=x/2; intcy=y/2; inthour=Integer.parseInt(DateUtils.getCurrentHour()); intmin=Integer.parseInt(DateUtils.getCurrentMin()); //时针的角度,这里是整点的角度。因为0°是从3点开始,所以这里减90°,从9点开始计算角度 intdrgeeHour=hour*30-90; if(drgeeHour<0){ drgeeHour+=360; } //加上时针在两个整点之间的角度,一分钟在分针上是6°,在时针上是min*6/12 drgeeHour+=min*6/12; //时针针尖的xy坐标,相当于已知圆心坐标和半径,求圆上任意一点的坐标 intxHour=(int)(cx+radius*Math.cos(drgeeHour*3.14/180)); intyHour=(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); //分针的角度 intdrgeeMin=min*6-90; if(drgeeMin<0){ drgeeMin+=360; } //分针针尖的xy坐标 intx1=(int)(cx+radius*Math.cos(drgeeMin*Math.PI/180)); inty1=(int)(cy+radius*Math.sin(drgeeMin*Math.PI/180)); canvas.drawLine(x1,y1,cx,cy,paint); returndeskClock; } }
时间工具类
importjava.text.SimpleDateFormat; publicclassDateUtils{ publicstaticStringgetCurrentDay(){ SimpleDateFormatformat=newSimpleDateFormat("dd"); Longt=newLong(System.currentTimeMillis()); Stringd=format.format(t); returnd; } publicstaticStringgetCurrentSecond(){ SimpleDateFormatformat=newSimpleDateFormat("ss"); Longt=newLong(System.currentTimeMillis()); Stringd=format.format(t); returnd; } publicstaticStringgetCurrentMin(){ SimpleDateFormatformat=newSimpleDateFormat("mm"); Longt=newLong(System.currentTimeMillis()); Stringd=format.format(t); returnd; } publicstaticStringgetCurrentHour(){ SimpleDateFormatformat=newSimpleDateFormat("HH"); Longt=newLong(System.currentTimeMillis()); Stringd=format.format(t); returnd; } }
下面就比较简单了,我是在BubbleTextView.java中添加listener,我这里偷懒了,应该给时钟单独创建一个view,继承BubbleTextView。
privatevoidapplyIconAndLabel(Bitmapicon,ItemInfoinfo){ /*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); } } privatevoidsetDeskClockIcon(ItemInfoinfo){ DeskClockUtil.getInstance().setListener(newDeskClockUtil.OnDeskClockIconChangeListener(){ @Override publicvoidonChange(Bitmapicon,ItemInfoinfo){ applyIcon(icon,info); } },info,getContext()); } privatevoidapplyIcon(Bitmapicon,ItemInfoinfo){ FastBitmapDrawableiconDrawable=DrawableFactory.get(getContext()).newIcon(icon,info); iconDrawable.setIsDisabled(info.isDisabled()); setIcon(iconDrawable); }
在Launcher.java的onResume()和onPause()中分别开始和暂停
@OverrideprotectedvoidonResume(){ ...... /*begin*/ DeskClockUtil.getInstance().onResume(this); /*end*/ if(mLauncherCallbacks!=null){ mLauncherCallbacks.onResume(); } } @Override protectedvoidonPause(){ //EnsurethatitemsaddedtoLauncherarequeueduntilLauncherreturns InstallShortcutReceiver.enableInstallQueue(InstallShortcutReceiver.FLAG_ACTIVITY_PAUSED); super.onPause(); mPaused=true; mDragController.cancelDrag(); mDragController.resetLastGestureUpTime(); //WecallonHide()aggressively.Thecustomcontentcallbacksshouldbeableto //debounceexcessonHidecalls. if(mWorkspace.getCustomContentCallbacks()!=null){ mWorkspace.getCustomContentCallbacks().onHide(); } if(mLauncherCallbacks!=null){ mLauncherCallbacks.onPause(); } /*begin*/ DeskClockUtil.getInstance().onPause(); /*end*/ }
这样就可以了,如果想要加秒针,在IconUtil中再把秒针画出来就行。
还有日历的动态图标也可以用同样的方法实现
总结
以上所述是小编给大家介绍的Android8.1Launcher3实现动态指针时钟功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!