編輯:Android 開發實例
當標準的 View 無法滿足需求時,自訂元件就成了開發者的好幫手。這一篇就來談談如何封裝一個可重複使用的 SurfaceView,無論是想呈現某個區域的動態畫面,還是打造類似 UC 瀏覽器底部的工具列,都能派上用場。

下面的程式碼示範了一個名為 ImageSurfaceView 的類別,它繼承 SurfaceView 並實作 Callback 介面,透過 SurfaceHolder 管理畫布、搭配 Handler 與 Runnable 完成週期性的重繪:
public class ImageSurfaceView extends SurfaceView implements Callback{
//用於控制SurfaceView
private SurfaceHolder sfh;
private Handler handler = new Handler();
private ImageRunnable imageRunnable = new ImageRunnable();
private Paint paint;
private Canvas canvas;
private Matrix matrix;
/**圖片的坐標*/
private float imageX, imageY;
/**獲取的圖片*/
private Bitmap bmp;
/**圖片寬高*/
private float bmpW, bmpH;
/**屏幕大小*/
private int screenW, screenH;
/**
* SurfaceView初始化函數
*/
public ImageSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
sfh = this.getHolder();
sfh.addCallback(this);
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
setFocusable(true);
}
/**
* SurfaceView視圖創建,響應此函數
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceCreated");
screenH = this.getHeight();
screenW = this.getWidth();
handler.post(imageRunnable);
}
/**
* 游戲繪圖
*/
public void draw() {
try {
canvas = sfh.lockCanvas();
canvas.drawRGB(0, 0, 0);
canvas.save();
//繪制
canvas.drawBitmap(bmp, matrix, paint);
System.out.println("繪制圖像了嗎?");
canvas.restore();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (canvas != null)
sfh.unlockCanvasAndPost(canvas);
}
}
/**
* 觸屏事件監聽
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
/**
* 圖片的線程運行
*/
class ImageRunnable implements Runnable{
@Override
public void run() {
long start = System.currentTimeMillis();
draw();
long end = System.currentTimeMillis();
if (end - start < 500) {
handler.postDelayed(this, 200 - (end-start));
}else{
handler.post(this);
}
}
}
/**
* SurfaceView視圖狀態發生改變,響應此函數
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
System.out.println("ImageSurfaceView is surfaceChanged");
}
/**
* SurfaceView視圖消亡時,響應此函數
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
}
}
在 XML 版面檔中,直接用類別的完整路徑作為標籤名稱即可把自訂元件放進來,並在下方搭配一列按鈕:
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <akai.test.getImage.ImageSurfaceView android:id="@+id/myImageView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <LinearLayout android:id="@+id/buttons" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="@android:color/white" > <Button android:id="@+id/getImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="選擇圖片" /> <Button android:id="@+id/getImage_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定" /> <Button android:id="@+id/getImage_cancle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" /> </LinearLayout> </FrameLayout>
以上僅為示範用途,實際專案請依需求調整。
public ImageSurfaceView(Context context, AttributeSet attrs),否則無法從 XML 正確 inflate。sfh.lockCanvas(),很可能拿到 Null 而拋出例外。
把網頁連同圖片一起保存到本地的實現方案
做離線閱讀功能時,會遇到一個不算難但很瑣碎的需求:把一個帶圖片的網頁下載到本地,並讓它在沒有網絡時也能正常顯示。難點不在保存 HTML,而在於把裡面的圖片鏈路一併改成本地
Android Dialog 去除白色邊框的實作思路與程式碼
用系統預設的 Dialog 樣式時,四周總會帶一圈白框,很多時候並不是我們想要的。要拿掉它,正解不是去改圖片,而是覆寫樣式。 第一步:自訂 style 在 values
Android 多語系支援:strings.xml 與 values-it 目錄
Android 會依裝置語言選擇對應資源。只要建立正確的 values 目錄,就能讓同一套應用顯示不同語言。 建立語系資料夾 在 res 下新增 values-it、va
一個 App 打包多個廣告平台版本的完整方案
Android 的廣告平台多如牛毛,而各大應用地區分市場對平台的接受度並不一致。開發者要接入廣告,通常繞不開兩個問題: 接哪個平台更賺錢? 接哪個平台不會被市場拒絕?