Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 更換桌布實作:WallpaperManager 三種用法與 Gallery 範例

編輯:關於 Android 程式設計

讓使用者從預先準備的圖片裡挑一張設為桌布,是入門階段很值得動手做的小功能。Android 提供了三條路徑可以設定壁紙:

  1. WallpaperManager.setResource(int resourceID)——直接指定 drawable 資源
  2. WallpaperManager.setBitmap(Bitmap bitmap)——傳入已解碼的圖像物件
  3. 覆寫 ContextWrapper 提供的 setWallpaper()

無論用哪一種,都要先在 AndroidManifest.xml 加上權限宣告:

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

版面配置

這個範例用 Gallery 做縮圖列表,用 ImageSwitcher 顯示目前選中的大圖,最後用一顆按鈕套用:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://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 立即換上對應圖片;按下按鈕才真正設為桌布。這裡用 ImageSwitcher 而非 ImageView,主要是為了它內建的切換動畫效果。

ImageAdapter:把圖片陣列餵給 Gallery

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 {

    private Context myContext;
    private int[] myImages;

    public ImageAdapter(Context c, int[] Images) {
        this.myContext = c;
        this.myImages = Images;
    }

    @Override
    public int getCount() {
        return this.myImages.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

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

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

主程式:ImageSwitcher 與桌布設定

package com.android.gallery2switcher;

import java.io.IOException;

import android.os.Bundle;
import android.app.Activity;
import android.app.WallpaperManager;
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.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(
                        android.widget.Gallery.LayoutParams.MATCH_PARENT,
                        android.widget.Gallery.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();
        }
    }
}

使用 ImageSwitcher 的兩個關鍵點

第一,一定要實作它的工廠介面 ViewFactory,其中的 makeView() 和 BaseAdapter 的 getView() 作用相同,都是回傳一個要顯示的 View。第二,動畫效果需要自行透過 setInAnimation() 與 setOutAnimation() 指定,這裡直接沿用系統預設的淡入淡出。

執行結果

如果桌布來源不是應用內建資源,而是外部檔案或網路圖片,只要把 setResource() 換成 setBitmap() 即可,其餘邏輯完全不變。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved