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); try { wallpaperManager.setBitmap(source.getBitmap()); iv.setImageDrawable(source); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

下面是实现效果:

Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager_第1张图片Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager_第2张图片

更换壁纸后:

Android学习笔记(四) android 更换桌面壁纸-----WallpaperManager_第3张图片

具体代码为:

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

更多相关文章

  1. Android之手机壁纸的改变
  2. android图片缩放手势检测类--ScaleGestureDetector
  3. android图片压缩方法
  4. android ListView SimpleAdapter 带图片
  5. Android之OnGestureListener实现图片的左右滑动
  6. android 动态壁纸
  7. 图片压缩保存读取操作
  8. Android 圆角图片,基于Glide4.9 的 BitmapTransformation,可任意设
  9. 【原创】Android 4.4前后版本读取图库图片方式的变化

随机推荐

  1. 文件对应
  2. android studio 54 下载进度条
  3. android Preference Demo
  4. Android对话框显示后自动消失
  5. Android(安卓)播放网络视频
  6. Android(安卓)Spinner 用PopupWindow实现
  7. G-sensor On Android
  8. Android(安卓)带阻尼回弹效果的ScorllVie
  9. Android中摇一摇效果的实现
  10. Android英语单词记忆程序源码