1. 定義
在Android系統(tǒng)中,有一種特殊的視圖艺普,稱為SurfaceView簸州,它擁有獨(dú)立的繪圖表面,即它不與其宿主窗口共享同一個(gè)繪圖表面歧譬。由于擁有獨(dú)立的繪圖表面岸浑,因此SurfaceView的UI可以在一個(gè)獨(dú)立的線程中進(jìn)行繪制,且不會占用主線程資源瑰步。SurfaceView一方面可以實(shí)現(xiàn)復(fù)雜而高效的UI矢洲,另一方面又不會導(dǎo)致用戶輸入無法得到響應(yīng)。
2. view缩焦、surfaceview和GLsurfaceview的區(qū)別
- View:顯示視圖读虏,內(nèi)置畫布,提供圖形繪制函數(shù)袁滥、觸屏事件盖桥、按鍵事件函數(shù)等;必須在UI主線程內(nèi)更新畫面题翻,速度較慢揩徊。
- SurfaceView : 基于view視圖進(jìn)行拓展的視圖類,更適合2D游戲的開發(fā);是view的子類塑荒,類似使用雙緩機(jī)制熄赡,在新的線程中更新畫面所以刷新界面速度比view快。
- GLSurfaceView:基于SurfaceView視圖再次進(jìn)行拓展的視圖類袜炕,專用于3D游戲開發(fā)的視圖本谜;是SurfaceView的子類,openGL專用偎窘。
3. Surface的使用
- Surface與MediaPlayer實(shí)現(xiàn)視頻播放
public class SurfaceViewActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private MediaPlayer mediaPlayer;
private static final String VIDEO_PATH = "https://inducesmile.com/wp-content/uploads/2016/05/small.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surface_view);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
//獲取到SurfaceHolder對象
surfaceHolder = surfaceView.getHolder();
//設(shè)置其生命周期監(jiān)聽
surfaceHolder.addCallback(this);
}
//創(chuàng)建SurfaceView時(shí)被調(diào)用
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDisplay(surfaceHolder); //將SurfaceView與MediaPlayer進(jìn)行綁定
try {
mediaPlayer.setDataSource(VIDEO_PATH);
mediaPlayer.prepare();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (IOException e) {
e.printStackTrace();
}
}
//SurfaceView大小尺寸改變時(shí)調(diào)用乌助,比如橫豎屏切換
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
//SurfaceView被銷毀時(shí)被調(diào)用
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseMediaPlayer();
}
//音頻播放準(zhǔn)備好后,開始播放
@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
@Override
protected void onPause() {
super.onPause();
releaseMediaPlayer();
}
@Override
protected void onStop() {
super.onStop();
releaseMediaPlayer();
}
//釋放MediaPlayer對象
private void releaseMediaPlayer(){
if (mediaPlayer != null){
mediaPlayer.release();
mediaPlayer = null;
}
}
}
首先根據(jù)SurfaceView獲取到其SurfaceHolder對象陌知,然后添加SurfaceHolder的監(jiān)聽事件他托,在SurfaceView被創(chuàng)建時(shí),調(diào)用mediaPlayer.setDisplay(surfaceHolder);
將SurfaceView和MediaPlayer進(jìn)行綁定仆葡。
- SurfaceView實(shí)現(xiàn)動畫效果
public class SinSurfaceView extends SurfaceView implements Runnable{
private SurfaceHolder mSurfaceHolder;
private Paint mPaint; //畫筆對象
private Path mPath; //路徑
public SinSurfaceView(Context context) {
super(context);
init();
}
public SinSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public SinSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
//初始化
private void init(){
//設(shè)置畫筆
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.GREEN);
mPaint.setStrokeJoin(Paint.Join.ROUND);
//設(shè)置路徑
mPath = new Path();
//獲取到SurfaceHolder對象
mSurfaceHolder = getHolder();
//設(shè)置其監(jiān)聽事件
mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
//開啟線程赏参,進(jìn)行繪制
new Thread(SinSurfaceView.this).start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isDrawing = false; //設(shè)置為false,表示停止繪制
mSurfaceHolder.removeCallback(this); //將callback移除
}
});
}
//是否進(jìn)行繪制標(biāo)識沿盅,默認(rèn)為false
private volatile boolean isDrawing = true;
private int drawX, drawY;
@Override
public void run() {
//當(dāng)允許繪制時(shí)把篓,則不斷進(jìn)行繪制
while (isDrawing){
drawX ++;
drawY = (int)(100*Math.sin(drawX*2*Math.PI / 180) + 400);
mPath.lineTo(drawX, drawY);
draw();
}
}
//將path對象繪制到canvas中
private void draw(){
Canvas canvas = null;
try {
//獲取到canvas對象
canvas = mSurfaceHolder.lockCanvas();
if (canvas != null){
canvas.drawColor(Color.WHITE);
canvas.drawPath(mPath, mPaint);
}
} finally {
if (canvas != null){
//釋放canvas對象
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
}