Android 프로 그래 밍 의 Surface View 실례 상세 설명

4511 단어 AndroidSurfaceView
이 글 의 실례 는 안 드 로 이 드 프로 그래 밍 의 Surface View 용법 을 설명 하 였 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
surfaceView 지식:
View 와 SurfaceView 의 주요 차이 점:
1.View 는 UI 스 레 드 에서 만 새로 고침 할 수 있 고 Surface View 는 하위 스 레 드 에서 새로 고침 할 수 있 습 니 다.
2.SurfaceView 는 새로 고침 주파 수 를 조절 할 수 있 습 니 다.
Surface View 의 몇 가지 중요 한 방법:
1.Surface View 를 계승 한 후 getHolder()방법 을 사용 하면 mSurface Holder 대상 을 얻 을 수 있 습 니 다.이것 은 Surface View 를 제어 할 수 있 는 그림 입 니 다.
2.이 Surface Holder.Callback 인 터 페 이 스 를 실현 하고 mSurface Holder.addCallback(this)에 리 셋 을 추가 하면 Surface View 의 생명 주 기 를 감지 할 수 있 습 니 다.
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);
      }
    }
  }
}

두 번 째 단계:새 레이아웃 파일 activitymain.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>

운행!
더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기