Android編程之SurfaceView執行個體詳解_Android

來源:互聯網
上載者:User

本文執行個體講述了Android編程之SurfaceView用法。分享給大家供大家參考,具體如下:

關於surfaceView相關知識:

View和SurfaceView主要區別:

1. View只能在UI線程中重新整理,而SurfaceView可以在子線程中重新整理

2. SurfaceView可以控制重新整理頻率

SurfaceView幾個重要的方法:

1. 繼承SurfaceView 後調用getHolder()方法可以擷取到mSurfaceHolder對象這個對於可以控制SurfaceView的繪製

2. 實現這個SurfaceHolder.Callback介面並且mSurfaceHolder.addCallback(this)添加回調可以感知到SurfaceView的生命週期

3. 繪製的時候mCanvas.drawColor(Color.BLACK);這個方法很重要,這個方法是清理上一次繪製的東西,這個方法一定要調用才能看到效果

實現效果 如下:

第一步:建立XRSurfaceView繼承SurfaceView

package com.rong.activity;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceView;/** * 自訂SurfaceView *  * @author 徐榮 * */public class XRSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {  // SurfaceView的寬  int surfaceWidth;  // SurfaceView的高  int surfaceHeight;  // SurfaceHolder對象  SurfaceHolder mSurfaceHolder;  // 開關線程的標誌位  boolean isRunning = true;  // 畫筆  Paint mPaint;  // 圓的半徑  float radius = 0;  // 圓是變大還是縮小的狀態  boolean status = true;  // 圓變化的速度  int mSpeed = 3;  public XRSurfaceView(Context context, AttributeSet attrs) {    super(context, attrs);    initView();  }  private void initView() {    // 擷取mSurfaceHolder    mSurfaceHolder = getHolder();    // 添加回調    mSurfaceHolder.addCallback(this);  }  @Override  public void surfaceCreated(SurfaceHolder holder) {    isRunning = true;    // 初始化畫筆    mPaint = new Paint();    mPaint.setAntiAlias(true);    mPaint.setColor(Color.BLUE);    // 開啟繪製線程    new Thread(this).start();  }  @Override  public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {    // 擷取surface的寬    surfaceWidth = width;    // 擷取surface的高    surfaceHeight = height;  }  @Override  public void surfaceDestroyed(SurfaceHolder holder) {    // 關閉繪製線程    isRunning = false;  }  @Override  public void run() {    Canvas mCanvas = null;    while (isRunning) {      try {        // 鎖定canva進行繪製        mCanvas = mSurfaceHolder.lockCanvas(null);        // 這個方法很重要,相當於重繪(一定要調用不然看不到效果)        mCanvas.drawColor(Color.BLACK);        // 畫圓        mCanvas.drawCircle((surfaceWidth / 2), (surfaceHeight / 2), radius, mPaint);        // 更改半徑變數        if (status) {          radius = radius + mSpeed;          if (radius > 200) {            status = false;          }        } else {          radius = radius - mSpeed;          if (radius < 0) {            status = true;          }        }      } catch (Exception e) {        e.printStackTrace();      } finally {        // 解除畫布鎖        mSurfaceHolder.unlockCanvasAndPost(mCanvas);      }    }  }}

第二步:建立布局檔案activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="#ffffff"  android:orientation="vertical" >  <com.rong.activity.XRSurfaceView    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_centerInParent="true"    android:orientation="vertical" /></RelativeLayout>

運行!

更多關於Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控制項用法總結》

希望本文所述對大家Android程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.