編輯:關於 Android 程式設計
更換壁紙涉及三件事:取得目前的壁紙、從一組圖片中挑選、把選中的圖片設為壁紙。Android 把這些能力集中在 Context 的 setWallpaper()/getWallpaper()/clearWallpaper() 三個方法上(API 5 之後另有 WallpaperManager 作為更完整的替代方案)。
本文用一個 Gallery 展示候選圖片,搭配三顆按鈕完成完整流程。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/clearWall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="恢復默認牆紙" />
<ImageView
android:id="@+id/currWall"
android:layout_width="100px"
android:layout_height="150px"
android:layout_gravity="center_horizontal" />
<Button
android:id="@+id/getWall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="獲取當前牆紙" />
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/setWall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="設置為當前牆紙" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.ljq.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WallActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<!-- 設定壁紙的權限 -->
<uses-permission android:name="android.permission.SET_WALLPAPER" />
</manifest>
package com.ljq.activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class WallAdapter extends BaseAdapter {
private int[] imgIds = null;
private Context context = null;
public WallAdapter(int[] imgIds, Context context) {
super();
this.imgIds = imgIds;
this.context = context;
}
public int getCount() { return imgIds.length; }
public Object getItem(int position) {
return imgIds[position % imgIds.length]; // 取餘數即可循環
}
public long getItemId(int position) { return position; }
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setBackgroundResource(imgIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new Gallery.LayoutParams(120, 120));
return imageView;
}
}
CENTER_CROP 的縮放模式會保持比例並填滿整個檢視,多餘的部分裁掉,用在等尺寸的縮圖網格上視覺效果最整齊。
package com.ljq.activity;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemSelectedListener;
public class WallActivity extends Activity {
private int[] imgIds = {R.drawable.w1, R.drawable.w2, R.drawable.w3, R.drawable.w4};
private int selectIndex = -1; // 目前選中圖片的索引
private ImageView currWall = null;
private Gallery gallery = null;
private Button clearWall = null;
private Button getWall = null;
private Button setWall = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new WallAdapter(imgIds, WallActivity.this));
gallery.setSpacing(5);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectIndex = position; // 記錄被選中的索引
}
public void onNothingSelected(AdapterView<?> parent) { }
});
currWall = (ImageView) findViewById(R.id.currWall);
clearWall = (Button) findViewById(R.id.clearWall);
getWall = (Button) findViewById(R.id.getWall);
setWall = (Button) findViewById(R.id.setWall);
clearWall.setOnClickListener(listener);
getWall.setOnClickListener(listener);
setWall.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button btn = (Button) v;
switch (btn.getId()) {
case R.id.clearWall:
// 還原為系統預設壁紙
try {
WallActivity.this.clearWallpaper();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.getWall:
// 取得目前壁紙並顯示在 ImageView
currWall.setBackgroundDrawable(getWallpaper());
break;
case R.id.setWall:
// 把選中的資源轉為串流後設為壁紙
InputStream in = WallActivity.this.getResources().openRawResource(imgIds[selectIndex]);
try {
setWallpaper(in);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
};
}

getWallpaper() 回傳的是 Drawable,可直接交給 ImageView 顯示;setWallpaper() 接受的是 InputStream,因此從資源取得圖片時必須先透過 openRawResource() 轉成串流。若圖片來源是網路或大型檔案,串流方式也能避免一次性把整張 Bitmap 載入記憶體而引發 OOM。
另外提醒兩點:宣告 SET_WALLPAPER 權限是必要條件,缺少它會直接拋出安全性例外;Gallery 與 setBackgroundDrawable() 都已被標記為 deprecated,新專案請改用 RecyclerView 搭配 Gallery 樣式的 LayoutManager,並以 setBackground() 取代。若使用 API 5 以上,AppCompatDelegate 或 WallpaperManager.getInstance().setStream() 是更明確的寫法。
Android WebView 使用方法詳解(附 JS 交互調用)
目前很多 Android App 都內置了可以顯示網頁的介面,你會發現這個介面一般都是由一個叫做 WebView 的元件渲染出來的。學會這個元件,能為你的 App 開發大
React Native 實戰:用 Navigator 實現頁面跳轉
主界面開發 上一節已完成首頁的開發,這節繼續完成主界面,也就是添加底部「首頁」與「我的」兩個 tabbar。 在 js 文件夾下新建 MainScene.js: impo
Android進階教學:Splash啟動頁的版本更新業務邏輯
本單元聚焦於Android進階開發中常見的Splash(啟動引導頁)設計,重點在於梳理啟動頁在「版本更新」情境下的業務判斷流程:應用啟動時,Splash頁會先確認目前的版
Android 常見 Intent Action 用法整理
Intent 是 Android 元件之間溝通的重要機制,而 Action 則用來描述要執行的操作類型。以下整理開發中較常使用的系統 Action。啟動與撥號相關ACTI