Android Development Learning Wallpaper Settings Wallpapers Detailed introduction and example _android

Source: Internet
Author: User

Today I'm sharing with you the way to set up wallpapers in Android, there are three ways to set up wallpaper in Android, respectively:

1, the use of Wallpapermanager setresource (int ResourceID) method

2, the use of Wallpapermanager SetBitmap (Bitmap Bitmap) method

3. Rewrite the setwallpaper provided in the Contextwrapper class ()

In addition, we also need to include the following permissions in the application: <uses-permission android:name= "Android.permission.SET_WALLPAPER"/>

Here's a basic way to implement the wallpaper application that comes with Android. First look at my layout code:

Copy Code code as follows:

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical"
Android:background= "#000000"
Tools:context= ". Mainactivity ">
<imageswitcher
Android:id= "@+id/imageswitcher"
Android:layout_width= "Fill_parent"
android:layout_height= "370DP" >
</ImageSwitcher>
<gallery
Android:id= "@+id/gallery"
Android:layout_width= "Fill_parent"
android:layout_height= "80DP"
android:layout_below= "@+id/imageswitcher"/>
<button
Android:id= "@+id/btngo"
Android:layout_width= "Wrap_content"
android:layout_height= "40DP"
android:layout_below= "@+id/gallery"
Android:layout_alignparentbottom= "true"
Android:layout_centerhorizontal= "true"
android:text= "@string/btngo"/>
</RelativeLayout>

Here we use Gallery to implement a list of thumbnails that can be selected by the user, and when the user selects an image in the list, the current image is displayed in the Imageswitcher control, and the current picture is set to be wallpaper when the button is clicked. In fact, the imageswitcher here can be completely replaced by ImageView, considering that Imageswitcher can provide a better animation effect, so we chose the imageswitcher here. In the same way, we continue to use the Imageadapter class in the Gallery of Android Development learning:

Copy Code code as follows:

Package com.android.gallery2switcher;

Import Android.content.Context;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.ImageView;

public class Imageadapter extends baseadapter{

Class member Mycontext to context parent class
Private context Mycontext;
Private int[] myimages;

constructor with two parameters, that is, the context and images array to store
Public Imageadapter (Context c,int[] Images)
{
TODO auto-generated Constructor stub
This.mycontext=c;
This.myimages=images;
}

Returns the total number of all pictures
@Override
public int GetCount ()
{

return this.myImages.length;
}

Using the GetItem method, the array ID of the image in the current container is obtained.
@Override
Public Object getitem (int position)
{
return position;
}


@Override
public long getitemid (int position)
{
return position;
}

Gets the view of the image currently being displayed, and the incoming array ID value makes it read and imaging
@Override
Public View GetView (int position, View Convertview, ViewGroup parent)
{
ImageView image=new ImageView (this.mycontext);
Image.setimageresource (This.myimages[position]);
Image.setscaletype (ImageView.ScaleType.FIT_XY);
Image.setadjustviewbounds (TRUE);
return image;
}

}

Now, we can start writing the program, the background code is as follows:

Copy Code code as follows:

Package com.android.gallery2switcher;

Import java.io.IOException;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.app.WallpaperManager;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.view.Window;
Import Android.view.animation.AnimationUtils;
Import Android.widget.AdapterView;
Import Android.widget.AdapterView.OnItemSelectedListener;
Import Android.widget.Button;
Import Android.widget.Gallery;
Import Android.widget.Gallery.LayoutParams;
Import Android.widget.ImageSwitcher;
Import Android.widget.ImageView;
Import Android.widget.ViewSwitcher.ViewFactory;

public class Mainactivity extends activity {

Gallery MGallery;
Imageswitcher Mswitcher;
Button Btngo;
Int[] Resources=new Int[]{r.drawable.image0,r.drawable.image1,r.drawable.image2,r.drawable.image3,
R.drawable.image4,r.drawable.image5,r.drawable.image6,r.drawable.image7,r.drawable.image8};
int index;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Do not display the title bar
Requestwindowfeature (Window.feature_no_title);
Setcontentview (R.layout.activity_main);
mgallery= (Gallery) Findviewbyid (r.id.gallery);
Mswitcher= (Imageswitcher) Findviewbyid (R.id.imageswitcher);
Implementation of Imageswitcher Factory interface
Mswitcher.setfactory (New Viewfactory ()
{
@Override
Public View Makeview ()
{
ImageView i = new ImageView (mainactivity.this);
I.setbackgroundcolor (0xff000000);
I.setscaletype (ImageView.ScaleType.FIT_CENTER);
I.setlayoutparams (New Imageswitcher.layoutparams (Layoutparams.match_parent, layoutparams.match_parent));
return i;
}
});
Setting up Resources
Mswitcher.setimageresource (Resources[0]);
Set animation
Mswitcher.setinanimation (animationutils.loadanimation) (this,android. r.anim.fade_in));
Mswitcher.setoutanimation (animationutils.loadanimation) (this,android. R.anim.fade_out));
btngo= (Button) Findviewbyid (R.id.btngo);
Btngo.setonclicklistener (New Onclicklistener ()
{
@Override
public void OnClick (View arg0)
{
SetWallpaper ();
}
});
Imageadapter madapter=new Imageadapter (this,resources);
Mgallery.setadapter (Madapter);
Mgallery.setonitemselectedlistener (New Onitemselectedlistener ()
{
@Override
public void onitemselected (adapterview<?> Adapter, View view,int position, long ID)
{
Set picture
Mswitcher.setimageresource (Resources[position]);
Get the current picture index
Index=position;
}
@Override
public void onnothingselected (adapterview<?> arg0)
{

}

});


}
Setup Wallpaper
public void SetWallpaper ()
{
Wallpapermanager mwallmanager=wallpapermanager.getinstance (this);
Try
{
Mwallmanager.setresource (Resources[index]);
}
catch (IOException E)
{
E.printstacktrace ();
}
}

@Override
public boolean Oncreateoptionsmenu (Menu menu)
{
return true;
}

}

As you can see, when using imageswitcher, we need to implement its factory interface, and here the Makeview () method is the same as the GetView () method in Baseadapter, which returns a view. We imageswitcher to use the system default animation effect. The final operating effect is as follows:

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.