静态:
1 WallpaperManager wallpaperManager = WallpaperManager.getInstance(getActivity()); 2 try { 3 wallpaperManager.setResource(getResources().getIdentifier("anim", "drawable", "com.example.asus.newjyjsproject")); 4 Toast.makeText(getActivity(), "设置成功", Toast.LENGTH_SHORT).show(); 5 } catch (IOException e) { 6 e.printStackTrace(); 7 }
动态:
首先实现 WallpaperService 服务
1 public class GIFWallpaperService3 extends WallpaperService { 2 3 @Override 4 public Engine onCreateEngine() { 5 try { 6 Movie movie = Movie.decodeStream( 7 getResources().getAssets().open("bb.gif")); 8 9 return new GIFWallpaperEngine(movie); 10 }catch(IOException e){ 11 Log.d("GIF", "Could not load asset"); 12 return null; 13 } 14 } 15 16 private class GIFWallpaperEngine extends Engine { 17 18 private final int frameDuration = 20; 19 20 private SurfaceHolder holder; 21 private Movie movie; 22 private boolean visible; 23 private Handler handler; 24 25 public GIFWallpaperEngine(Movie movie) { 26 this.movie = movie; 27 handler = new Handler(); 28 } 29 30 @Override 31 public void onCreate(SurfaceHolder surfaceHolder) { 32 super.onCreate(surfaceHolder); 33 this.holder = surfaceHolder; 34 } 35 36 private Runnable drawGIF = new Runnable() { 37 public void run() { 38 draw(); 39 } 40 }; 41 42 43 private void draw() { 44 if (visible) { 45 // 自适应屏幕 46 WindowManager wm = (WindowManager) getSystemService( 47 Context.WINDOW_SERVICE); 48 DisplayMetrics metrics = new DisplayMetrics(); 49 wm.getDefaultDisplay().getMetrics(metrics); 50 int width = metrics.widthPixels; 51 int height = metrics.heightPixels; 52 Canvas canvas = holder.lockCanvas(); 53 canvas.save(); 54 // Adjust size and position so that 55 // the image looks good on your screen 56 int w = movie.width(); 57 int h = movie.height(); 58 59 float scaleWight = ((float)width)/w; 60 float scaleHeight = ((float)height)/h; 61 62 canvas.scale(scaleWight, scaleHeight); 63 64 movie.draw(canvas, 0, 0); 65 canvas.restore(); 66 holder.unlockCanvasAndPost(canvas); 67 movie.setTime((int) (System.currentTimeMillis() % movie.duration())); 68 69 handler.removeCallbacks(drawGIF); 70 handler.postDelayed(drawGIF, frameDuration); 71 } 72 } 73 74 @Override 75 public void onVisibilityChanged(boolean visible) { 76 this.visible = visible; 77 if (visible) { 78 handler.post(drawGIF); 79 } else { 80 handler.removeCallbacks(drawGIF); 81 } 82 } 83 84 @Override 85 public void onDestroy() { 86 super.onDestroy(); 87 handler.removeCallbacks(drawGIF); 88 } 89 } 90 }
在配置文件中注册:
1 <service 2 android:name=".GIFWallpaperService" 3 android:enabled="true" 4 android:label="路灯" 5 android:permission="android.permission.BIND_WALLPAPER" > 6 <intent-filter> 7 <action android:name="android.service.wallpaper.WallpaperService"/> 8 </intent-filter> 9 <meta-data 10 android:name="android.service.wallpaper" 11 android:resource="@xml/livepaper" > 12 </meta-data> 13 </service>
在res/xml下创建文件 livepaper.xml
<?xml version="1.0" encoding="UTF-8"?> <wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:label="GIF Wallpaper" android:thumbnail="@drawable/aac"> </wallpaper>
最后在Activity中调用系统壁纸:
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent chooseIntent = new Intent(Intent.ACTION_SET_WALLPAPER); // 启动系统选择应用 Intent intent = new Intent(Intent.ACTION_CHOOSER); intent.putExtra(Intent.EXTRA_INTENT, chooseIntent); intent.putExtra(Intent.EXTRA_TITLE, "选择动态壁纸"); startActivity(intent); } });