广告
返回顶部
首页 > 资讯 > 移动开发 >android实现icon动态旋转效果
  • 489
分享到

android实现icon动态旋转效果

2024-04-02 19:04:59 489人浏览 安东尼
摘要

本文实例为大家分享了Android实现icon动态旋转效果的具体代码,供大家参考,具体内容如下 碰到客户的这样一个需求,点击icon后,前景的icon开始旋转,背景的icon不动,就

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

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

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

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

写一个IconScript的基类继承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);//画底图  
        }  
    }  
      
      
    public void run(View view){  
        isRuning = true;  
    }  
      
      
    public void onStop(){  
        isRuning = false;  
    }  
      
      
    public void onPause(){  
        isRuning = false;  
    }  
      
      
    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;  
    }  
}  

核心类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;  
     
   private View mView;  
     
   private ClockThread mClockThread = null;  
     
   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();  
       }  
   }  
     
 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);//画底图  
 
    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();  
                       }  
                   }  
               }  
           }  
       }  
   }  
 
}  

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

--- 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());
        //加了一个系统的属性来控制
+       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类中添加代码

--- 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.

有了这个基础,我想要实现的效果就变得很简单,同样的定义一个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;
       
    private View mView; 
       
    private boolean mIsShowInScreen = false;  
         
    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);//默认显示的图片 
        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);//画底图  
        
       
            //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() {  
            
       }  
 
       public void resumeRun() {  
             
       }  
        
       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);  
               }  
                 
            
           }  
       }  
   }  
  
}  

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

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

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();
}

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

--结束END--

本文标题: android实现icon动态旋转效果

本文链接: https://www.lsjlt.com/news/164741.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • android实现icon动态旋转效果
    本文实例为大家分享了android实现icon动态旋转效果的具体代码,供大家参考,具体内容如下 碰到客户的这样一个需求,点击icon后,前景的icon开始旋转,背景的icon不动,就...
    99+
    2022-11-13
  • Android 3D旋转动画效果实现分解
    这篇文章主要介绍一下如何实现View的3D旋转效果,实现的主要原理就是围绕Y轴旋转,同时在Z轴方面上有一个深入的缩放。演示的demo主要有以下几个重点: 1,自定义旋转动画 2...
    99+
    2022-06-06
    3d 3d旋转 动画 Android
  • HTLM怎么实现动态旋转木马效果
    这篇文章主要介绍“HTLM怎么实现动态旋转木马效果”,在日常操作中,相信很多人在HTLM怎么实现动态旋转木马效果问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”HTLM怎么实现...
    99+
    2022-10-19
  • Android实现Bitmap位图旋转效果
    位图的旋转也可以借助Matrix或者Canvas来实现。 通过postRotate方法设置旋转角度,然后用createBitmap方法创建一个经过旋转处理的Bitmap对象,最...
    99+
    2022-06-06
    bitmap Android
  • Android编程实现3D旋转效果实例
    本文实例讲述了Android编程实现3D旋转效果的方法。分享给大家供大家参考,具体如下: 下面的示例是在Android中实现图片3D旋转的效果。 实现3D效果一般使用OpenG...
    99+
    2022-06-06
    3d旋转 3d Android
  • Android编程实现3D滑动旋转效果的方法
    本文实例讲述了Android编程实现3D滑动旋转效果的方法。分享给大家供大家参考,具体如下: 这里我们通过代码实现一些滑动翻页的动画效果。 Animation实现动画有两个方式...
    99+
    2022-06-06
    方法 3d Android
  • Android 实现旋转木马的音乐效果
    一、百度在线音乐旋转木马效果 就上面那个,当音乐在播放的时候,那个光碟轮子在转,就想旋转木马一般。感觉好好玩啊。 碰巧想起前阵子做音乐播放器,哎,那这个也可以做在手机的音乐播...
    99+
    2022-06-06
    音乐 Android
  • Android利用Flutter实现立体旋转效果
    目录前言ImageShader 简介构建 ui.Image对象使用 ImageShader 填充形状立体旋转效果实现总结前言 之前我们提到了 CustomPaint er 的 Pai...
    99+
    2022-11-13
  • Android自定义View实现叶子飘动旋转效果(四)
    上一篇实现了叶子飘动功能,《Android自定义叶子飘动》 现在实现旋转效果 要实现这个效果,要在之前的功能上添加2个功能 1、通过matrix.postTranslate(...
    99+
    2022-06-06
    view Android
  • Android实现旋转动画
    本文实例为大家分享了Android实现旋转动画的具体代码,供大家参考,具体内容如下 旋转动画(可加速、减速) 1、准备工作 首先需要有一个用于旋转的图片 需要考虑如何开始、结束、加速...
    99+
    2022-11-12
  • Android SeekBar 自定义thumb旋转动画效果
    目录简介示例dimens.xmldrawableshape_thumb_round_1.xmllayers_thumb_ring_sweep_1.xmlrotate_thumb_1....
    99+
    2022-11-12
  • CSS3怎么实现旋转圈动画效果
    这篇文章主要讲解了“CSS3怎么实现旋转圈动画效果”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CSS3怎么实现旋转圈动画效果”吧!效果:html代码:<body style...
    99+
    2023-07-04
  • css旋转动画效果的实现方法
    这篇文章主要讲解了“css旋转动画效果的实现方法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“css旋转动画效果的实现方法”吧!我们可以通过css来做出动画...
    99+
    2022-10-19
  • Android中实现ProgressBar菊花旋转进度条的动画效果
    在一些常见到的加载中需要显示一个加载动画,如旋转的菊花,旋转的圈圈等等动画…,然后我们现在就来说下怎么去试下它吧 一.菊花的旋转动画 1.新建一个drawable文件 在res/d...
    99+
    2022-11-12
  • Flutter实现旋转扫描效果
    效果图: 1 .测试Demo启动文件 main() { runApp(MaterialApp( home: SignSwiperPage(), )); } cla...
    99+
    2022-11-13
  • opencv实现图像旋转效果
    本文实例为大家分享了opencv实现图像旋转效果的具体代码,供大家参考,具体内容如下 图像旋转: 在opencv中首先根据旋转角度和中心获取旋转矩阵,然后根据旋转矩阵进行变换 参数:...
    99+
    2022-11-11
  • Android实现绕球心旋转的引导页效果
    现在很多APP都会出现Android实现绕球心旋转的引导页效果,一个类似小车一直在往前开的旋转式动画效果。 先看一下预览效果: 嗯,整体效果还算理想,基本实现了页面绕屏幕底...
    99+
    2022-06-06
    引导页 Android
  • JS旋转实现转盘抽奖效果
    本文实例为大家分享了JS旋转实现转盘抽奖效果的具体代码,供大家参考,具体内容如下 闲来没事,做了一个模拟转盘抽奖的HTML&JS的效果: 可以在设置的时候,选择几个区域,并...
    99+
    2022-11-13
  • css怎么实现旋转翻牌动画效果
    小编给大家分享一下css怎么实现旋转翻牌动画效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!css动画之旋转翻牌效果,具体内容如下所示:我们先设置两个盒子大小,...
    99+
    2023-06-08
  • html5中怎么实现3d旋转动画效果
    这篇“html5中怎么实现3d旋转动画效果”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“h...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作