溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android基礎(chǔ)之SurfaceView視圖中對(duì)畫布的3種刷新方法

發(fā)布時(shí)間:2020-06-10 19:01:37 來源:網(wǎng)絡(luò) 閱讀:4931 作者:hagar 欄目:移動(dòng)開發(fā)

//MysurfaceView.java

package com.example.surfaceview;


import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceHolder.Callback;

import android.view.SurfaceView;


public class MysurfaceView extends SurfaceView implements Callback{

private SurfaceHolder sfh;

private Paint paint;

private int textX=10,textY=10;

public MysurfaceView(Context context)

{

super(context);

sfh=this.getHolder();//控制SurfaceView的大小 格式 主要用于監(jiān)聽SurfaceView的狀態(tài)

//其實(shí)surfaceView只是保存當(dāng)前視圖的像素?cái)?shù)據(jù) 在使用surfaceView時(shí)  并不會(huì)與surfaceview直接打交道

//而是通過surfaceHolder來控制 使用surfaceHolder的lockCanvas()函數(shù)獲取到surfaceView的

//canvas對(duì)象 。在通過在canvas上繪制內(nèi)容來修改surfaceView中的數(shù)據(jù)

//locakCanvas不僅獲取canvas  還對(duì)獲取的canvas進(jìn)行加鎖

sfh.addCallback(this);//將其監(jiān)聽接口實(shí)例傳入..完成對(duì)surfaceView的監(jiān)聽

paint=new Paint();

paint.setColor(Color.WHITE);

setFocusable(true);

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

// TODO Auto-generated method stub

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

// TODO Auto-generated method stub

myDraw();

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

// TODO Auto-generated method stub

}

public void myDraw()

{

Canvas canvas=sfh.lockCanvas();

canvas.drawRect(0, 0,this.getWidth(),this.getHeight(),paint);//the first method 繪制一個(gè)和手機(jī)屏幕一樣大小的矩形

canvas.drawColor(Color.BLACK);//the second methos繪制顏色填充整個(gè)屏幕

canvas.drawRGB(0,0,0);//the third method 繪制顏色填充整個(gè)屏幕 

canvas.drawText("game", textX, textY, paint);

sfh.unlockCanvasAndPost(canvas);

}

public boolean onTouchEvent(MotionEvent event)

{

textX=(int)event.getX();

textY=(int)event.getY();

myDraw();

invalidate();

return true;

}

}

//MainActivity.java如下:

package com.example.surfaceview;


import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;


public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(new MysurfaceView(this));

}


@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}


}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI