203-設定背景桌布(行動裝置進階應用)

1. AndroidManifest.xml
  • 重點程式碼
<uses-permission android:name="android.permission.SET_WALLPAPER" />
2. main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/app_name" />

    <ImageSwitcher
        android:id="@+id/imageSwitcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:spacing="16dp" />

</RelativeLayout>

3. GAD02.java

import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import android.widget.Gallery.LayoutParams;

public class GAD02 extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

protected static InputStream is;
private ImageSwitcher mSwitcher;
private Context mContext;

/* 設定在Gallery的圖檔 */
private Integer[] mThumbIds = { R.drawable.p01, R.drawable.p02,
R.drawable.p03, R.drawable.p04, R.drawable.p05, R.drawable.p06,
R.drawable.p07, R.drawable.p08, R.drawable.p09, R.drawable.p10 };

/* 設定在Switcher的圖檔 */
private Integer[] mImageIds = { R.drawable.p01, R.drawable.p02,
R.drawable.p03, R.drawable.p04, R.drawable.p05, R.drawable.p06,
R.drawable.p07, R.drawable.p08, R.drawable.p09, R.drawable.p10 };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);

/* 設定Switcher */
mSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
mSwitcher.setFactory(this);
/* 設定淡入淡出效果 */
mSwitcher.setAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));

Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
/* 設定按下Gallery的圖片事件 */
g.setOnItemClickListener(new Gallery.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
final int position, long id) {
new AlertDialog.Builder(GAD02.this)
.setTitle(R.string.app_about)
.setMessage(R.string.app_about_msg)
.setIcon(mThumbIds[position])
.setPositiveButton(R.string.str_ok,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface arg0,
int arg1) {
try {
is = GAD02.this
.getBaseContext()
.getResources()
.openRawResource(
mThumbIds[position]);

GAD02.this.setWallpaper(is);
Toast.makeText(GAD02.this,
R.string.my_text_pre,
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
}
}
})
.setNegativeButton(R.string.my_text_no,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
Toast.makeText(GAD02.this,
R.string.my_text_no,
Toast.LENGTH_SHORT).show();
}

}).show();
}
});
}

/* 呈現Switcher的模式 */
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
return i;
}

public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}

public int getCount() {
return mImageIds.length;
}

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

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

/* 顯示Gallery裡的模式 */
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
return i;
}
}

@Override
public void onItemSelected(AdapterView<?> adapterview, View view,
int position, long arg3) {
mSwitcher.setImageResource(mImageIds[position]);
}

@Override
public void onNothingSelected(AdapterView<?> adapterview) {
}
}

沒有留言: