Android 开发 SurfaceView 总结
Android中一种常见的自定义画UI接口类:SurfaceView.可以在异步线程中,完成相关数据更新。
首先介绍几个基本的定义,在其他知识中也会设计如下名词:
1.Paint
画笔,所有的图像、图形都是由画笔完成的。
定义非常简单:
Paint paint = new Paint();
paint.setColor(Color.Black);//设置画笔的颜色
2.Canvas
画布,图像和图形都是通过Paint画在画布上面。画布对象通常是通过SurfaceHolder.lockCanvax()获得。
Canvas canvas = holder.lockCanvas();
canvas.drawRect(0,0,widht,height,paint);//画矩形
3.Surface
在应用的时候,没有使用到具体的实现类和抽象类。
API里面的描述是:Surface是原始图像缓存区的一个句柄(原始图像缓存区是有屏幕图像合成器管理的)。因此,可以知道Canvas所需要展示的数据,会被加载到Surface中,然后显示在屏幕上的。
4.SurfaceView
一个屏幕显示的View,控制Surface的显示大小,位置等。说白了就类似TextView一样。
需要子类实现具体的制订化功能,继承SurfaceView的同时,还需要实现SurfaceHolder.Callback接口(后面有讲解)。
5.SurfaceHolder
怎么能说清楚呢,SurfaceHolder是Surface的控制器,提供几个方法:
addCallback(SurfaceHolder.Callback callback); //添加SurfaceHolder.Callback的监听实现
lockCanvas();
lockCanvas(Rect dirty);
unlockCanvas(Canvas canvas);
6.SurfaceHolder.Callback
是一个接口,实现类可以监听SurfaceView的状态变化。接口方法如下:
surfaceCreated();
surfaceChanged();
surfaceDestory();
具体使用Demo,完成的功能就是将一张图片,上下不停的滑动:
1、RBSurfaceView 是Surface的实现类。
public class RBSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private Paint paint;
private WindowManager windowManager;
private Context context;
private float screenWidth,screenHeight;
private float bitmapX,bitmapY;
private int alphaNum;
private boolean alphaFlag ;
private OnRunThread onRunThread;
private OnDrawThread onDrawThread;
@SuppressWarnings("deprecation")
public RBSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
getHolder().addCallback(this);
windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
screenWidth = (float) windowManager.getDefaultDisplay().getWidth();
screenHeight =(float) windowManager.getDefaultDisplay().getHeight();
paint = new Paint();
onRunThread = new OnRunThread(this);
onDrawThread = new OnDrawThread(this);
onRunThread.start();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, screenWidth, screenHeight, paint);
Bitmap bitmapA = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bitmapA, bitmapX, bitmapY, paint);
if (alphaFlag) {
Bitmap bitmapB = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_gcoding);
paint.setAlpha(alphaNum);
canvas.drawBitmap(bitmapB, 0, 0, paint);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
onDrawThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public float getBitmapX() {
return bitmapX;
}
public void setBitmapX(float bitmapX) {
this.bitmapX = bitmapX;
}
public float getBitmapY() {
return bitmapY;
}
public void setBitmapY(float bitmapY) {
this.bitmapY = bitmapY;
}
public int getAlphaNum() {
return alphaNum;
}
public void setAlphaNum(int alphaNum) {
this.alphaNum = alphaNum;
}
public float getScreenWidth() {
return screenWidth;
}
public void setScreenWidth(float screenWidth) {
this.screenWidth = screenWidth;
}
public float getScreenHeight() {
return screenHeight;
}
public void setScreenHeight(float screenHeight) {
this.screenHeight = screenHeight;
}
public boolean isAlphaFlag() {
return alphaFlag;
}
public void setAlphaFlag(boolean alphaFlag) {
this.alphaFlag = alphaFlag;
}
}
2、OnDrawThread 是不停的让RBSurfaceView的onDraw方法画图片
public class OnDrawThread extends Thread{
private RBSurfaceView surfaceView;
private SurfaceHolder holder;
public OnDrawThread(RBSurfaceView surfaceView){
super();
this.surfaceView = surfaceView;
holder = surfaceView.getHolder();
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Canvas canvas = null;
while (true) {
canvas = holder.lockCanvas(null);
synchronized (surfaceView) {
if (canvas != null) {
surfaceView.onDraw(canvas);
}
}
holder.unlockCanvasAndPost(canvas);
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3、OnRunThread 主要负责不停的变化图片的坐标,然后设置给RBSurfaceView。
public class OnRunThread extends Thread{
private RBSurfaceView surfaceView;
private SurfaceHolder holder;
public OnRunThread(RBSurfaceView surfaceView){
super();
this.surfaceView = surfaceView;
holder = surfaceView.getHolder();
bitmapX = 0;
bitmapY = 0;
}
boolean runFlag;
int alphaNum;
float bitmapX;
float bitmapY;
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (true) {
while (bitmapX < surfaceView.getScreenWidth()) {
surfaceView.setBitmapX(bitmapX);
surfaceView.setBitmapY(bitmapY);
if (runFlag) {
bitmapY = bitmapY - 20;
}else {
bitmapY = bitmapY + 20;
}
if (bitmapY <=0) {
runFlag = false;
}else if (bitmapY >= surfaceView.getScreenHeight()+60) {
runFlag = true;
}
try {
Thread.sleep(30);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
surfaceView.setAlphaFlag(true);
for (alphaNum = 0; alphaNum <= 255; alphaNum++) {
if (alphaNum == 255) {
surfaceView.setAlphaFlag(false);
bitmapX = 0;
bitmapY = surfaceView.getScreenHeight() +60;
}
surfaceView.setAlphaNum(alphaNum);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}