Android學習筆記(四) android 更換案頭壁紙—–WallpaperManager

來源:互聯網
上載者:User

Android 中壁紙管理主要由 WallpaperManager 這個類負責

 

 

Provides access to the system wallpaper. With WallpaperManager, you can get
the current wallpaper, get the desired dimensions for the wallpaper, set the
wallpaper, and more. Get an instance of WallpaperManager with getInstance().

 

 

他的初始化方法為

wallpaperManager = WallpaperManager.getInstance(this); 

 

擷取當前壁紙的方法為:

Drawable drawable = wallpaperManager.getDrawable();

 

設定一張圖片作為壁紙的方法為

 

 

BitmapDrawable source = new BitmapDrawable(path);</p><p> try {<br />wallpaperManager.setBitmap(source.getBitmap());<br />iv.setImageDrawable(source);<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />} 

 

下面是實現效果:

 

 

更換壁紙後:

 

 

具體代碼為:

 

package young.MyWallPaperManager;<br />import java.io.File;<br />import java.io.FileInputStream;<br />import java.io.FileNotFoundException;<br />import java.io.IOException;<br />import java.io.InputStream;<br />import java.io.InputStreamReader;<br />import java.util.ArrayList;<br />import javax.security.auth.PrivateCredentialPermission;</p><p>import android.app.Activity;<br />import android.app.WallpaperManager;<br />import android.graphics.Bitmap;<br />import android.graphics.BitmapFactory;<br />import android.graphics.drawable.BitmapDrawable;<br />import android.graphics.drawable.Drawable;<br />import android.os.Bundle;<br />import android.view.ContextMenu;<br />import android.view.Menu;<br />import android.view.MenuItem;<br />import android.view.View;<br />import android.view.ContextMenu.ContextMenuInfo;<br />import android.view.View.OnClickListener;<br />import android.widget.Button;<br />import android.widget.EditText;<br />import android.widget.ImageView;<br />import android.widget.TextView;<br />public class MyWallPaperManager extends Activity {</p><p>private ImageView iv ;<br />private Button changeImageButton;<br />private TextView currentTextView;</p><p>private ArrayList<File> images ;<br />private WallpaperManager wallpaperManager;</p><p> /** Called when the activity is first created. */<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);</p><p> //獲得案頭壁紙<br /> wallpaperManager = WallpaperManager.getInstance(this);<br /> Drawable drawable = wallpaperManager.getDrawable();</p><p> iv = (ImageView)findViewById(R.id.imview);<br /> currentTextView = (TextView)findViewById(R.id.currentwall);<br /> changeImageButton =(Button)findViewById(R.id.ChangeWallpaperButton);</p><p> iv.setImageDrawable(drawable);</p><p> //registerForContextMenu(changeImageButton);<br /> changeImageButton.setOnCreateContextMenuListener(this);</p><p> //取得所有圖片<br /> images = new ArrayList<File>();<br /> getAllFiles(new File("/sdcard"));<br /> }</p><p> @Override<br /> public void onCreateContextMenu(ContextMenu menu,View view,<br /> ContextMenuInfo menuInfo){</p><p> menu.setHeaderTitle("點擊設定為案頭壁紙");<br /> for(int i = 0 ; i <images.size() ; i++){<br /> menu.add(0, i, i, images.get(i).getName());<br /> }<br /> }</p><p> @Override<br /> public boolean onContextItemSelected(MenuItem item){<br /> String name = (String) item.getTitle();<br /> //根據名字取得圖片<br /> String path = getElementByName(name).getPath();<br /> //<br /> BitmapDrawable source = new BitmapDrawable(path);</p><p> try {<br /> //設定為壁紙<br />wallpaperManager.setBitmap(source.getBitmap());<br />//顯示<br />iv.setImageDrawable(source);<br />} catch (IOException e) {<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br />}<br /> return true;<br /> }</p><p> //取得所有png 和 jpg jpeg<br /> private void getAllFiles(File root){</p><p>File files[] = root.listFiles();</p><p>if(files != null)<br />for(File f:files){</p><p>if(f.isDirectory()){<br />getAllFiles(f);<br />}<br />else{<br />//檢查檔案類型<br />String fpath = f.getPath().substring(f.getPath().lastIndexOf(".")+1,f.getPath().length());<br />//如果是圖片<br />if(fpath.equals("png")||fpath.equals("jpg")){<br />images.add(f);<br />}<br />}<br />}<br /> }</p><p> private File getElementByName(String Name){<br /> for(int i = 0 ; i <images.size() ; i++){<br /> if(images.get(i).getName().equals(Name))<br /> return images.get(i);<br /> }<br /> return null;<br /> }</p><p>}<br /> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.