20140917设置动态壁纸
Manifest.xml中:
1 2 3 4 5 6 7 8 9 10 11 12 | < service android:enabled="true" android:permission="android.permission.BIND_WALLPAPER" android:name="com.android.settings.common.display.BackgroundSettingService"> < intent-filter > < action android:name="android.service.wallpaper.WallpaperService"></ action > </ intent-filter > < meta-data android:name="android.service.wallpaper" android:resource="@xml/wallpaper"> </ meta-data > </ service > |
选中设置中的条目:
1 2 3 4 5 6 7 8 9 10 11 12 13 | Activity activity = getActivity(); PreferenceScreen screenBackgroundSetting = (PreferenceScreen) findPreference(KEY_BACKGROUND_SETTING); if (screenBackgroundSetting != null ){ screenBackgroundSetting.setOnPreferenceClickListener( new OnPreferenceClickListener(){ @Override public boolean onPreferenceClick(Preference preference) { // TODO Auto-generated method stub startFragmentTransaction( new CommonBackgroundSettingFragment()); return true ; } }); } |
右边界面切换为如下Fragment:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | package com.android.settings.common.display; import android.app.Fragment; import android.app.WallpaperInfo; import android.app.WallpaperManager; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.Toast; import com.android.settings.R; public class CommonBackgroundSettingFragment extends Fragment{ private Button btnBackground1; private static final String tagetPackageName = "com.android.wallpaper" ; private static final int REQUEST_SET_LIVE_WALLPAPER = 101 ; private static final String TAG = "CommonBackgroundSettingFragment" ; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_background_setting, container, false ); btnBackground1 = (Button) rootView.findViewById(R.id.btnBackground1); btnBackground1.requestFocus(); btnBackground1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER); startActivityForResult(intent, REQUEST_SET_LIVE_WALLPAPER); Toast.makeText(CommonBackgroundSettingFragment. this .getActivity(), getString(R.string.start_background_setting), Toast.LENGTH_LONG).show(); } }); return rootView; } @Override public void onResume() { // TODO Auto-generated method stub super .onResume(); } @Override public void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SET_LIVE_WALLPAPER) { if (isLiveWallpaperRunning( this .getActivity(), tagetPackageName)) { Toast.makeText( this .getActivity(), "LiveWallpaper is Running" , Toast.LENGTH_LONG).show(); } else { Toast.makeText( this .getActivity(), "LiveWallpaper is not Running" , Toast.LENGTH_LONG).show(); } } super .onActivityResult(requestCode, resultCode, data); } /** * 判断一个动态壁纸是否已经在运行 * * @param context * :上下文 * @param tagetPackageName * :要判断的动态壁纸的包名 * @return */ public static boolean isLiveWallpaperRunning(Context context, String tagetPackageName) { WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); // 得到壁纸管理器 WallpaperInfo wallpaperInfo = wallpaperManager.getWallpaperInfo(); // 如果系统使用的壁纸是动态壁纸话则返回该动态壁纸的信息,否则会返回null if (wallpaperInfo != null ) { // 如果是动态壁纸,则得到该动态壁纸的包名,并与想知道的动态壁纸包名做比较 String currentLiveWallpaperPackageName = wallpaperInfo.getPackageName(); if (currentLiveWallpaperPackageName.equals(tagetPackageName)) { return true ; } } return false ; } } |
设置动态壁纸的Service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | /** * LiveWallpaper Launcher And Preference Template * The MIT License * copyright (c) 2011 mdlab.jp * @author itoz */ package com.android.settings.common.display; import java.sql.Date; import java.text.SimpleDateFormat; import android.annotation.SuppressLint; import android.content.SharedPreferences; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Bundle; import android.os.Handler; import android.service.wallpaper.WallpaperService; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.widget.Toast; import com.android.settings.R; @SuppressLint ( "NewApi" ) public class BackgroundSettingService extends WallpaperService { private static final String TAG = "BackgroundSettingService" ; private Handler handler = new Handler(); @Override public void onCreate() { Log.i(TAG, "service onCreate" ); super .onCreate(); } @Override public void onDestroy() { Log.i(TAG, "service onDestory" ); super .onDestroy(); } @Override public Engine onCreateEngine() { Log.i(TAG, "service onCreateEngine" ); return new MyEngine(); } private class MyEngine extends Engine{ private float x = 200 ; private float y = 300 ; private Paint mPaint; private boolean mVisible = false ; private Runnable drawThread = new Runnable() { public void run() { drawWallpaper(); } }; public MyEngine(){ Log.i(TAG, "MyEngine" ); mPaint = new Paint(); mPaint.setColor(Color.RED); mPaint.setAntiAlias( true ); mPaint.setStrokeWidth( 4 ); mPaint.setStyle(Paint.Style.STROKE); mPaint.setTextSize( 24 ); } @Override public Bundle onCommand(String action, int x, int y, int z, Bundle extras, boolean resultRequested) { // TODO Auto-generated method stub Log.i(TAG, "onCommand" ); return super .onCommand(action, x, y, z, extras, resultRequested); } @Override public void onCreate(SurfaceHolder surfaceHolder) { Log.i(TAG, "onCreate" ); super .onCreate(surfaceHolder); //作用是使壁纸能响应touch event,默认是false setTouchEventsEnabled( true ); } @Override public void onDestroy() { Log.i(TAG, "onDestroy" ); super .onDestroy(); handler.removeCallbacks(drawThread); } @Override public void onOffsetsChanged( float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { // TODO Auto-generated method stub Log.i(TAG, "onoffsetsChanged" ); super .onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset); } @Override public void onVisibilityChanged( boolean visible) { Log.i(TAG, "onVisibilityChanged" ); mVisible = visible; if (visible) { //开始 handler.postDelayed(drawThread, 50 ); } else { handler.removeCallbacks(drawThread); } } @Override public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { Log.i(TAG, "onSurfaceChanged" ); // TODO Auto-generated method stub super .onSurfaceChanged(holder, format, width, height); } @Override public void onSurfaceCreated(SurfaceHolder holder) { Log.i(TAG, "onSurfaceCreated" ); super .onSurfaceCreated(holder); } @Override public void onSurfaceDestroyed(SurfaceHolder holder) { Log.i(TAG, "onSurfaceDestoryed" ); // TODO Auto-generated method stub super .onSurfaceDestroyed(holder); handler.removeCallbacks(drawThread); } @Override public void onTouchEvent(MotionEvent event) { Log.i(TAG, "onTouchEvent" ); // TODO Auto-generated method stub super .onTouchEvent(event); } private void drawWallpaper() { SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = holder.lockCanvas(); drawTime(canvas); holder.unlockCanvasAndPost(canvas); //循环执行 handler.postDelayed(drawThread, 50 ); } private void drawTime(Canvas canvas) { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" ); String dataStr = sdf.format(date); canvas.save(); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.captain_america); Bitmap mBitmapDisplayed = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), false ); canvas.drawBitmap(mBitmapDisplayed, 0 , 0 , null ); // canvas.drawColor(0xff00ff00); canvas.drawText(dataStr, x, y, mPaint); canvas.restore(); } } } |
效果为:背景是“captain_america.png”,同时上层有时间的动态更新。
posted on 2015-04-06 15:50 leihupqrst 阅读(226) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步