Android壁紙設定

來源:互聯網
上載者:User

 前幾天看到豌豆莢上面風景壁紙的APP 下載得挺火,閑來無事心痒痒的想寫一個壁紙程式。由於是Android開發新手,忘記了一個很重要的開發步驟,就是對許可權的設定。開始以為自己寫的壁紙設定程式有問題,經過對doc文檔的多次閱讀,寫好幾種對壁紙的設定。供大家參考。

1、別忘記在ApplicationManifest.xml 中加上許可權的設定。

<uses-permission android:name = "android.permission.SET_WALLPAPER"/>

         2、設定壁紙的方法總結。

壁紙設定方法有三種

        第一 通過WallpaperManager方法中的 setBitmap()

第二 通過WallpaperManager方法中的 setResource()

第三 通過ContextWrapper 類中提供的setWallpaper()方法

 由於 Activity 繼承ContextThemeWrapper ,ContextThemeWrapper繼承 ContextWrapper.

        1)通過執行個體化WallpaperManager 類調用單例類中setBitmap()方法。

 

 

Java代碼  
  1. WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);  
  2.  Resources res = getResources();  
  3.  Bitmap bitmap=BitmapFactory.decodeResource(res, getResources().getIdentifier("wallpaper" + imagePosition, "drawable", "com.ch"));   
  4. wallpaperManager.setBitmap(bitmap);  
  5. Toast.makeText(this, "設定成功", Toast.LENGTH_SHORT).show();  

         2)第二 通過WallpaperManager方法中的 setResource()

 

Java代碼  
  1. WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);  
  2.     try {  
  3.         wallpaperManager.setResource(getResources().getIdentifier("wallpaper" + imagePosition, "drawable", "com.ch"));  
  4. Toast.makeText(this, "設定成功", Toast.LENGTH_SHORT).show();  
  5.     } catch (IOException e) {  
  6.         e.printStackTrace();  
  7. }  

  3) 第三 通過ContextWrapper 類中提供的setWallpaper()方法Java代碼  

  1. //重寫ContextWrapper中setWallpaper()方法  
  2. public void setWallpaper(InputStream paramInputStream) throws IOException {  
  3.         super.setWallpaper(paramInputStream);  
  4.         Toast.makeText(this, "設定成功", 1).show();  
  5. }  
  6.   
  7. //設定壁紙代碼  
  8.                         Resources localResources = getBaseContext().getResources();  
  9.             InputStream localInputStream2 = localResources  
  10.                     .openRawResource(getResources().getIdentifier(  
  11.                             "wallpaper" + imagePosition, "drawable", "com.ch"));  
  12.             try {  
  13.                 setWallpaper(localInputStream2);  
  14.             } catch (IOException e) {  
  15.                 e.printStackTrace();  
  16.             }  
     總結:           設定壁紙功能很簡單,可以當成我們第一個app應用練手。其中可以加入ImageSwitcher 和gallery等控制項。    真正完成一個發行就緒的App應用

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.