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

Android开发学习之WallPaper设置壁纸详细介绍与实例

程序员文章站 2022-07-11 19:34:29
今天和大家分享的是关于在android中设置壁纸的方法,在android中设置壁纸的方法有三种,分别是: 1、使用wallpapermanager的setresource...

今天和大家分享的是关于在android中设置壁纸的方法,在android中设置壁纸的方法有三种,分别是:

1、使用wallpapermanager的setresource(int resourceid)方法

2、使用wallpapermanager的setbitmap(bitmap bitmap)方法

3、重写contextwrapper 类中提供的setwallpaper()

除此之外,我们还需要在应用程序中加入下列权限: <uses-permission android:name="android.permission.set_wallpaper"/>

下面我们以此为基本方法,来实现android中自带的壁纸应用。首先来看我的布局代码:

复制代码 代码如下:

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

 在这里我们使用gallery来实现一个可以供用户选择的缩略图列表,当用户选择列表中的图像时,会在imageswitcher控件中显示出当前图像,当点击button时,当前图片将被设置为壁纸。其实这里的imageswitcher完全可以替换为imageview,考虑到imageswitcher可以提供较好的动画效果,所以我们在这里选择了imageswitcher。同样地,我们继续使用android开发学习之gallery中的那个imageadapter类:

 

复制代码 代码如下:

 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{

 //类成员mycontext为context父类
 private context mycontext;
 private int[] myimages;

 //构造函数,有两个参数,即要存储的context和images数组
 public imageadapter(context c,int[] images)
 {
  // todo auto-generated constructor stub
  this.mycontext=c;
  this.myimages=images;
 }

 //返回所有的图片总数量
 @override
 public int getcount()
 {

  return this.myimages.length;
 }

 //利用getitem方法,取得目前容器中图像的数组id
 @override
 public object getitem(int position)
 {
  return position;
 }


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

 //取得目前欲显示的图像的view,传入数组id值使之读取与成像
 @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;
 }

}
 

 现在,我们就可以开始编写程序了,后台的代码如下:
 

复制代码 代码如下:

 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);
  //不显示标题栏
  requestwindowfeature(window.feature_no_title);
  setcontentview(r.layout.activity_main);
  mgallery=(gallery)findviewbyid(r.id.gallery);
  mswitcher=(imageswitcher)findviewbyid(r.id.imageswitcher);
  //实现imageswitcher的工厂接口
    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;
     }
    });
     //设置资源
     mswitcher.setimageresource(resources[0]);
     //设置动画
     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)
   {
    //设置图片
    mswitcher.setimageresource(resources[position]);
    //获取当前图片索引
    index=position;
   }
   @override
   public void onnothingselected(adapterview<?> arg0)
   {

   }

  });

      
 }
 //设置壁纸
    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;
 }

}
 

 可以看到,在使用imageswitcher的时候,我们需要实现它的工厂接口,并且这里的makeview()方法和baseadapter里的getview()方法是一样的,即返回一个view视图。我们imageswitcher给使用了系统默认的动画效果。最终运行效果如下:

Android开发学习之WallPaper设置壁纸详细介绍与实例

Android开发学习之WallPaper设置壁纸详细介绍与实例

Android开发学习之WallPaper设置壁纸详细介绍与实例

Android开发学习之WallPaper设置壁纸详细介绍与实例