经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
android实现icon动态旋转效果
来源:jb51  时间:2022/7/20 13:09:25  对本文有异议

本文实例为大家分享了android实现icon动态旋转效果的具体代码,供大家参考,具体内容如下

碰到客户的这样一个需求,点击icon后,前景的icon开始旋转,背景的icon不动,就是这样一个效果

通过第三方的方法是不可能实现的,我这里是通过修改系统launcher的代码来实现。实现思路是在launcher中找到显示icon图标代码,并把这个图标覆盖掉。很多第手机的时钟icon是可以动态变化的,好在公司已经有人实现这个功能,可以借鉴

我这里先把时钟动态icon的实现说明下,需要的朋友可以参考。

写一个IconScript的基类继承Drawable

  1. package com.android.launcher3;
  2. ?
  3. import android.graphics.Canvas;
  4. import android.graphics.ColorFilter;
  5. import android.graphics.Paint;
  6. import android.graphics.drawable.Drawable;
  7. import android.view.View;
  8. import android.util.Log;
  9. ?
  10. public class IconScript extends Drawable{ ?
  11. ? ? public boolean isRuning = false; ?
  12. ? ? public FastBitmapDrawable mFastBitmapDrawable = null; ?
  13. ? ? protected Paint mPaint = new Paint(); ?
  14. ? ? ??
  15. ? ? public IconScript(){ ?
  16. ? ? ? ? mPaint.setAntiAlias(true); ??
  17. ? ? ? ? mPaint.setFilterBitmap(true); ?
  18. ? ? } ?
  19. ? ? ??
  20. ? ? public void draw(Canvas canvas){ ?
  21. ? ? ? ? if(mFastBitmapDrawable != null){ ?
  22. ?? ??? ? ? ?Log.e("fly","IconScript=");
  23. ? ? ? ? ? ? canvas.drawBitmap(mFastBitmapDrawable.getBitmap(), null, getBounds(),mPaint);//画底图 ?
  24. ? ? ? ? } ?
  25. ? ? } ?
  26. ? ? ??
  27. ? ? /**?
  28. ? ? ?* 运行脚本?
  29. ? ? ?* @param view?
  30. ? ? ?*/ ?
  31. ? ? public void run(View view){ ?
  32. ? ? ? ? isRuning = true; ?
  33. ? ? } ?
  34. ? ? ??
  35. ? ? /**?
  36. ? ? ?* 停止脚本?
  37. ? ? ?* (未调用,暂留入口)?
  38. ? ? ?*/ ?
  39. ? ? public void onStop(){ ?
  40. ? ? ? ? isRuning = false; ?
  41. ? ? } ?
  42. ? ? ??
  43. ? ? /**?
  44. ? ? ?* 暂停脚本?
  45. ? ? ?* (未调用,暂留入口)?
  46. ? ? ?*/ ?
  47. ? ? public void onPause(){ ?
  48. ? ? ? ? isRuning = false; ?
  49. ? ? } ?
  50. ? ? ??
  51. ? ? /**?
  52. ? ? ?* 恢复脚本?
  53. ? ? ?* (未调用,暂留入口)?
  54. ? ? ?*/ ?
  55. ? ? public void onResume(){ ?
  56. ? ? ? ? isRuning = true; ?
  57. ? ? } ?
  58. ??
  59. ? ? @Override ?
  60. ? ? public int getOpacity() { ?
  61. ? ? ? ? // TODO Auto-generated method stub ?
  62. ? ? ? ? return 0; ?
  63. ? ? } ?
  64. ??
  65. ? ? @Override ?
  66. ? ? public void setAlpha(int arg0) { ?
  67. ? ? ? ? // TODO Auto-generated method stub ?
  68. ? ? ? ? ??
  69. ? ? } ?
  70. ??
  71. ? ? @Override ?
  72. ? ? public void setColorFilter(ColorFilter arg0) { ?
  73. ? ? ? ? // TODO Auto-generated method stub ?
  74. ? ? ? ? ??
  75. ? ? } ?
  76. ? ? ??
  77. ? ? @Override ?
  78. ? ? public int getIntrinsicWidth() { ?
  79. ? ? ? ? int width = getBounds().width(); ?
  80. ? ? ? ? if (width == 0) { ?
  81. ? ? ? ? ? ? width = mFastBitmapDrawable.getBitmap().getWidth(); ?
  82. ? ? ? ? } ?
  83. ? ? ? ? return width; ?
  84. ? ? } ?
  85. ??
  86. ? ? @Override ?
  87. ? ? public int getIntrinsicHeight() { ?
  88. ? ? ? ? int height = getBounds().height(); ?
  89. ? ? ? ? if (height == 0) { ?
  90. ? ? ? ? ? ? height = mFastBitmapDrawable.getBitmap().getHeight(); ?
  91. ? ? ? ? } ?
  92. ? ? ? ? return height; ?
  93. ? ? } ?
  94. ??
  95. ? ? @Override ?
  96. ? ? public int getMinimumWidth() { ?
  97. ? ? ? ? return getBounds().width(); ?
  98. ? ? } ?
  99. ??
  100. ? ? @Override ?
  101. ? ? public int getMinimumHeight() { ?
  102. ? ? ? ? return getBounds().height(); ?
  103. ? ? } ?
  104. ? ? ??
  105. ? ? @Override ?
  106. ? ? public void setFilterBitmap(boolean filterBitmap) { ?
  107. ? ? ? ? mPaint.setFilterBitmap(filterBitmap); ?
  108. ? ? ? ? mPaint.setAntiAlias(filterBitmap); ?
  109. ? ? } ?
  110. ? ? ??
  111. ? ? public void setFastBitmapDrawable(FastBitmapDrawable drawable){ ?
  112. ? ? ? ? mFastBitmapDrawable = drawable; ?
  113. ? ? } ?
  114. ? ? ??
  115. ? ? public FastBitmapDrawable setFastBitmapDrawable(){ ?
  116. ? ? ? ? return mFastBitmapDrawable; ?
  117. ? ? } ?
  118. } ?

核心类ClockScript继承IconScript

  1. package com.android.launcher3;
  2. ?
  3. ?
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Rect;
  8. import android.text.format.Time;
  9. import android.view.View;
  10. import android.content.Context;
  11. import android.graphics.Bitmap;
  12. import android.graphics.BitmapFactory;
  13. ?
  14. ?
  15. ?
  16. ?
  17. public class ClockScript extends IconScript { ?
  18. ? ?Rect mRect = null; ?
  19. ? ?/**?
  20. ? ? * 效果展示目标View?
  21. ? ? */ ?
  22. ? ?private View mView; ?
  23. ? ?/**?
  24. ? ? * 通知系统更新视图现成?
  25. ? ? */ ?
  26. ? ?private ClockThread mClockThread = null; ?
  27. ? ?/**?
  28. ? ? * 当前是否显示在屏幕上?
  29. ? ? */ ?
  30. ? ?private boolean mIsShowInScreen = false; ?
  31. ? ?Context mContext;?
  32. ?
  33. ?
  34. ? ?public ClockScript(Context context){ ?
  35. ? ? ? ?super(); ?
  36. ? ? mContext = context; ?
  37. ? ?} ?
  38. ? ?public void run(View view) { ?
  39. ? ? ? ?mView = view; ?
  40. ? ? ? ?mRect = getBounds(); ?
  41. ? ? ? ?if(mClockThread == null){ ?
  42. ? ? ? ? ? ?mClockThread = new ClockThread(); ?
  43. ? ? ? ? ? ?mClockThread.start(); ?
  44. ? ? ? ?} ?
  45. ? ?} ?
  46. ? ? ?
  47. ? ?@Override ?
  48. ? ?public void onPause() { ?
  49. ? ? ? ?mClockThread.pauseRun(); ?
  50. ? ? ? ?super.onPause(); ?
  51. ? ?} ?
  52. ? ? ?
  53. ? ?@Override ?
  54. ? ?public void onResume() { ?
  55. ? ? ? ?mClockThread.resumeRun(); ?
  56. ? ? ? ?super.onResume(); ?
  57. ? ?} ?
  58. ? ? ?
  59. ? ?@Override ?
  60. ? ?public void onStop() { ?
  61. ? ? ? ?mClockThread.stopRun(); ?
  62. ? ? ? ?super.onStop(); ?
  63. ? ?} ?
  64. ? ? ?
  65. ?
  66. ? ?@Override ?
  67. ? ?public void draw(Canvas canvas) { ?
  68. ? ? ? ?super.draw(canvas); ?
  69. ? ? ? ?mIsShowInScreen = true; ?
  70. ?
  71. ?
  72. ? ? ? ?drawIndicator(canvas,mRect.centerX(),mRect.centerY(),mPaint); ?
  73. ?
  74. ?
  75. ? ? ? ?if(mClockThread.wait){ ?
  76. ? ? ? ? ? ?mClockThread.resumeRun(); ?
  77. ? ? ? ?} ?
  78. ? ?} ?
  79. ? ?/**?
  80. ? ? * 画指针?
  81. ? ? * @param canvas?
  82. ? ? * @param centerX?
  83. ? ? * @param centerY?
  84. ? ? * @param p?
  85. ? ? */ ?
  86. ?private void drawIndicator(Canvas canvas,int centerX,int centerY,Paint p){ ?
  87. ? ? ? ? ?
  88. ? ? Bitmap clockIcon = Utilities.createIconBitmap( BitmapFactory.decodeResource(mContext.getResources(), R.drawable.ic_launcher_clock),mContext); ?
  89. ?
  90. ?
  91. ?
  92. ? ? int X = clockIcon.getWidth()/2;
  93. ? ? int Y = clockIcon.getHeight()/2;
  94. ? ? canvas.drawBitmap(clockIcon, null, getBounds(),p);//画底图 ?
  95. ?
  96. ? ? Time t=new Time();
  97. ? ? ?t.setToNow();?
  98. ? ? p.setAntiAlias(true);
  99. ? ? p.setStrokeWidth(3);
  100. ? ? p.setColor(Color.WHITE);
  101. ? ? p.setStyle(Paint.Style.FILL);
  102. ?
  103. ?
  104. //hour
  105. 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);
  106. //minute
  107. 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);
  108. //second
  109. p.setColor(Color.RED);
  110. p.setStrokeWidth(1);
  111. p.setStyle(Paint.Style.FILL);
  112. 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);
  113. ? ? ??
  114. p.setColor(Color.WHITE);
  115. canvas.drawCircle(X, Y, 4, p);
  116. p.setColor(Color.GRAY);
  117. canvas.drawCircle(X, Y, 2, p);
  118. ?
  119. } ?
  120. ? ? ?
  121. ? ?class ClockThread extends Thread { ?
  122. ? ? ? ?int times = 0; ?
  123. ? ? ? ?boolean running = true; ?
  124. ?
  125. ? ? ? ?public boolean wait = false; ?
  126. ?
  127. ? ? ? ?public void stopRun() { ?
  128. ? ? ? ? ? ?running = false; ?
  129. ? ? ? ? ? ?synchronized (this) { ?
  130. ? ? ? ? ? ? ? ?this.notify(); ?
  131. ? ? ? ? ? ?} ?
  132. ? ? ? ?}; ?
  133. ?
  134. ? ? ? ?public void pauseRun() { ?
  135. ? ? ? ? ? ?this.wait = true; ?
  136. ? ? ? ? ? ?synchronized (this) { ?
  137. ? ? ? ? ? ? ? ?this.notify(); ?
  138. ? ? ? ? ? ?} ?
  139. ? ? ? ?} ?
  140. ?
  141. ? ? ? ?public void resumeRun() { ?
  142. ? ? ? ? ? ?this.wait = false; ?
  143. ? ? ? ? ? ?synchronized (this) { ?
  144. ? ? ? ? ? ? ? ?this.notify(); ?
  145. ? ? ? ? ? ?} ?
  146. ? ? ? ?} ?
  147. ?
  148. ? ? ? ?public void run() { ?
  149. ? ? ? ? ? ?while (running) { ?
  150. ? ? ? ? ? ? ? ?synchronized (mView) { ?
  151. ? ? ? ? ? ? ? ? ? ?mView.postInvalidate(); ?
  152. ? ? ? ? ? ? ? ?} ?
  153. ? ? ? ? ? ? ? ? ?
  154. ? ? ? ? ? ? ? ?if(!mIsShowInScreen){ ?
  155. ? ? ? ? ? ? ? ? ? ?pauseRun(); ?
  156. ? ? ? ? ? ? ? ?} ?
  157. ? ? ? ? ? ? ? ?mIsShowInScreen = false; ?
  158. ? ? ? ? ? ? ? ?try { ?
  159. ? ? ? ? ? ? ? ? ? ?Thread.sleep(500); ?
  160. ? ? ? ? ? ? ? ?} catch (Exception e) { ?
  161. ? ? ? ? ? ? ? ? ? ?System.out.println(e); ?
  162. ? ? ? ? ? ? ? ?} ?
  163. ? ? ? ? ? ? ? ? ?
  164. ? ? ? ? ? ? ? ?synchronized (this) { ?
  165. ? ? ? ? ? ? ? ? ? ?if (wait) { ?
  166. ? ? ? ? ? ? ? ? ? ? ? ?try { ?
  167. ? ? ? ? ? ? ? ? ? ? ? ? ? ?wait(); ?
  168. ? ? ? ? ? ? ? ? ? ? ? ?} catch (InterruptedException e) { ?
  169. ? ? ? ? ? ? ? ? ? ? ? ? ? ?// TODO Auto-generated catch block ?
  170. ? ? ? ? ? ? ? ? ? ? ? ? ? ?e.printStackTrace(); ?
  171. ? ? ? ? ? ? ? ? ? ? ? ?} ?
  172. ? ? ? ? ? ? ? ? ? ?} ?
  173. ? ? ? ? ? ? ? ?} ?
  174. ? ? ? ? ? ?} ?
  175. ? ? ? ?} ?
  176. ? ?} ?
  177. ?
  178. } ?

接下来就是如何初始化这个ClockScript,在Icon类里面添加代码

  1. --- a/packages/apps/Launcher3/src/com/android/launcher3/IconCache.java
  2. +++ b/packages/apps/Launcher3/src/com/android/launcher3/IconCache.java
  3. @@ -84,6 +84,7 @@ public class IconCache {
  4. ? ? ? ? ?public CharSequence title = "";
  5. ? ? ? ? ?public CharSequence contentDescription = "";
  6. ? ? ? ? ?public boolean isLowResIcon;
  7. + ? ? ? ?public IconScript script;
  8. ? ? ?}
  9. ?
  10. ? ? ?private final HashMap<UserHandleCompat, Bitmap> mDefaultIcons = new HashMap<>();
  11. @@ -591,7 +592,17 @@ public class IconCache {
  12. ? ? ? ? if (info != null) {
  13. ? ? ? ? ? ? entry.title = info.getLabel();
  14. ? ? ? ? }
  15. -
  16. + ? ? ?
  17. + ? ? ? Log.e("IconCache ","componentName.getPackageName()="+componentName.getPackageName());
  18. ? ? ? ? //加了一个系统的属性来控制
  19. + ? ? ? if(null != entry && componentName.getPackageName().equals("com.android.deskclock") ?&& android.os.SystemProperties.getBoolean("launcher.calender.updateicon", false))
  20. + ? ? ? { ? ? ? ?
  21. + ? ? ? ? ?Log.e("IconCache","clock init");
  22. + ? ? ? ? ? entry.script = new ClockScript(mContext); ?
  23. + ? ? ? }
  24. ?
  25. ? ? ? ? ?return entry;
  26. ? ? ?}
  27. ?
  28. @@ -891,4 +902,20 @@ public class IconCache {
  29. ? ? ? ? ? ? ?return null;
  30. ? ? ? ? ?}
  31. ? ? ?}
  32. + ? ?public IconScript getScript(Intent intent, UserHandleCompat user){ ?
  33. + ? ? ? ?synchronized (mCache) { ?
  34. + ? ? ? ? ? ? ComponentName component = intent.getComponent(); ?
  35. + ? ? ? ? ? ?
  36. + ? ? ? ? ? ? if (component == null) { ?
  37. + ? ? ? ? ? ? ? ?Log.e("IconCache ","component==null");
  38. + ? ? ? ? ? ? ? ? return null; ?
  39. + ? ? ? ? ? ? } ?
  40. + ? ? ? ? ? ?LauncherActivityInfoCompat launcherActInfo = mLauncherApps.resolveActivity(intent, user); ?
  41. + ? ? ? ? ? ?CacheEntry entry = cacheLocked(component, launcherActInfo,user, false, false); ?
  42. + ? ? ? ? ? ?return entry.script; ?
  43. + ? ? ? ?} ?
  44. + ? ?} ?

在BubbleTextView类中添加代码

  1. --- a/packages/apps/Launcher3/src/com/android/launcher3/BubbleTextView.java
  2. +++ b/packages/apps/Launcher3/src/com/android/launcher3/BubbleTextView.java
  3. @@ -30,6 +30,7 @@ import android.graphics.drawable.ColorDrawable;
  4. ?import android.graphics.drawable.Drawable;
  5. ?import android.os.Build;
  6. ?import android.util.AttributeSet;
  7. +import android.util.Log;
  8. ?import android.util.SparseArray;
  9. ?import android.util.TypedValue;
  10. ?import android.view.KeyEvent;
  11. @@ -38,6 +39,7 @@ import android.view.View;
  12. ?import android.view.ViewConfiguration;
  13. ?import android.view.ViewParent;
  14. ?import android.widget.TextView;
  15. +import android.graphics.Rect;
  16. ?
  17. ?import com.android.launcher3.IconCache.IconLoadRequest;
  18. ?import com.android.launcher3.model.PackageItemInfo;
  19. @@ -148,12 +150,44 @@ public class BubbleTextView extends TextView
  20. ?
  21. ? ? ?public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache) {
  22. ? ? ? ? ?applyFromShortcutInfo(info, iconCache, false);
  23. - ? ?}
  24. + ? ? ? ?
  25. + ? ? ? ?mScript = info.getScript(iconCache); //zengxiao add
  26. + ? ? ? ?if(mScript!=null){
  27. + ? ? ? ?if(mScript!=null){
  28. + ? ? ? ?}else{
  29. + ? ? ? ? ? ? ? Log.e("rtyre","info.iconResource.packageName ------null");
  30. + ? ? ? ?}
  31. + ? ?}
  32. + ? private IconScript mScript;
  33. + ? ?@Override ?
  34. + ? ?public void setCompoundDrawables(Drawable left, Drawable top, ?
  35. + ? ? ? ? ? ?Drawable right, Drawable bottom) { ?
  36. + ? ? ??
  37. + ? ? ? ?if(top != null){ ?
  38. + ? ? ? ? ? ??
  39. + ? ? ? ? ? ?if(mScript != null){ ?
  40. + ? ? ? ? ? ? ?
  41. + ? ? ? ? ? ? ? ?top = mScript; ?
  42. + ? ? ? ? ? ? ? ?Rect rect=new Rect(0,0,LauncherAppState.getInstance().getInvariantDeviceProfile().iconBitmapSize,LauncherAppState.getInstanc
  43. + ? ? ? ? ? ? ? ?mScript.setBounds(rect); ?
  44. + ? ? ? ? ? ? ? ? ? ? ? ??
  45. + ? ? ? ? ? ? ? ?if(!mScript.isRuning)?
  46. + ? ? ? ? ? ? ? ?{
  47. + ? ? ? ? ? ? ? ? ? ? ? ? ? ?
  48. + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? mScript.run(this); ?
  49. + ? ? ? ? ? ? ? ?}
  50. + ? ? ? ? ? ?} ?
  51. + ? ? ? ?} ?
  52. + ? ? ?
  53. + ? ? ? ?super.setCompoundDrawables(left, top, right, bottom); ?
  54. + ? ?} ?
  55. ?
  56. ? ? ?public void applyFromShortcutInfo(ShortcutInfo info, IconCache iconCache,
  57. ? ? ? ? ? ? ?boolean promiseStateChanged) {
  58. ? ? ? ? ?Bitmap b = info.getIcon(iconCache);
  59. -
  60. + ? ? ? ?mScript = info.getScript(iconCache);?
  61. ? ? ? ? ?FastBitmapDrawable iconDrawable = mLauncher.createIconDrawable(b);
  1. --- a/packages/apps/Launcher3/src/com/android/launcher3/ShortcutInfo.java
  2. +++ b/packages/apps/Launcher3/src/com/android/launcher3/ShortcutInfo.java
  3. @@ -304,5 +304,13 @@ public class ShortcutInfo extends ItemInfo {
  4. ? ? ?public boolean isDisabled() {
  5. ? ? ? ? ?return isDisabled != 0;
  6. ? ? ?}
  7. + ? ?
  8. + ? ?
  9. + ? ?public IconScript getScript(IconCache iconCache){ ?
  10. + ? ? ? ?return iconCache.getScript(promisedIntent != null ? promisedIntent : intent, user); ?
  11. + ? ?} ?
  12. + ? ?
  13. ?}

把这些代码添加上功能基本ok.

有了这个基础,我想要实现的效果就变得很简单,同样的定义一个WallpaperScript继承IconScript

  1. package com.android.launcher3;
  2. ?
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.IntentFilter;
  7. import android.graphics.Bitmap;
  8. import android.graphics.Canvas;
  9. import android.graphics.drawable.BitmapDrawable;
  10. import android.text.format.Time;
  11. import android.view.View;
  12. import android.util.Log;
  13. import android.graphics.Rect;
  14. import android.graphics.Typeface;
  15. import android.graphics.Paint;
  16. import java.util.Calendar;
  17. import android.graphics.BitmapFactory;
  18. import android.graphics.Matrix;
  19. ?
  20. public class WallpaperScript extends IconScript { ?
  21. ? ? private float mDensity = 1.5f; ?
  22. ? ? Time mTime = new Time(); ?
  23. ?? ?int myCount =0;
  24. ? ? Context mContext;?
  25. ?? ?Boolean isDraw =false;
  26. ? ? ?/**?
  27. ? ? * 效果展示目标View?
  28. ? ? */ ?
  29. ? ? private View mView;?
  30. ?? ? /**?
  31. ? ? * 当前是否显示在屏幕上?
  32. ? ? */ ?
  33. ? ? private boolean mIsShowInScreen = false; ?
  34. ?? ? ? /**?
  35. ? ? * 通知系统更新视图现成?
  36. ? ? */ ?
  37. ?? ?private WallpaperThread mWallpaperThread = null; ?
  38. ?? ?
  39. ?? ?int[] arr=new int[]{R.drawable.ic_launcher_wallpaper_1,R.drawable.ic_launcher_wallpaper_2,R.drawable.ic_launcher_wallpaper_3,
  40. ?? ??? ??? ??? ??? ??? ?R.drawable.ic_launcher_wallpaper_4,R.drawable.ic_launcher_wallpaper_5,R.drawable.ic_launcher_wallpaper_6
  41. ? ? };
  42. ?? ?int index = 0;
  43. ? ? public WallpaperScript(Context context) { ?
  44. ? ? ? ? super(); ?
  45. ? ? ? ? mContext = context; ?
  46. ? ? }
  47. ?? ?public void run(View view) { ?
  48. ? ? ? ?mView = view; ?
  49. ? ? ? ?if(mWallpaperThread == null){
  50. ?? ??? ? ? //Log.d("WallpaperScript","mWallpaperThread ?");?? ??? ?
  51. ? ? ? ? ? ?mWallpaperThread = new WallpaperThread(); ?
  52. ? ? ? ? ? ?mWallpaperThread.start(); ?
  53. ? ? ? ?}
  54. ?? ? ? ? IntentFilter filter = new IntentFilter(); ?
  55. ?? ??? ? filter.addAction("android.intent.action.WallpaperChange"); ? ??? ??? ?
  56. ?? ??? ??? ? view.getContext().registerReceiver(new BroadcastReceiver() { ?
  57. ? ? ? ? ? ? ? @Override ?
  58. ? ? ? ? ? ? ? public void onReceive(Context arg0, Intent arg1) { ?
  59. ?? ??? ??? ? ? ? ? Log.d("WallpaperScript","onReceive ?");?? ?
  60. ?? ??? ??? ??? ? ? isDraw = true;
  61. ? ? ? ? ? ? ? ? ? ?mWallpaperThread.startRun(); ?
  62. ?? ??? ??? ??? ? ? mWallpaperThread.start();
  63. ?? ??? ??? ??? ? ?// myCount =0;
  64. ? ? ? ? ? ? ? } ?
  65. ? ? ? ? ?}, filter); ??? ? ??
  66. ?? ?} ?
  67. ? ? ?
  68. ? ?@Override ?
  69. ? ?public void onPause() { ?
  70. ? ? ? ?mWallpaperThread.pauseRun(); ?
  71. ? ? ? ?super.onPause(); ?
  72. ? ?} ?
  73. ? ? ?
  74. ? ?@Override ?
  75. ? ?public void onResume() { ?
  76. ? ? ? ?mWallpaperThread.resumeRun(); ?
  77. ? ? ? ?super.onResume(); ?
  78. ? ?} ?
  79. ? ? ?
  80. ? ?@Override ?
  81. ? ?public void onStop() { ?
  82. ? ? ? ?mWallpaperThread.stopRun(); ?
  83. ? ? ? ?super.onStop(); ?
  84. ? ?} ?
  85. ??
  86. ? ? @Override ?
  87. ? ? public void draw(Canvas canvas) { ?
  88. ? ? ? ? super.draw(canvas); ?
  89. ?? ??? ?
  90. ?? ??? ?Bitmap wallpaperIconfirst = Utilities.createIconBitmap( BitmapFactory.decodeResource(mContext.getResources(), arr[0]),mContext); ?
  91. ?? ??? ?
  92. ? ? ? ? canvas.drawBitmap(wallpaperIconfirst, null, getBounds(),mPaint);//默认显示的图片?
  93. ?? ? ? ?if(isDraw){
  94. ?? ??? ??? ?
  95. ?? ??? ??? ?index = index%6;
  96. ?? ??? ??? ?
  97. ?? ??? ??? ?Bitmap wallpaperIcon = Utilities.createIconBitmap( BitmapFactory.decodeResource(mContext.getResources(), arr[index]),mContext); ?
  98. ?? ??? ? ? ?
  99. ?? ??? ??? ?index = index+1;
  100. ?? ??? ??? ?
  101. ?? ??? ?
  102. ?? ??? ??? ?myCount =myCount+1;
  103. ? ? ? ? ?? ?
  104. ?? ??? ??? ?canvas.drawBitmap(wallpaperIcon, null, getBounds(),mPaint);//画底图 ?
  105. ?? ??? ?
  106. ?? ? ??
  107. ?? ??? ??? ?//Log.d("WallpaperScript","WallpaperScript index "+index+" myCount "+myCount);
  108. ?? ??? ??
  109. ?? ??? ??? ?if(myCount==49){
  110. ?? ??? ??? ??? ?myCount=0;
  111. ?? ??? ??? ??? ?mWallpaperThread.stopRun();
  112. ?? ??? ??? ??? ?//Log.d("WallpaperScript","WallpaperScript myCount " +myCount);
  113. ?? ??? ??? ??? ?isDraw = false;
  114. ?? ??? ??? ?}?? ?
  115. ?? ??? ??? ?
  116. ?? ??? ?}
  117. ?? ??? ?
  118. ? ? }
  119. ? ? ??
  120. ? ? class WallpaperThread extends Thread { ?
  121. ? ? ? ?int times = 0; ?
  122. ? ? ? ?boolean running = true; ?
  123. ?
  124. ? ? ? ?public boolean wait = false; ?
  125. ?? ? ??
  126. ?? ? ? ?public void startRun() { ?
  127. ? ? ? ? ? ?running = true; ?
  128. ? ? ? ? ? ?synchronized (this) { ?
  129. ? ? ? ? ? ? ? ?this.notify(); ?
  130. ? ? ? ? ? ?}?
  131. ? ? ? ?}; ?
  132. ?
  133. ? ? ? ?public void stopRun() { ?
  134. ? ? ? ? ? ?running = false; ?
  135. ? ? ? ? ? ?synchronized (this) { ?
  136. ? ? ? ? ? ? ? ?this.notify(); ?
  137. ? ? ? ? ? ?}?
  138. ? ? ? ?}; ?
  139. ?
  140. ? ? ? ?public void pauseRun() { ?
  141. ? ? ? ? ? ?/*this.wait = true; ?
  142. ? ? ? ? ? ?synchronized (this) { ?
  143. ? ? ? ? ? ? ? ?this.notify(); ?
  144. ? ? ? ? ? ?} */?
  145. ? ? ? ?} ?
  146. ?
  147. ? ? ? ?public void resumeRun() { ?
  148. ? ? ? ? ? ?/*this.wait = false; ?
  149. ? ? ? ? ? ?synchronized (this) { ?
  150. ? ? ? ? ? ? ? ?this.notify(); ?
  151. ? ? ? ? ? ?}*/ ?
  152. ? ? ? ?} ?
  153. ? ? ? ??
  154. ? ? ? ?public void run() {?
  155. ?? ??? ? ? Log.d("WallpaperScript","WallpaperThread running "+ running);?? ??? ?
  156. ? ? ? ? ? ?while (running) { ?
  157. ? ? ? ? ? ? ? ?synchronized (mView) {
  158. ? ? ? ? ? ? ? ? ? ?Log.d("WallpaperScript","WallpaperThread run()");?? ??? ??? ??? ? ??
  159. ? ? ? ? ? ? ? ? ? ?mView.postInvalidate(); ?
  160. ? ? ? ? ? ? ? ?} ?
  161. ? ? ? ? ? ? ? ?try { ?
  162. ? ? ? ? ? ? ? ? ? ?Thread.sleep(50); ?
  163. ? ? ? ? ? ? ? ?} catch (Exception e) { ?
  164. ? ? ? ? ? ? ? ? ? ?System.out.println(e); ?
  165. ? ? ? ? ? ? ? ?} ?
  166. ? ? ? ? ? ? ? ? ?
  167. ? ? ? ? ? ??
  168. ? ? ? ? ? ?} ?
  169. ? ? ? ?} ?
  170. ? ?} ?
  171. ??
  172. } ?

把所有的图片放到一个数组里面,然后轮流去绘制里面的图片,点击图标的时候会发送一个广播,通过广播去控制线程的开启,这样功能基本上实现。

另外,怎样去实现没有界面的app,这个只需要AndroidManifest设置。

  1. android:theme="@android:style/Theme.NoDisplay"

切换锁屏壁纸和主屏幕壁纸的代码

  1. WallpaperManager manager = WallpaperManager.getInstance(this);
  2. try {
  3. ? ? manager.setBitmap(bitmap,null, true, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
  4. } catch (Exception e) {
  5. ? ? e.printStackTrace();
  6. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号