Home>

main.xml layout file:

<?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="Restore default wallpaper" />
  <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="Get current wallpaper" />
  <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="Set as current wallpaper" />
</linearlayout>

Manifest file:

<?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" />
  <!-Set phone wallpaper permissions->
  <uses-permission android:name="android.permission.set_wallpaper" />
</manifest>

walladapter custom adapter:

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];//recyclable
  }
  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]);//Set the background image of imageview
    imageview.setscaletype (imageview.scaletype.center_crop);
    imageview.setlayoutparams (new gallery.layoutparams (120, 120));
    return imageview;
  }
}

wallactivity class:

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;//The index of the selected picture in the id array
  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;//Record the selected picture index
      }
      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://Restore mobile wallpaper
        try {
          wallactivity.this.clearwallpaper ();
        } catch (ioexception e) {
          e.printstacktrace ();
        }
        break;
      case r.id.getwall://Set the content displayed by imageview as the current wallpaper
        currwall.setbackgrounddrawable (getwallpaper ());
        break;
      case r.id.setwall://Set wallpaper
        inputstream in=wallactivity.this.getresources (). openrawresource (imgids [selectindex]);
        try {
          setwallpaper (in);
        } catch (ioexception e) {
          e.printstacktrace ();
        }
        break;
      }
    }
  };
}

operation result:

  • Previous PHP product comparison function code sharing
  • Next ASPNET display gradient image implementation method