- import android.app.Activity;
- import android.os.Bundle;
- import android.view.Window;
-
- public class Run extends Activity{
-
- DrawView drawView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
-
- drawView = new DrawView(this);
- setContentView(drawView);
- }
-
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- drawView.resume();//因为可能出现 返回屏幕的情况,这这里调用 来控制 线程,当返回的时候,暂停线程,如果用到设备监听,这样可以节电
- }
-
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- drawView.pause();
- }
-
- }
首先这是这主程序,主要用来启动控制下面(1),(2),程序: (1)为调用的时候每次保留 canvas到bitmap中,每次刷新的时候继续使用之前的bitmap (2)为每次 刷新canvas,用白色做背景,在其上面重新画图
(1):
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.Random;
-
- import org.xml.sax.InputSource;
-
- import android.R.color;
- import android.content.Context;
- import android.content.res.AssetManager;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.Rect;
- import android.util.Log;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
-
-
- public class DrawView extends SurfaceView implements Runnable{//继承runnable实现线程
- //
- Thread gameloop= null;
- SurfaceHolder surface;
- volatile Boolean running= false;
- Bitmap bitmap;
- Canvas can;
- AssetManager assets = null;
- BitmapFactory.Options options = null;
-
- public DrawView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
-
-
- surface=getHolder();
- assets=context.getAssets();
- options=new BitmapFactory.Options();
- options.inPreferredConfig = Bitmap.Config.ARGB_8888;
-
-
-
- }
-
- public void pause(){
- running = false;
- while(true){
-
- try {
- gameloop.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
-
- public void resume(){
- running = true;
- gameloop = new Thread(this);
- gameloop.start();
- }
-
- Random r=new Random();
- boolean flag=true;
- Point p=new Point();
- @Override
- public void run() {
- // TODO Auto-generated method stub
-
- int i=0;
- while(running){
- if(!surface.getSurface().isValid())
- continue;
-
- Canvas canvas = surface.lockCanvas();
-
- if(flag==true){
- paint.setColor(Color.rgb(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
- flag=false;
- bitmap=Bitmap.createBitmap(
- canvas.getWidth(),canvas.getHeight(),Config.ARGB_8888);
-
- p.x=canvas.getWidth();
- p.y=canvas.getHeight();
- can=new Canvas();
- can.setBitmap(bitmap);
- can.drawColor(Color.WHITE);
- }
- getNext();
- canvas.drawBitmap(bitmap, 0,0, paint);
-
- surface.unlockCanvasAndPost(canvas);
-
-
- try {
-
- Thread.sleep(50);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- }
- }
- Paint paint=new Paint();
- private void getNext() {
- // TODO Auto-generated method stub
- paint.setColor(Color.rgb(r.nextInt(256),r.nextInt(256),r.nextInt(256)));
- can.drawLine(r.nextInt(p.x), r.nextInt(p.y), r.nextInt(p.x), r.nextInt(p.y), paint);
- }
- }
在(1)中,我尝试过直接使用 canvas所得到的画布,但是每次都不能得到所想要的前一个画布 测试得到的情况如下: null null null 一开始有三个为空的canvas,第一个设置的第一个会在 3的位置,但是获得的在第一个的位置,之后每次获得都是当前的三个之前,这样 就无法每次得到之前的canvas,然后在其中画图,我所做的处理是使用一个bitmap,每次将其保存在bitmap中,要画图的时候再从bitmap中提取出来
就相当于用一个bitmap来保存 状态,实质上还是每次 刷新canvas
(2)
- //
- public class CopyOfDrawView extends SurfaceView implements Runnable{
-
- Thread gameloop= null;
- SurfaceHolder surface;
- volatile Boolean running= false;
-
- AssetManager assets = null;
- BitmapFactory.Options options = null;
- Bitmap knight[];
- int frame=0;
-
- public CopyOfDrawView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
-
-
- surface=getHolder();
- assets=context.getAssets();
- options=new BitmapFactory.Options();
- options.inPreferredConfig = Bitmap.Config.ARGB_8888;
-
- knight= new Bitmap[8];
- try {
- for(int i =0;i<8;i++){
- String filename= "knight"+String.valueOf(i+1)+".png";//在 assets中放图片实现存8张bitmap在knight中,显示这8张位图就可以实现动态的动画
- InputStream input = assets.open(filename);
- knight[i]=BitmapFactory.decodeStream(input, null, options);
-
- input.close();
-
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
- }
-
- public void pause(){
- running = false;
- while(true){
-
- try {
- gameloop.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
-
- public void resume(){
- running = true;
- gameloop = new Thread(this);
- gameloop.start();
- }
-
- Random r=new Random();
- boolean flag=true;
- Point p=new Point();
- @Override
- public void run() {
- // TODO Auto-generated method stub
-
- int i=0;
- while(running){
- if(!surface.getSurface().isValid())
- continue;
-
- Canvas canvas = surface.lockCanvas();
-
- canvas.drawColor(Color.WHITE);
-
- canvas.drawBitmap(knight[frame], 0, 0,null);
-
- Rect dest = new Rect(100,0,300,200); //使用 dest得到一个矩形,这个矩形作为bitmap的框,这样就可以实现不同大小的bitmap
- canvas.drawBitmap(knight[frame],null, dest, null);
-
- dest = new Rect(200,0,800,600);
- canvas.drawBitmap(knight[frame],null, dest, null);//画位图
-
-
-
-
- surface.unlockCanvasAndPost(canvas);
-
-
- try {
-
- Thread.sleep(50);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
-
-
- }
- }
-
- }
这个是每次都在canvas中重画
|