Android implementation of Mobile wallpaper change method _android

Source: Internet
Author: User

This article is an example of how Android implements mobile wallpaper changes. Share to everyone for your reference. Specifically as follows:

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_conte NT "android:text=" restore Default wallpaper "/> <imageview android:id=" @+id/currwall "android:layout_width=" 100px "Andr 
    oid: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"/> Y android:id= "@+id/gallery" android:layout_width= "fill_parent" android:layout_height= "wrap_content"/> <Bu Tton android:id= "@+id/setwall" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Andro Id: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 the-->
  <uses-permission android:name= "Android.permission.SET_WALLPAPER" for mobile wallpaper >
</manifest>

Walladapter Custom Adapters:

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 = 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];//can loop} public long getitemid (int position) {return position; Public View GetView (int position, View Convertview, ViewGroup parent) {ImageView ImageView = new ImageView (Contex
    T); Imageview.setbackgroundresource (Imgids[position])//Set ImageView background picture 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.drawab
  LE.W4};
  private int selectindex=-1;//The selected picture in the ID array of the index 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 selected picture index} public void onnothingselected (Adapter
    View<?> 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://Settings ImageView display is 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;
}
    }
  };

 }

Run Result:

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.