Android 프로 그래 밍 의 Surface View 실례 상세 설명
4511 단어 AndroidSurfaceView
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>
운행!더 많은 안 드 로 이 드 관련 내용 에 관심 이 있 는 독자 들 은 본 사이트 의 주 제 를 볼 수 있다.
본 고 에서 말 한 것 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.