Android实现手机壁纸改变的方法

时间:2021-10-08 09:02:00

本文实例讲述了android实现手机壁纸改变的方法。分享给大家供大家参考。具体如下:

main.xml布局文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <button android:id="@+id/clearwall"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="恢复默认墙纸" />
  <imageview android:id="@+id/currwall"
    android:layout_width="100px"
    android:layout_height="150px"
    android:layout_gravity="center_horizontal" />
  <button android:id="@+id/getwall"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="获取当前墙纸" />
  <gallery android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
  <button android:id="@+id/setwall"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="设置为当前墙纸" />
</linearlayout>

清单文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.ljq.activity"
   android:versioncode="1"
   android:versionname="1.0">
  <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".wallactivity"
         android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
  </application>
  <uses-sdk android:minsdkversion="7" />
  <!-- 设置手机墙纸权限 -->
  <uses-permission android:name="android.permission.set_wallpaper" />
</manifest>

walladapter自定义适配器:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.ljq.activity;
import android.content.context;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.gallery;
import android.widget.imageview;
public class walladapter extends baseadapter {
  private int[] imgids = null;
  private context context = null;
  public walladapter(int[] imgids, context context) {
    super();
    this.imgids = imgids;
    this.context = context;
  }
  public int getcount() {
    return imgids.length;
  }
  public object getitem(int position) {
    //return imgids[position];
    return imgids[position%imgids.length];//可循环
  }
  public long getitemid(int position) {
    return position;
  }
  public view getview(int position, view convertview, viewgroup parent) {
    imageview imageview = new imageview(context);
    imageview.setbackgroundresource(imgids[position]);// 设置imageview的背景图片
    imageview.setscaletype(imageview.scaletype.center_crop);
    imageview.setlayoutparams(new gallery.layoutparams(120, 120));
    return imageview;
  }
}

wallactivity类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.ljq.activity;
import java.io.ioexception;
import java.io.inputstream;
import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.adapterview;
import android.widget.button;
import android.widget.gallery;
import android.widget.imageview;
import android.widget.adapterview.onitemselectedlistener;
public class wallactivity extends activity {
  private int[] imgids={r.drawable.w1, r.drawable.w2, r.drawable.w3, r.drawable.w4};
  private int selectindex=-1;//被选中的图片在id数组中的索引
  private imageview currwall=null;
  private gallery gallery=null;
  private button clearwall=null;
  private button getwall=null;
  private button setwall=null;
  @override
  public void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.main);
    gallery=(gallery)findviewbyid(r.id.gallery);
    gallery.setadapter(new walladapter(imgids, wallactivity.this));
    gallery.setspacing(5);
    gallery.setonitemselectedlistener(new onitemselectedlistener(){
      public void onitemselected(adapterview<?> parent, view view,
          int position, long id) {
        selectindex = position;//记录被选中的图片索引
      }
      public void onnothingselected(adapterview<?> parent) {
      }
    });
    currwall=(imageview)findviewbyid(r.id.currwall);
    clearwall=(button)findviewbyid(r.id.clearwall);
    getwall=(button)findviewbyid(r.id.getwall);
    setwall=(button)findviewbyid(r.id.setwall);
    clearwall.setonclicklistener(listener);
    getwall.setonclicklistener(listener);
    setwall.setonclicklistener(listener);
  }
  view.onclicklistener listener=new view.onclicklistener(){
    public void onclick(view v) {
      button btn=(button)v;
      switch (btn.getid()) {
      case r.id.clearwall://还原手机壁纸
        try {
          wallactivity.this.clearwallpaper();
        } catch (ioexception e) {
          e.printstacktrace();
        }
        break;
      case r.id.getwall://设置imageview显示的内容为当前墙纸
        currwall.setbackgrounddrawable(getwallpaper());
        break;
      case r.id.setwall://设置墙纸
        inputstream in=wallactivity.this.getresources().openrawresource(imgids[selectindex]);
        try {
          setwallpaper(in);
        } catch (ioexception e) {
          e.printstacktrace();
        }
        break;
      }
    }
  };
}

运行结果:

Android实现手机壁纸改变的方法

希望本文所述对大家的android程序设计有所帮助。