android圖像繪制(四)自定義一個(gè)SurfaceView控件
更新時(shí)間:2013年01月17日 10:25:11 作者:
自定義控件(類(lèi)似按鈕等)的使用,自定義一個(gè)SurfaceView。如某一塊的動(dòng)態(tài)圖(自定義相應(yīng)),或者類(lèi)似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下
自定義控件(類(lèi)似按鈕等)的使用,自定義一個(gè)SurfaceView。
如某一塊的動(dòng)態(tài)圖(自定義相應(yīng)),或者類(lèi)似UC瀏覽器下面的工具欄。
如下圖示例:
自定義類(lèi)代碼:
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)聽(tīng)
*/
@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視圖消亡時(shí),響應(yīng)此函數(shù)
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
}
}
layout的xml代碼如下(使用方法,類(lèi)的全地址做為控件名):
<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>
以上代碼為例子,僅供參考!
注意以下問(wèn)題:
1、本類(lèi)的初始化函數(shù)需要加入?yún)?shù),為:public ImageSurfaceView(Context context, AttributeSet attrs) ;
2、不要在初始化的時(shí)候獲取screen的寬度和高度,在初始化的時(shí)候并還沒(méi)有執(zhí)行SurfaceCreated,所以獲取寬度和高度要在surfaceCreated或者之后,且在surfaceDestroyed之前;
3、在顯示本控件的時(shí)候,會(huì)執(zhí)行surfaceCreated和surfaceChanged,當(dāng)跳轉(zhuǎn)到其他界面的時(shí)候則執(zhí)行surfaceDestroyed(不管是否當(dāng)前的activity已經(jīng)銷(xiāo)毀),所以如果在跳轉(zhuǎn)回到次控件的時(shí)候立刻執(zhí)行sfh.lockCanvas()的話將會(huì)獲得空值Null。
如某一塊的動(dòng)態(tài)圖(自定義相應(yīng)),或者類(lèi)似UC瀏覽器下面的工具欄。
如下圖示例:
自定義類(lèi)代碼:
復(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)聽(tīng)
*/
@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視圖消亡時(shí),響應(yīng)此函數(shù)
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
System.out.println("ImageSurfaceView is surfaceDestroyed");
}
}
layout的xml代碼如下(使用方法,類(lèi)的全地址做為控件名):
復(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>
以上代碼為例子,僅供參考!
注意以下問(wèn)題:
1、本類(lèi)的初始化函數(shù)需要加入?yún)?shù),為:public ImageSurfaceView(Context context, AttributeSet attrs) ;
2、不要在初始化的時(shí)候獲取screen的寬度和高度,在初始化的時(shí)候并還沒(méi)有執(zhí)行SurfaceCreated,所以獲取寬度和高度要在surfaceCreated或者之后,且在surfaceDestroyed之前;
3、在顯示本控件的時(shí)候,會(huì)執(zhí)行surfaceCreated和surfaceChanged,當(dāng)跳轉(zhuǎn)到其他界面的時(shí)候則執(zhí)行surfaceDestroyed(不管是否當(dāng)前的activity已經(jīng)銷(xiāo)毀),所以如果在跳轉(zhuǎn)回到次控件的時(shí)候立刻執(zhí)行sfh.lockCanvas()的話將會(huì)獲得空值Null。
您可能感興趣的文章:
- Android編程使用自定義shape實(shí)現(xiàn)shadow陰影效果的方法
- Android 自定義陰影效果詳解及實(shí)例
- Android自定義控件ImageView實(shí)現(xiàn)點(diǎn)擊之后出現(xiàn)陰影效果
- android自定義Dialog彈框和背景陰影顯示效果
- android 自定義控件 自定義屬性詳細(xì)介紹
- android自定義倒計(jì)時(shí)控件示例
- 輕松實(shí)現(xiàn)可擴(kuò)展自定義的Android滾輪時(shí)間選擇控件
- Android自定義表格控件滿足人們對(duì)視覺(jué)的需求
- android自定義按鈕示例(重寫(xiě)imagebutton控件實(shí)現(xiàn)圖片按鈕)
- Android實(shí)現(xiàn)萬(wàn)能自定義陰影控件實(shí)例代碼
相關(guān)文章
Android自定義ViewGroup實(shí)現(xiàn)彈性滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)彈性滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android實(shí)現(xiàn)菜單關(guān)聯(lián)activity的方法示例
這篇文章主要介紹了Android實(shí)現(xiàn)菜單關(guān)聯(lián)activity的方法,涉及Android使用Intent實(shí)現(xiàn)菜單關(guān)聯(lián)activity相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android獲取arrays.xml里的數(shù)組字段值實(shí)例詳解
這篇文章主要介紹了Android獲取arrays.xml里的數(shù)組字段值實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能
這篇文章主要為大家詳細(xì)介紹了Android SQLite數(shù)據(jù)庫(kù)連接實(shí)現(xiàn)登錄功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
深入理解Android中Scroller的滾動(dòng)原理
今天給大家講解的是Scroller類(lèi)的滾動(dòng)實(shí)現(xiàn)原理,可能很多朋友不太了解該類(lèi)是用來(lái)干嘛的,但是研究Launcher的朋友應(yīng)該對(duì)他很熟悉,Scroller類(lèi)是滾動(dòng)的一個(gè)封裝類(lèi),可以實(shí)現(xiàn)View的平滑滾動(dòng)效果,而我們今天就來(lái)探究一下為什么Scroller能夠?qū)崿F(xiàn)平滑滾動(dòng)。2016-08-08
Android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾
這篇文章主要介紹了android 開(kāi)發(fā)中l(wèi)ayout下的子文件夾,需要的朋友可以參考下2017-12-12
Android 8.0版本更新無(wú)法自動(dòng)安裝問(wèn)題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android 8.0版本更新無(wú)法自動(dòng)安裝問(wèn)題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Android UI系列-----Dialog對(duì)話框示例
本篇文章主要介紹了Android UI系列-----Dialog對(duì)話框示例,在界面上彈出一個(gè)Dialog對(duì)話框使我們經(jīng)常需要做的,有需要的可以了解一下。2017-01-01

