編輯:關於 Android 程式設計
讓使用者從預先準備的圖片裡挑一張設為桌布,是入門階段很值得動手做的小功能。Android 提供了三條路徑可以設定壁紙:
WallpaperManager.setResource(int resourceID)——直接指定 drawable 資源WallpaperManager.setBitmap(Bitmap bitmap)——傳入已解碼的圖像物件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,主要是為了它內建的切換動畫效果。
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;
}
}
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();
}
}
}
第一,一定要實作它的工廠介面 ViewFactory,其中的 makeView() 和 BaseAdapter 的 getView() 作用相同,都是回傳一個要顯示的 View。第二,動畫效果需要自行透過 setInAnimation() 與 setOutAnimation() 指定,這裡直接沿用系統預設的淡入淡出。




如果桌布來源不是應用內建資源,而是外部檔案或網路圖片,只要把 setResource() 換成 setBitmap() 即可,其餘邏輯完全不變。
Android 圖像處理:底片(濾鏡)效果的實現原理
底片效果是如何實現的?我們先來看代碼。 // 底片 public static Bitmap Negative(Bitmap bm) { int Width
用 WIFI 讓 Android 手機與 Android Studio 連線,實機測試 App
想擺脫 USB 線、用手機無線連線做實機測試嗎?以下步驟示範如何透過 WIFI 完成設定。 1、讓手機取得 ROOT 權限(例如小米 note,直接下載安裝 MIUI 開
Android 主題切換實戰:白天與夜間模式怎麼做
智慧型手機迅速普及,也大幅改變了我們的生活習慣。很多人習慣睡前滑一會兒手機,但一般應用的日間模式亮度偏高,對眼睛其實是種負擔。因此現在的應用多半會提供「日間/夜間」兩套模
仿 QQ 側滑刪除:一套 ListView、RecyclerView 與任意 ViewGroup 都能用的通用解耦方案
側滑刪除已經是列表頁的標配交互,我自己也一直想動手做一個類似 QQ 的版本。自定義 View 我摸過一段時間,但自定義 ViewGroup 的實戰經驗偏少,而側滑刪除偏偏