欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-29 22:21:58
本文实例讲述了android实现手机壁纸改变的方法。分享给大家供大家参考。具体如下: main.xml布局文件:

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

main.xml布局文件:

<?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>

清单文件:

<?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自定义适配器:

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类:

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程序设计有所帮助。