欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android圖像繪制(四)自定義一個SurfaceView控件

 更新時間:2013年01月17日 10:25:11   作者:  
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。如某一塊的動態(tài)圖(自定義相應(yīng)),或者類似UC瀏覽器下面的工具欄,感興趣的朋友可以了解下
自定義控件(類似按鈕等)的使用,自定義一個SurfaceView。
如某一塊的動態(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。

相關(guān)文章

  • Android開發(fā)之Activity管理工具類完整示例

    Android開發(fā)之Activity管理工具類完整示例

    這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實(shí)例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • Android中關(guān)于遞歸和二分法的算法實(shí)例代碼

    Android中關(guān)于遞歸和二分法的算法實(shí)例代碼

    這篇文章主要介紹了Android中關(guān)于遞歸和二分法的算法實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • popupwindow焦點(diǎn)問題解決方案

    popupwindow焦點(diǎn)問題解決方案

    在android 開發(fā)過程中,總會遇到一些問題,比如popupwindow焦點(diǎn)問題等等,我們該如何解決呢?需要的朋友可以了解下
    2012-11-11
  • Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果

    Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果

    這篇文章主要介紹了Android編程簡單實(shí)現(xiàn)雷達(dá)掃描效果,涉及Android圖形繪制及顯示的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android開發(fā)中Activity屬性設(shè)置小結(jié)

    Android開發(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í)例詳解

    利用Android畫圓弧canvas.drawArc()實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等)

    Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等)

    這篇文章主要介紹了Android中Activity常用功能設(shè)置小結(jié)(包括全屏、橫豎屏等),以簡單實(shí)例形式分析了Android實(shí)現(xiàn)全屏、豎屏及一直顯示等的技巧與注意事項,需要的朋友可以參考下
    2015-10-10
  • android monkey自動化測試改為java調(diào)用monkeyrunner Api

    android monkey自動化測試改為java調(diào)用monkeyrunner Api

    一般情況下我們使用android中的monkeyrunner進(jìn)行自動化測試時,使用的是python語言來寫測試腳本。不過,最近發(fā)現(xiàn)可以用java調(diào)用monkeyrunner Api,于是,就簡單研究了一下。這里做一些總結(jié)。希望有對在研究的午飯可以有所用處
    2012-11-11
  • Android仿淘寶物流追蹤的實(shí)例代碼

    Android仿淘寶物流追蹤的實(shí)例代碼

    本篇文章主要介紹了Android仿淘寶物流追蹤的實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Android 獲取應(yīng)用緩存大小與清除緩存的方法

    Android 獲取應(yīng)用緩存大小與清除緩存的方法

    今天小編就為大家分享一篇Android 獲取應(yīng)用緩存大小與清除緩存的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08

最新評論