Android開發 設置手機壁紙

內容概要

可以選擇自己喜歡的圖片進行壁紙設置

所需方法

1、使用WallpaperManager的setResource(int ResourceID)方法

2、使用WallpaperManager的setBitmap(Bitmap bitmap)方法

3、重寫ContextWrapper 類中提供的setWallpaper()

4.傳入9張自己喜歡的圖片,命名image1-image9

Mainfest中加入權限:

<uses-permission android:name="android.permission.SET_WALLPAPER"/>

佈局代碼

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

ImageAdapter類

使用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;
 }

}

main代碼

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

}

實現效果圖

在這裏插入圖片描述
可左右拖動:在這裏插入圖片描述
點擊設置壁紙後:
在這裏插入圖片描述


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章