Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android牆紙的設定

Android牆紙的設定

編輯:關於Android編程

這是個全民看臉的時代,手機也一樣。漂亮的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

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
 * 

SurfaceView雙緩存機制:一個front buffer,一個back buffer。假設當前顯示的是front,若內容發生變化, * back在原來基礎上把內容畫好。然後back和front交換位置。

*/ 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


    
    
    
    

    
        
            
                
                
            
        
        
        
            
                
            
            
        
    

mylivewallpaper.xml
res/values/strings.xml
石鑫
實時交互壁紙
SettingsActivity.java
public class SettingsActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
	}
}
activity_settings.xml


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved