Android中Su易做图ceView学习
Su易做图ceView和View的明显不同在于Su易做图ce不需要通过线程来更新视图,但在绘制之前必须使用lockCanvas方法锁定画布,并得 到画布,然后绘制,完成后用unlockCanvasAndPost方法解锁画布。Su易做图ceView类的事件处理和View一样。
首先来看一个简单的框架。
绘制界面类:
[java]
package com.example.bonusball;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
public class CanvasView extends Su易做图ceView implements Su易做图ceHolder.Callback
{
private Su易做图ceHolder myHolder;
private Paint ballPaint; // Paint used to draw the cannonball
private int screenWidth; // width of the screen
private int screenHeight; // height of the screen
private int ballRadius;
private CanvasThread myThread;
//控制循环
private boolean isLoop;
public CanvasView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myHolder=this.getHolder();
myHolder.addCallback(this);
ballPaint=new Paint();
ballPaint.setColor(Color.BLUE);
isLoop = true;
}
public void fireBall(float startX,float startY)
{
System.out.println("Fire");
}
@Override
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
screenWidth = w; // store the width
screenHeight = h; // store the height
ballRadius=w/10;
}
@Override
public void su易做图ceCreated(Su易做图ceHolder holder) {
// TODO Auto-generated method stub
myThread = new CanvasThread();
System.out.println("Su易做图ceCreated!");
myThread.start();
}
@Override
public void su易做图ceDestroyed(Su易做图ceHolder holder) {
// TODO Auto-generated method stub
// 停止循环
isLoop = false;
}
public void drawGameElements(Canvas canvas)
{
canvas.drawCircle(100, 100,ballRadius,ballPaint);
}
private class CanvasThread extends Thread
{
@Override
public void run()
{
while(true)
{
synchronized( myHolder )
{
Canvas canvas = myHolder.lockCanvas(null);//获取画布
drawGameElements(canvas);
myHolder.unlockCanvasAndPost(canvas);//解锁画布,提交画好的图像
//System.out.println("run");
}
}
}
}
}
package com.example.bonusball;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.Su易做图ceHolder;
import android.view.Su易做图ceView;
public class CanvasView extends Su易做图ceView implements Su易做图ceHolder.Callback
{
private Su易做图ceHolder myHolder;
private Paint ballPaint; // Paint used to draw the cannonball
private int screenWidth; // width of the screen
private int screenHeight; // height of the screen
private int ballRadius;
private CanvasThread myThread;
//控制循环
private boolean isLoop;
public CanvasView(Context context) {
super(context);
// TODO Auto-generated constructor stub
myHolder=this.getHolder();
myHolder.addCallback(this);
ballPaint=new Paint();
ballPaint.setColor(Color.BLUE);
isLoop = true;
}
public void fireBall(float startX,float startY)
{
System.out.println("Fire");
}
@Override
public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
补充:移动开发 , Android ,