android圖像繪制(四)自定義一個SurfaceView控件
更新時間:2013年01月17日 10:25:11 作者:
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。
如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄。
如下圖示例:
自定義類代碼:
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;
/**圖片的坐標(biāo)*/
private float imageX, imageY;
/**獲取的圖片*/
private Bitmap bmp;
/**圖片寬高*/
private float bmpW, bmpH;
/**屏幕大小*/
private int screenW, screenH;
/**
* SurfaceView初始化函數(shù)
*/
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視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@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);
}
}
/**
* 觸屏事件監(jiān)聽
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
/**
* 圖片的線程運(yùn)行
*/
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視圖狀態(tài)發(fā)生改變,響應(yīng)此函數(shù)
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
System.out.println("ImageSurfaceView is surfaceChanged");
}
/**
* SurfaceView視圖消亡時,響應(yīng)此函數(shù)
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
}
}
layout的xml代碼如下(使用方法,類的全地址做為控件名):
<FrameLayout xmlns:android="http://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>
以上代碼為例子,僅供參考!
注意以下問題:
1、本類的初始化函數(shù)需要加入?yún)?shù),為:public ImageSurfaceView(Context context, AttributeSet attrs) ;
2、不要在初始化的時候獲取screen的寬度和高度,在初始化的時候并還沒有執(zhí)行SurfaceCreated,所以獲取寬度和高度要在surfaceCreated或者之后,且在surfaceDestroyed之前;
3、在顯示本控件的時候,會執(zhí)行surfaceCreated和surfaceChanged,當(dāng)跳轉(zhuǎn)到其他界面的時候則執(zhí)行surfaceDestroyed(不管是否當(dāng)前的activity已經(jīng)銷毀),所以如果在跳轉(zhuǎn)回到次控件的時候立刻執(zhí)行sfh.lockCanvas()的話將會獲得空值Null。
如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄。
如下圖示例:

自定義類代碼:
復(fù)制代碼 代碼如下:
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;
/**圖片的坐標(biāo)*/
private float imageX, imageY;
/**獲取的圖片*/
private Bitmap bmp;
/**圖片寬高*/
private float bmpW, bmpH;
/**屏幕大小*/
private int screenW, screenH;
/**
* SurfaceView初始化函數(shù)
*/
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視圖創(chuàng)建,響應(yīng)此函數(shù)
*/
@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);
}
}
/**
* 觸屏事件監(jiān)聽
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
return true;
}
/**
* 圖片的線程運(yùn)行
*/
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視圖狀態(tài)發(fā)生改變,響應(yīng)此函數(shù)
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
System.out.println("ImageSurfaceView is surfaceChanged");
}
/**
* SurfaceView視圖消亡時,響應(yīng)此函數(shù)
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
}
}
layout的xml代碼如下(使用方法,類的全地址做為控件名):
復(fù)制代碼 代碼如下:
<FrameLayout xmlns:android="http://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>
以上代碼為例子,僅供參考!
注意以下問題:
1、本類的初始化函數(shù)需要加入?yún)?shù),為:public ImageSurfaceView(Context context, AttributeSet attrs) ;
2、不要在初始化的時候獲取screen的寬度和高度,在初始化的時候并還沒有執(zhí)行SurfaceCreated,所以獲取寬度和高度要在surfaceCreated或者之后,且在surfaceDestroyed之前;
3、在顯示本控件的時候,會執(zhí)行surfaceCreated和surfaceChanged,當(dāng)跳轉(zhuǎn)到其他界面的時候則執(zhí)行surfaceDestroyed(不管是否當(dāng)前的activity已經(jīng)銷毀),所以如果在跳轉(zhuǎn)回到次控件的時候立刻執(zhí)行sfh.lockCanvas()的話將會獲得空值Null。
您可能感興趣的文章:
- Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法
- Android 自定義陰影效果詳解及實(shí)例
- Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后出現(xiàn)陰影效果
- android自定義Dialog彈框和背景陰影顯示效果
- android 自定義控件 自定義屬性詳細(xì)介紹
- android自定義倒計時控件示例
- 輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時間選擇控件
- Android自定義表格控件滿足人們對視覺的需求
- android自定義按鈕示例(重寫imagebutton控件實(shí)現(xiàn)圖片按鈕)
- Android實(shí)現(xiàn)萬能自定義陰影控件實(shí)例代碼
相關(guān)文章
Android開發(fā)之Activity管理工具類完整示例
這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實(shí)例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android中關(guān)于遞歸和二分法的算法實(shí)例代碼
這篇文章主要介紹了Android中關(guān)于遞歸和二分法的算法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果
這篇文章主要介紹了Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果,涉及Android圖形繪制及顯示的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10Android開發(fā)中Activity屬性設(shè)置小結(jié)
Android應(yīng)用開發(fā)中會經(jīng)常遇到Activity組件的使用,下面就來講解下Activity組件。Activity的生命周期、通信方式和IntentFilter等內(nèi)容,并提供了一些日常開發(fā)中經(jīng)常用到的關(guān)于Activity的技巧和方法。通過本文,你可以進(jìn)一步了接Android中Activity的運(yùn)作方式。2015-05-05利用Android畫圓弧canvas.drawArc()實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等)
這篇文章主要介紹了Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等),以簡單實(shí)例形式分析了Android實(shí)現(xiàn)全屏、豎屏及一直顯示等的技巧與注意事項,需要的朋友可以參考下2015-10-10android monkey自動化測試改為java調(diào)用monkeyrunner Api
一般情況下我們使用android中的monkeyrunner進(jìn)行自動化測試時,使用的是python語言來寫測試腳本。不過,最近發(fā)現(xiàn)可以用java調(diào)用monkeyrunner Api,于是,就簡單研究了一下。這里做一些總結(jié)。希望有對在研究的午飯可以有所用處2012-11-11Android 獲取應(yīng)用緩存大小與清除緩存的方法
今天小編就為大家分享一篇Android 獲取應(yīng)用緩存大小與清除緩存的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08