标签:
这是个全民看脸的时代,手机也一样。漂亮的APP给手机增色不少,那就赶紧给你这个女朋友打扮一下吧。
从设置静态壁纸,到动态壁纸,到锁屏壁纸,再到主题,其实也没那么简单。
1、设置静态壁纸
MainActivity.java
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wallpaperManager = WallpaperManager.getInstance(this); registerReceiver(broadcastReceiver, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED)); } WallpaperManager wallpaperManager; public void onClick(View v){ switch(v.getId()){ case R.id.btn_setWallpaper: File file = new File(Environment.getExternalStorageDirectory()+"/pictures/field.png"); try { InputStream is = new FileInputStream(file); //ContextWrapper方法,可读Bitmap、InputStream //需SET_WALLPAPER权限。读文件需WRITE_EXTERNAL_STORAGE权限 setWallpaper(is); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } break; case R.id.btn_setWallpaper2: //可加载res、InputStream、Bitmap try { wallpaperManager.setResource(R.drawable.frame); } catch (IOException e) { e.printStackTrace(); } break; case R.id.btn_clearWallpaper: //清除壁纸 try { wallpaperManager.clear(); } catch (IOException e) { e.printStackTrace(); } } } //接收壁纸改变广播 private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if(intent.getAction().equals(Intent.ACTION_WALLPAPER_CHANGED)){ Toast.makeText(MainActivity.this, "已更改墙纸", Toast.LENGTH_SHORT).show(); } } }; }activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/btn_setWallpaper" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="setWallpaper"/> <Button android:id="@+id/btn_setWallpaper2" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="setWallpaper2"/> <Button android:id="@+id/btn_clearWallpaper" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="clearWallpaper"/> </LinearLayout>2、设置动态壁纸
MyWallpaperService.java,每次从动态壁纸库选择当前壁纸会触发该Service实例化并调用onCreateEngine
/** * 包装器Service,用来实例化和管理Engine */ public class MyWallpaperService extends WallpaperService { private static final String TAG = MyWallpaperService.class.getSimpleName(); public MyWallpaperService(){ super(); Log.d(TAG, "MyWallpaperService"); } @Override public Engine onCreateEngine() { Log.d(TAG, "onCreateEngine"); return new MyWallpaperServiceEngine(this); } }MyWallpaperServiceEngine.java
/** * Engine封装一个用于显示墙纸的SurfaceView * <P>SurfaceView双缓存机制:一个front buffer,一个back buffer。假设当前显示的是front,若内容发生变化, * back在原来基础上把内容画好。然后back和front交换位置。<P> */ public class MyWallpaperServiceEngine extends WallpaperService.Engine { private static final String TAG = MyWallpaperServiceEngine.class.getSimpleName(); //每秒30个frame private static final int FPS = 5; private final Handler handler = new Handler(); public MyWallpaperServiceEngine(WallpaperService wp){ wp.super(); paint = new Paint(); paint.setARGB(255, 255, 255, 255); } @Override public void onCreate(SurfaceHolder surfaceHolder) { super.onCreate(surfaceHolder); } @Override public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) { super.onOffsetsChanged(xOffset, yOffset, xOffsetStep, yOffsetStep, xPixelOffset, yPixelOffset); } @Override public void onTouchEvent(MotionEvent event) { super.onTouchEvent(event); } Timer timer; @Override public void onSurfaceCreated(SurfaceHolder holder) { super.onSurfaceCreated(holder); timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { drawFrame(); } }, 0, 1000/FPS); } public void onSurfaceDestroyed(SurfaceHolder holder) { timer.cancel(); }; Paint paint; float y = 300f; private void drawFrame(){ final SurfaceHolder holder = getSurfaceHolder(); Canvas canvas = null; try{ canvas = holder.lockCanvas(new Rect(100, 100, 500, 1100)); if(canvas!=null){ //清屏,重置像素数组 canvas.drawColor(Color.BLACK); //在画布上绘制 canvas.drawCircle(300f, y, 200f, paint); y += 20f; if(y>900f){ y = 300f; } } }finally{ if(canvas!=null){ holder.unlockCanvasAndPost(canvas); } } // handler.removeCallbacks(drawSurface); // handler.postDelayed(drawSurface, 1000/FPS); } private final Runnable drawSurface = new Runnable() { @Override public void run() { drawFrame(); } }; }AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.qinuli.compiletest" android:versionCode="1" android:versionName="1.0" > <uses-permission android:name="android.permission.SET_WALLPAPER"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:permission="android.permission.BIND_WALLPAPER" > <activity android:name="com.qinuli.compiletest.MainActivity"> <intent-filter > <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name="com.qinuli.compiletest.SettingsActivity"></activity> <service android:name="com.qinuli.compiletest.MyWallpaperService"> <intent-filter > <action android:name="android.service.wallpaper.WallpaperService"/> </intent-filter> <meta-data android:name="android.service.wallpaper" android:resource="@xml/mylivewallpaper"/> </service> </application> </manifest>mylivewallpaper.xml
<wallpaper xmlns:android="http://schemas.android.com/apk/res/android" android:author="@string/author" android:description="@string/description" android:thumbnail="@drawable/wallpapericon" android:settingsActivity="com.qinuli.compiletest.SettingsActivity"/>res/values/strings.xml
<string name="author">石鑫</string> <string name="description">实时交互壁纸</string>SettingsActivity.java
public class SettingsActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); } }activity_settings.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Feel free to ask!"/> </LinearLayout>
标签:
原文地址:http://blog.csdn.net/talk2soul/article/details/45173185