1、View的刷屏方式
View主要是调用onDraw(Canvas canvas)方法,利用Canvas(画布)来绘制图形,再利用invalidate()/postInvalidate()方法来进行重绘,达到类刷屏的效果。
下面通过“随手指移动的小球”为例子,展示View的刷屏方式,代码如下:
继承View的MyView类:
package com.example.huawei;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View{
private int textX = 100, textY = 100;
public MyView(Context context){
super(context);
//setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawCircle(textX, textY, 20, paint);
super.onDraw(canvas);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int x = (int)event.getX();
int y = (int)event.getY();
if(event.getAction() == MotionEvent.ACTION_DOWN){
textX = x;
textY = y;
}else if(event.getAction() == MotionEvent.ACTION_UP){
textX = x;
textY = y;
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
textX = x;
textY = y;
}
invalidate();
//return super.onTouchEvent(event);
return true;
}
}
使用MyView类的MainActivity类:
package com.example.huawei;
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 MyView(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;
}
}
继承View类的MyView类,刷屏主要是通过重写onDraw(Canvas canvas)方法来实现的。View的画图方式,主要是在onDraw()方法内,利用画布canvas进行绘制。
但是,onDraw()方法只会在View视图开始创建的时候,执行一遍而已。要想在新的画布上,进行图形绘制,需要调用View内的两个方法:
invalidate()
postInvalidate()
上面两个调用onDraw()进行重新绘制画布的方法,其主要区别:
invalidate()方法不能在当前自己创建的子线程中循环调用执行(可以在系统的UI线程中执行)。
postInvalidate()方法可以在当前创建的子线程中循环调用。
如果不在当前View创建线程循环重绘画布的话,这两种重绘画布的函数,并没有什么区别,都可以使用。
2、SurfaceView的刷屏方式
继承SurfaceView类的执行类,使用SurfaceHolder类的对象的lockCanvas()方法,获得画布canvas,在上面进行图形绘制。
下面的例子,是利用SurfaceView类,来实现“随手指移动的小球”,代码如下:
package com.example.huawei;
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.SurfaceView;
import android.view.SurfaceHolder.Callback;
public class MySurfaceView extends SurfaceView implements Callback{
private SurfaceHolder surfaceHolder;
private Paint paint;
private int textX = 100, textY = 100;
public MySurfaceView(Context context){
super(context);
//实例化SurfaceHolder
surfaceHolder = this.getHolder();
//为surfaceHolder添加状态监听
surfaceHolder.addCallback(this);
//实例化一个画笔
paint = new Paint();
//设置画笔颜色为白色
paint.setColor(Color.WHITE);
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
public void myDraw(){
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
textX = (int)event.getX();
textY = (int)event.getY();
myDraw();
return true;
}
}
使用MySurfaceView类的MainActivity类:
package com.example.huawei;
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;
}
}
从上面的效果图可以看到,MySurfaceView类不断调用myDraw(0方法进行图形绘制,但是,canvas并没有换,还是原来的画布。因此,在上面绘制的小球越来越多。这是因为MySurfaceView类没有更换canvas,没有进行刷屏。
对于SurfaceView类的刷屏方式,主要有四种:
2.1、每次绘画之前,在画布上,填充一种颜色,将先前绘制的图形覆盖掉:
public void myDrawColor() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
2.2、每次绘画之前,在画布上,指定RGB颜色来填充画布(类似于方法一),将先前绘制的图形覆盖掉:
public void myDrawRGB() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawRGB(0, 0, 0);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
2.3、每次绘画之前,绘制一个等同于屏幕大小的Rect图形,将先前绘制的图形覆盖掉:
public void myDrawRect() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawRect(0,0,this.getWidth(),this.getHeight(),paint);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
2.4、每次绘图之前,绘制一张等同于屏幕大小的图片,将先前绘制的图形覆盖掉:
public void myDrawBitmap() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
使用SurfaceView的刷图方式,实现“随手指移动的小球”,其主要的实现代码如下:
package com.example.huawei;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
public class MySurfaceView extends SurfaceView implements Callback{
private SurfaceHolder surfaceHolder;
private Paint paint;
private int textX = 100, textY = 100;
private Bitmap bitmap;
public MySurfaceView(Context context){
super(context);
//实例化SurfaceHolder
surfaceHolder = this.getHolder();
//为surfaceHolder添加状态监听
surfaceHolder.addCallback(this);
//实例化一个画笔
paint = new Paint();
//设置画笔颜色为白色
paint.setColor(Color.WHITE);
BitmapDrawable bitmapDrawable = (BitmapDrawable)(context.getResources().getDrawable(R.drawable.ic_launcherbit));
bitmap = bitmapDrawable.getBitmap();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
public void myDraw(){
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
public void myDrawColor() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(Color.BLACK);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
public void myDrawRGB() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawRGB(0, 0, 0);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
public void myDrawRect() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawRect(0,0,this.getWidth(),this.getHeight(),paint);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
public void myDrawBitmap() {
Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawBitmap(bitmap, 0, 0, paint);
canvas.drawCircle(textX, textY, 20, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
textX = (int)event.getX();
textY = (int)event.getY();
myDrawBitmap();
return true;
}
}
使用MySurfaceView类的MainActivity类:
package com.example.huawei;
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;
}
}