android實(shí)現(xiàn)icon動(dòng)態(tài)旋轉(zhuǎn)效果
本文實(shí)例為大家分享了android實(shí)現(xiàn)icon動(dòng)態(tài)旋轉(zhuǎn)效果的具體代碼,供大家參考,具體內(nèi)容如下
碰到客戶的這樣一個(gè)需求,點(diǎn)擊icon后,前景的icon開(kāi)始旋轉(zhuǎn),背景的icon不動(dòng),就是這樣一個(gè)效果
通過(guò)第三方的方法是不可能實(shí)現(xiàn)的,我這里是通過(guò)修改系統(tǒng)launcher的代碼來(lái)實(shí)現(xiàn)。實(shí)現(xiàn)思路是在launcher中找到顯示icon圖標(biāo)代碼,并把這個(gè)圖標(biāo)覆蓋掉。很多第手機(jī)的時(shí)鐘icon是可以動(dòng)態(tài)變化的,好在公司已經(jīng)有人實(shí)現(xiàn)這個(gè)功能,可以借鑒
我這里先把時(shí)鐘動(dòng)態(tài)icon的實(shí)現(xiàn)說(shuō)明下,需要的朋友可以參考。
寫(xiě)一個(gè)IconScript的基類(lèi)繼承Drawable
package com.android.launcher3; ? import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.view.View; import android.util.Log; ? public class IconScript extends Drawable{ ? ? ? public boolean isRuning = false; ? ? ? public FastBitmapDrawable mFastBitmapDrawable = null; ? ? ? protected Paint mPaint = new Paint(); ? ? ? ?? ? ? public IconScript(){ ? ? ? ? ? mPaint.setAntiAlias(true); ?? ? ? ? ? mPaint.setFilterBitmap(true); ? ? ? } ? ? ? ?? ? ? public void draw(Canvas canvas){ ? ? ? ? ? if(mFastBitmapDrawable != null){ ? ?? ??? ? ? ?Log.e("fly","IconScript="); ? ? ? ? ? ? canvas.drawBitmap(mFastBitmapDrawable.getBitmap(), null, getBounds(),mPaint);//畫(huà)底圖 ? ? ? ? ? } ? ? ? } ? ? ? ?? ? ? /**? ? ? ?* 運(yùn)行腳本? ? ? ?* @param view? ? ? ?*/ ? ? ? public void run(View view){ ? ? ? ? ? isRuning = true; ? ? ? } ? ? ? ?? ? ? /**? ? ? ?* 停止腳本? ? ? ?* (未調(diào)用,暫留入口)? ? ? ?*/ ? ? ? public void onStop(){ ? ? ? ? ? isRuning = false; ? ? ? } ? ? ? ?? ? ? /**? ? ? ?* 暫停腳本? ? ? ?* (未調(diào)用,暫留入口)? ? ? ?*/ ? ? ? public void onPause(){ ? ? ? ? ? isRuning = false; ? ? ? } ? ? ? ?? ? ? /**? ? ? ?* 恢復(fù)腳本? ? ? ?* (未調(diào)用,暫留入口)? ? ? ?*/ ? ? ? public void onResume(){ ? ? ? ? ? isRuning = true; ? ? ? } ? ?? ? ? @Override ? ? ? public int getOpacity() { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? return 0; ? ? ? } ? ?? ? ? @Override ? ? ? public void setAlpha(int arg0) { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ?? ? ? } ? ?? ? ? @Override ? ? ? public void setColorFilter(ColorFilter arg0) { ? ? ? ? ? // TODO Auto-generated method stub ? ? ? ? ? ?? ? ? } ? ? ? ?? ? ? @Override ? ? ? public int getIntrinsicWidth() { ? ? ? ? ? int width = getBounds().width(); ? ? ? ? ? if (width == 0) { ? ? ? ? ? ? ? width = mFastBitmapDrawable.getBitmap().getWidth(); ? ? ? ? ? } ? ? ? ? ? return width; ? ? ? } ? ?? ? ? @Override ? ? ? public int getIntrinsicHeight() { ? ? ? ? ? int height = getBounds().height(); ? ? ? ? ? if (height == 0) { ? ? ? ? ? ? ? height = mFastBitmapDrawable.getBitmap().getHeight(); ? ? ? ? ? } ? ? ? ? ? return height; ? ? ? } ? ?? ? ? @Override ? ? ? public int getMinimumWidth() { ? ? ? ? ? return getBounds().width(); ? ? ? } ? ?? ? ? @Override ? ? ? public int getMinimumHeight() { ? ? ? ? ? return getBounds().height(); ? ? ? } ? ? ? ?? ? ? @Override ? ? ? public void setFilterBitmap(boolean filterBitmap) { ? ? ? ? ? mPaint.setFilterBitmap(filterBitmap); ? ? ? ? ? mPaint.setAntiAlias(filterBitmap); ? ? ? } ? ? ? ?? ? ? public void setFastBitmapDrawable(FastBitmapDrawable drawable){ ? ? ? ? ? mFastBitmapDrawable = drawable; ? ? ? } ? ? ? ?? ? ? public FastBitmapDrawable setFastBitmapDrawable(){ ? ? ? ? ? return mFastBitmapDrawable; ? ? ? } ? } ?
核心類(lèi)ClockScript繼承IconScript
package com.android.launcher3; ? ? import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.text.format.Time; import android.view.View; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; ? ? ? ? public class ClockScript extends IconScript { ? ? ?Rect mRect = null; ? ? ?/**? ? ? * 效果展示目標(biāo)View? ? ? */ ? ? ?private View mView; ? ? ?/**? ? ? * 通知系統(tǒng)更新視圖現(xiàn)成? ? ? */ ? ? ?private ClockThread mClockThread = null; ? ? ?/**? ? ? * 當(dāng)前是否顯示在屏幕上? ? ? */ ? ? ?private boolean mIsShowInScreen = false; ? ? ?Context mContext;? ? ? ? ?public ClockScript(Context context){ ? ? ? ? ?super(); ? ? ? mContext = context; ? ? ?} ? ? ?public void run(View view) { ? ? ? ? ?mView = view; ? ? ? ? ?mRect = getBounds(); ? ? ? ? ?if(mClockThread == null){ ? ? ? ? ? ? ?mClockThread = new ClockThread(); ? ? ? ? ? ? ?mClockThread.start(); ? ? ? ? ?} ? ? ?} ? ? ? ? ? ?@Override ? ? ?public void onPause() { ? ? ? ? ?mClockThread.pauseRun(); ? ? ? ? ?super.onPause(); ? ? ?} ? ? ? ? ? ?@Override ? ? ?public void onResume() { ? ? ? ? ?mClockThread.resumeRun(); ? ? ? ? ?super.onResume(); ? ? ?} ? ? ? ? ? ?@Override ? ? ?public void onStop() { ? ? ? ? ?mClockThread.stopRun(); ? ? ? ? ?super.onStop(); ? ? ?} ? ? ? ? ? ? ?@Override ? ? ?public void draw(Canvas canvas) { ? ? ? ? ?super.draw(canvas); ? ? ? ? ?mIsShowInScreen = true; ? ? ? ? ? ? ?drawIndicator(canvas,mRect.centerX(),mRect.centerY(),mPaint); ? ? ? ? ? ? ?if(mClockThread.wait){ ? ? ? ? ? ? ?mClockThread.resumeRun(); ? ? ? ? ?} ? ? ?} ? ? ?/**? ? ? * 畫(huà)指針? ? ? * @param canvas? ? ? * @param centerX? ? ? * @param centerY? ? ? * @param p? ? ? */ ? ?private void drawIndicator(Canvas canvas,int centerX,int centerY,Paint p){ ? ? ? ? ? ? ? ? Bitmap clockIcon = Utilities.createIconBitmap( BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher_clock),mContext); ? ? ? ? ? ? int X = clockIcon.getWidth()/2; ? ? int Y = clockIcon.getHeight()/2; ? ? canvas.drawBitmap(clockIcon, null, getBounds(),p);//畫(huà)底圖 ? ? ? ? Time t=new Time(); ? ? ?t.setToNow();? ? ? p.setAntiAlias(true); ? ? p.setStrokeWidth(3); ? ? p.setColor(Color.WHITE); ? ? p.setStyle(Paint.Style.FILL); ? ? //hour canvas.drawLine(X, Y, int)(X + (clockIcon.getWidth()/2-35) * Math.cos((t.hour+(float)t.minute/60) * (Math.PI / 6) - Math.PI / 2)), (int)(Y + (clockIcon.getWidth()/2-35) * Math.sin((t.hour+(float)t.minute/60) * (Math.PI / 6) - Math.PI / 2)), p); //minute canvas.drawLine(X, Y,(int)(X + (clockIcon.getWidth()/2-27) * Math.cos(t.minute * (Math.PI / 30) - Math.PI / 2)),(int)(Y + (clockIcon.getWidth()/2-27) * Math.sin(t.minute * (Math.PI / 30) - Math.PI / 2)),p); //second p.setColor(Color.RED); p.setStrokeWidth(1); p.setStyle(Paint.Style.FILL); canvas.drawLine(X, Y,(int)(X + (clockIcon.getWidth()/2-20) * Math.cos(t.second * (Math.PI / 30) - Math.PI / 2)),(int)(Y + (clockIcon.getWidth()/2-20) * Math.sin(t.second * (Math.PI / 30) - Math.PI / 2)),p); ? ? ?? p.setColor(Color.WHITE); canvas.drawCircle(X, Y, 4, p); p.setColor(Color.GRAY); canvas.drawCircle(X, Y, 2, p); ? } ? ? ? ? ? ?class ClockThread extends Thread { ? ? ? ? ?int times = 0; ? ? ? ? ?boolean running = true; ? ? ? ? ? ?public boolean wait = false; ? ? ? ? ? ?public void stopRun() { ? ? ? ? ? ? ?running = false; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?} ? ? ? ? ?}; ? ? ? ? ? ?public void pauseRun() { ? ? ? ? ? ? ?this.wait = true; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ? ?public void resumeRun() { ? ? ? ? ? ? ?this.wait = false; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?} ? ? ? ? ?} ? ? ? ? ? ?public void run() { ? ? ? ? ? ? ?while (running) { ? ? ? ? ? ? ? ? ?synchronized (mView) { ? ? ? ? ? ? ? ? ? ? ?mView.postInvalidate(); ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if(!mIsShowInScreen){ ? ? ? ? ? ? ? ? ? ? ?pauseRun(); ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ?mIsShowInScreen = false; ? ? ? ? ? ? ? ? ?try { ? ? ? ? ? ? ? ? ? ? ?Thread.sleep(500); ? ? ? ? ? ? ? ? ?} catch (Exception e) { ? ? ? ? ? ? ? ? ? ? ?System.out.println(e); ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ? ? ?if (wait) { ? ? ? ? ? ? ? ? ? ? ? ? ?try { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?wait(); ? ? ? ? ? ? ? ? ? ? ? ? ?} catch (InterruptedException e) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// TODO Auto-generated catch block ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace(); ? ? ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ?} ? ? ? ? ?} ? ? ?} ? ? } ?
接下來(lái)就是如何初始化這個(gè)ClockScript,在Icon類(lèi)里面添加代碼
--- a/packages/apps/Launcher3/src/com/android/launcher3/IconCache.java +++ b/packages/apps/Launcher3/src/com/android/launcher3/IconCache.java @@ -84,6 +84,7 @@ public class IconCache { ? ? ? ? ?public CharSequence title = ""; ? ? ? ? ?public CharSequence contentDescription = ""; ? ? ? ? ?public boolean isLowResIcon; + ? ? ? ?public IconScript script; ? ? ?} ? ? ? ?private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>(); @@ -591,7 +592,17 @@ public class IconCache { ? ? ? ? if (info != null) { ? ? ? ? ? ? entry.title = info.getLabel(); ? ? ? ? } - + ? ? ? + ? ? ? Log.e("IconCache ","componentName.getPackageName()="+componentName.getPackageName()); ? ? ? ? //加了一個(gè)系統(tǒng)的屬性來(lái)控制 + ? ? ? if(null != entry && componentName.getPackageName().equals("com.android.deskclock") ?&& android.os.SystemProperties.getBoolean("launcher.calender.updateicon", false)) + ? ? ? { ? ? ? ? + ? ? ? ? ?Log.e("IconCache","clock init"); + ? ? ? ? ? entry.script = new ClockScript(mContext); ? + ? ? ? } ? ? ? ? ? ?return entry; ? ? ?} ? @@ -891,4 +902,20 @@ public class IconCache { ? ? ? ? ? ? ?return null; ? ? ? ? ?} ? ? ?} + ? ?public IconScript getScript(Intent intent, UserHandleCompat user){ ? + ? ? ? ?synchronized (mCache) { ? + ? ? ? ? ? ? ComponentName component = intent.getComponent(); ? + ? ? ? ? ? ? + ? ? ? ? ? ? if (component == null) { ? + ? ? ? ? ? ? ? ?Log.e("IconCache ","component==null"); + ? ? ? ? ? ? ? ? return null; ? + ? ? ? ? ? ? } ? + ? ? ? ? ? ?LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user); ? + ? ? ? ? ? ?CacheEntry entry = cacheLocked(component, launcherActInfo,user, false, false); ? + ? ? ? ? ? ?return entry.script; ? + ? ? ? ?} ? + ? ?} ?
在BubbleTextView類(lèi)中添加代碼
--- a/packages/apps/Launcher3/src/com/android/launcher3/BubbleTextView.java +++ b/packages/apps/Launcher3/src/com/android/launcher3/BubbleTextView.java @@ -30,6 +30,7 @@ import android.graphics.drawable.ColorDrawable; ?import android.graphics.drawable.Drawable; ?import android.os.Build; ?import android.util.AttributeSet; +import android.util.Log; ?import android.util.SparseArray; ?import android.util.TypedValue; ?import android.view.KeyEvent; @@ -38,6 +39,7 @@ import android.view.View; ?import android.view.ViewConfiguration; ?import android.view.ViewParent; ?import android.widget.TextView; +import android.graphics.Rect; ? ?import com.android.launcher3.IconCache.IconLoadRequest; ?import com.android.launcher3.model.PackageItemInfo; @@ -148,12 +150,44 @@ public class BubbleTextView extends TextView ? ? ? ?public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) { ? ? ? ? ?applyFromShortcutInfo(info, iconCache, false); - ? ?} + ? ? ? ? + ? ? ? ?mScript = info.getScript(iconCache); //zengxiao add + ? ? ? ?if(mScript!=null){ + ? ? ? ?if(mScript!=null){ + ? ? ? ?}else{ + ? ? ? ? ? ? ? Log.e("rtyre","info.iconResource.packageName ------null"); + ? ? ? ?} + ? ?} + ? private IconScript mScript; + ? ?@Override ? + ? ?public void setCompoundDrawables(Drawable left, Drawable top, ? + ? ? ? ? ? ?Drawable right, Drawable bottom) { ? + ? ? ?? + ? ? ? ?if(top != null){ ? + ? ? ? ? ? ?? + ? ? ? ? ? ?if(mScript != null){ ? + ? ? ? ? ? ? ? + ? ? ? ? ? ? ? ?top = mScript; ? + ? ? ? ? ? ? ? ?Rect rect=new Rect(0,0,LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize,LauncherAppState.getInstanc + ? ? ? ? ? ? ? ?mScript.setBounds(rect); ? + ? ? ? ? ? ? ? ? ? ? ? ?? + ? ? ? ? ? ? ? ?if(!mScript.isRuning)? + ? ? ? ? ? ? ? ?{ + ? ? ? ? ? ? ? ? ? ? ? ? ? ? + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mScript.run(this); ? + ? ? ? ? ? ? ? ?} + ? ? ? ? ? ?} ? + ? ? ? ?} ? + ? ? ? + ? ? ? ?super.setCompoundDrawables(left, top, right, bottom); ? + ? ?} ? ? ? ? ?public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache, ? ? ? ? ? ? ?boolean promiseStateChanged) { ? ? ? ? ?Bitmap b = info.getIcon(iconCache); - + ? ? ? ?mScript = info.getScript(iconCache);? ? ? ? ? ?FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
--- a/packages/apps/Launcher3/src/com/android/launcher3/ShortcutInfo.java +++ b/packages/apps/Launcher3/src/com/android/launcher3/ShortcutInfo.java @@ -304,5 +304,13 @@ public class ShortcutInfo extends ItemInfo { ? ? ?public boolean isDisabled() { ? ? ? ? ?return isDisabled != 0; ? ? ?} + ? ? + ? ? + ? ?public IconScript getScript(IconCache iconCache){ ? + ? ? ? ?return iconCache.getScript(promisedIntent != null ? promisedIntent : intent, user); ? + ? ?} ? + ? ? ?}
把這些代碼添加上功能基本ok.
有了這個(gè)基礎(chǔ),我想要實(shí)現(xiàn)的效果就變得很簡(jiǎn)單,同樣的定義一個(gè)WallpaperScript繼承IconScript
package com.android.launcher3; ? import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.text.format.Time; import android.view.View; import android.util.Log; import android.graphics.Rect; import android.graphics.Typeface; import android.graphics.Paint; import java.util.Calendar; import android.graphics.BitmapFactory; import android.graphics.Matrix; ? public class WallpaperScript extends IconScript { ? ? ? private float mDensity = 1.5f; ? ? ? Time mTime = new Time(); ? ?? ?int myCount =0; ? ? Context mContext;? ?? ?Boolean isDraw =false; ? ? ?/**? ? ? * 效果展示目標(biāo)View? ? ? */ ? ? ? private View mView;? ?? ? /**? ? ? * 當(dāng)前是否顯示在屏幕上? ? ? */ ? ? ? private boolean mIsShowInScreen = false; ? ?? ? ? /**? ? ? * 通知系統(tǒng)更新視圖現(xiàn)成? ? ? */ ? ?? ?private WallpaperThread mWallpaperThread = null; ? ?? ? ?? ?int[] arr=new int[]{R.drawable.ic_launcher_wallpaper_1,R.drawable.ic_launcher_wallpaper_2,R.drawable.ic_launcher_wallpaper_3, ?? ??? ??? ??? ??? ??? ?R.drawable.ic_launcher_wallpaper_4,R.drawable.ic_launcher_wallpaper_5,R.drawable.ic_launcher_wallpaper_6 ? ? }; ?? ?int index = 0; ? ? public WallpaperScript(Context context) { ? ? ? ? ? super(); ? ? ? ? ? mContext = context; ? ? ? } ?? ?public void run(View view) { ? ? ? ? ?mView = view; ? ? ? ? ?if(mWallpaperThread == null){ ?? ??? ? ? //Log.d("WallpaperScript","mWallpaperThread ?");?? ??? ? ? ? ? ? ? ?mWallpaperThread = new WallpaperThread(); ? ? ? ? ? ? ?mWallpaperThread.start(); ? ? ? ? ?} ?? ? ? ? IntentFilter filter = new IntentFilter(); ? ?? ??? ? filter.addAction("android.intent.action.WallpaperChange"); ? ??? ??? ? ?? ??? ??? ? view.getContext().registerReceiver(new BroadcastReceiver() { ? ? ? ? ? ? ? ? @Override ? ? ? ? ? ? ? ? public void onReceive(Context arg0, Intent arg1) { ? ?? ??? ??? ? ? ? ? Log.d("WallpaperScript","onReceive ?");?? ? ?? ??? ??? ??? ? ? isDraw = true; ? ? ? ? ? ? ? ? ? ?mWallpaperThread.startRun(); ? ?? ??? ??? ??? ? ? mWallpaperThread.start(); ?? ??? ??? ??? ? ?// myCount =0; ? ? ? ? ? ? ? } ? ? ? ? ? ?}, filter); ??? ? ?? ?? ?} ? ? ? ? ? ?@Override ? ? ?public void onPause() { ? ? ? ? ?mWallpaperThread.pauseRun(); ? ? ? ? ?super.onPause(); ? ? ?} ? ? ? ? ? ?@Override ? ? ?public void onResume() { ? ? ? ? ?mWallpaperThread.resumeRun(); ? ? ? ? ?super.onResume(); ? ? ?} ? ? ? ? ? ?@Override ? ? ?public void onStop() { ? ? ? ? ?mWallpaperThread.stopRun(); ? ? ? ? ?super.onStop(); ? ? ?} ? ?? ? ? @Override ? ? ? public void draw(Canvas canvas) { ? ? ? ? ? super.draw(canvas); ? ?? ??? ? ?? ??? ?Bitmap wallpaperIconfirst = Utilities.createIconBitmap( BitmapFactory.decodeResource(mContext.getResources(), arr[0]),mContext); ? ?? ??? ? ? ? ? ? canvas.drawBitmap(wallpaperIconfirst, null, getBounds(),mPaint);//默認(rèn)顯示的圖片? ?? ? ? ?if(isDraw){ ?? ??? ??? ? ?? ??? ??? ?index = index%6; ?? ??? ??? ? ?? ??? ??? ?Bitmap wallpaperIcon = Utilities.createIconBitmap( BitmapFactory.decodeResource(mContext.getResources(), arr[index]),mContext); ? ?? ??? ? ? ? ?? ??? ??? ?index = index+1; ?? ??? ??? ? ?? ??? ? ?? ??? ??? ?myCount =myCount+1; ? ? ? ? ?? ? ?? ??? ??? ?canvas.drawBitmap(wallpaperIcon, null, getBounds(),mPaint);//畫(huà)底圖 ? ?? ??? ? ?? ? ?? ?? ??? ??? ?//Log.d("WallpaperScript","WallpaperScript index "+index+" myCount "+myCount); ?? ??? ?? ?? ??? ??? ?if(myCount==49){ ?? ??? ??? ??? ?myCount=0; ?? ??? ??? ??? ?mWallpaperThread.stopRun(); ?? ??? ??? ??? ?//Log.d("WallpaperScript","WallpaperScript myCount " +myCount); ?? ??? ??? ??? ?isDraw = false; ?? ??? ??? ?}?? ? ?? ??? ??? ? ?? ??? ?} ?? ??? ? ? ? } ? ? ?? ? ? class WallpaperThread extends Thread { ? ? ? ? ?int times = 0; ? ? ? ? ?boolean running = true; ? ? ? ? ? ?public boolean wait = false; ? ?? ? ?? ?? ? ? ?public void startRun() { ? ? ? ? ? ? ?running = true; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?}? ? ? ? ?}; ? ? ? ? ? ?public void stopRun() { ? ? ? ? ? ? ?running = false; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?}? ? ? ? ?}; ? ? ? ? ? ?public void pauseRun() { ? ? ? ? ? ? ?/*this.wait = true; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?} */? ? ? ? ?} ? ? ? ? ? ?public void resumeRun() { ? ? ? ? ? ? ?/*this.wait = false; ? ? ? ? ? ? ?synchronized (this) { ? ? ? ? ? ? ? ? ?this.notify(); ? ? ? ? ? ? ?}*/ ? ? ? ? ?} ? ? ? ? ?? ? ? ? ?public void run() {? ?? ??? ? ? Log.d("WallpaperScript","WallpaperThread running "+ running);?? ??? ? ? ? ? ? ? ?while (running) { ? ? ? ? ? ? ? ? ?synchronized (mView) { ? ? ? ? ? ? ? ? ? ?Log.d("WallpaperScript","WallpaperThread run()");?? ??? ??? ??? ? ?? ? ? ? ? ? ? ? ? ? ?mView.postInvalidate(); ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ?try { ? ? ? ? ? ? ? ? ? ? ?Thread.sleep(50); ? ? ? ? ? ? ? ? ?} catch (Exception e) { ? ? ? ? ? ? ? ? ? ? ?System.out.println(e); ? ? ? ? ? ? ? ? ?} ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ? ? ? ? ? ?} ? ? ? ? ?} ? ? ?} ? ?? } ?
把所有的圖片放到一個(gè)數(shù)組里面,然后輪流去繪制里面的圖片,點(diǎn)擊圖標(biāo)的時(shí)候會(huì)發(fā)送一個(gè)廣播,通過(guò)廣播去控制線程的開(kāi)啟,這樣功能基本上實(shí)現(xiàn)。
另外,怎樣去實(shí)現(xiàn)沒(méi)有界面的app,這個(gè)只需要AndroidManifest設(shè)置。
android:theme="@android:style/Theme.NoDisplay"
切換鎖屏壁紙和主屏幕壁紙的代碼
WallpaperManager manager = WallpaperManager.getInstance(this); try { ? ? manager.setBitmap(bitmap,null, true, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM); } catch (Exception e) { ? ? e.printStackTrace(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
- Android中利用matrix 控制圖片的旋轉(zhuǎn)、縮放、移動(dòng)
- Android Tween動(dòng)畫(huà)之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
- Android開(kāi)發(fā) 旋轉(zhuǎn)屏幕導(dǎo)致Activity重建解決方法
- Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android編程中調(diào)用Camera時(shí)預(yù)覽畫(huà)面有旋轉(zhuǎn)問(wèn)題的解決方法
- Android開(kāi)發(fā)之圖形圖像與動(dòng)畫(huà)(二)Animation實(shí)現(xiàn)圖像的漸變/縮放/位移/旋轉(zhuǎn)
- Android 圖片縮放與旋轉(zhuǎn)的實(shí)現(xiàn)詳解
- Android編程實(shí)現(xiàn)RotateAnimation設(shè)置中心點(diǎn)旋轉(zhuǎn)動(dòng)畫(huà)效果
- Android部分手機(jī)拍照后獲取的圖片被旋轉(zhuǎn)問(wèn)題的解決方法
相關(guān)文章
android之json數(shù)據(jù)過(guò)長(zhǎng)打印不全問(wèn)題的解決
這篇文章主要介紹了android之json數(shù)據(jù)過(guò)長(zhǎng)打印不全問(wèn)題的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04基于android實(shí)現(xiàn)五子棋開(kāi)發(fā)
這篇文章主要為大家詳細(xì)介紹了基于android實(shí)現(xiàn)五子棋開(kāi)發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02android Animation監(jiān)聽(tīng)器AnimationListener的使用方法)
AnimaitonListener的使用方法主要是在Animation上設(shè)置一個(gè)監(jiān)聽(tīng)器,下面通過(guò)一個(gè)實(shí)例說(shuō)明它的使用方法2013-11-11Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架功能
這篇文章主要介紹了Jetpack?Compose?實(shí)現(xiàn)一個(gè)圖片選擇框架,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要介紹了Android基于CountDownTimer實(shí)現(xiàn)倒計(jì)時(shí)功能,簡(jiǎn)單分析了基于CountDownTimer類(lèi)實(shí)現(xiàn)倒計(jì)時(shí)功能的技巧,需要的朋友可以參考下2015-12-12Flutter學(xué)習(xí)之SliverList和SliverGird的使用詳解
Sliver的組件一般都用在CustomScrollView中,除了SliverAppBar之外,我們還可以為CustomScrollView添加List或者Grid來(lái)實(shí)現(xiàn)更加復(fù)雜的組合效果。本文就來(lái)聊聊SliverList和SliverGird的使用吧2023-02-02實(shí)例講解Android中的AIDL內(nèi)部進(jìn)程通信接口使用
這篇文章主要通過(guò)實(shí)例介紹了Android中的AIDL內(nèi)部進(jìn)程通信接口使用,文中通過(guò)一個(gè)音樂(lè)播放的服務(wù)編寫(xiě)例子來(lái)講解AIDL的傳遞對(duì)象及一般使用步驟,需要的朋友可以參考下2016-04-04