編輯:關於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.xmlmylivewallpaper.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. 什麼是服務: Android中的四大組件, 是一個可以長時間在後台運行的不提供用戶界面的一個類. 2.服務的特性: 服務一旦被啟動無論啟動它的界面還存不存
系列回顧:本系列主要從開發的角度介紹UiAutomator的使用,總共包括三篇: 基礎入門: Android自動化測試之UiAutomator(一) 技巧篇: Andro
短暫的暑假已經結束了,假期培訓正式開始。Androidmanifest.XML 清單文件es 資源文件Drawable 顏色改變Layout 布局的文件setConten
本文實例講述了Android實現向Launcher添加快捷方式的方法。分享給大家供大家參考。具體如下:當我們在應用程序Launcher的桌面空白處長按觸摸時,會出現一個對