画布画android

时间:2012-12-15 19:55:07

标签: android canvas paint

我正在开发android,我必须为android做Paint

我正在使用下面的代码,当我执行代码时,绘图工作,但是,似乎有两个绘制的表面,当你绘制一个,另一个消失。

我一直在寻找确切的错误,但找不到它。

以下是代码:

import java.util.Random; 

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
 import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
  MySurfaceView mySurfaceView;
  Button Cuadrado;
  Button Circulo;
  Button Color;
  Button Linea;
  private boolean Bcuadrado,Bcirculo,Bcolor=false;
  private boolean Blinea=true;
    Canvas canvas = new Canvas();

  @TargetApi(11)
@SuppressLint("NewApi")
@Override
  public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       RelativeLayout mainLayout =(RelativeLayout)findViewById(R.id.main_layout_id );
       View view =getLayoutInflater().inflate(R.layout.itemlayout, mainLayout,false);
       mainLayout.addView(view);
       mySurfaceView = new MySurfaceView(this);
       Cuadrado=(Button)findViewById(R.id.button1);
       Circulo=(Button)findViewById(R.id.button2);
       Color=(Button)findViewById(R.id.button3 );
       Linea=(Button)findViewById(R.id.button4 );
       int w= view.getWidth();
       int h= view.getHeight();
       float x=view.getX();
       float y= view.getY();
       mySurfaceView.setY(100);
       mainLayout.addView(mySurfaceView);
       Cuadrado.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(Bcuadrado==false){
                Bcuadrado=true;
                Bcirculo=false;
                Bcolor=false;
                Blinea=false;
            }
        }
       });

       Circulo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if(!Bcirculo){
            Bcuadrado=false;
            Bcirculo=true;
            Bcolor=false;
            Blinea=false;
        }
    }
   });

       Color.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        if(!Bcolor){
            Bcuadrado=false;
            Bcirculo=false;
            Bcolor=true;
            Blinea=false;
        }
    }
   });

       Linea.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        if(!Blinea){
            Bcuadrado=false;
            Bcirculo=false;
            Bcolor=false;
            Blinea=true;
        }
    }
   });
}

  class MySurfaceView extends SurfaceView{
   Path path;
   SurfaceHolder surfaceHolder;
   volatile boolean running = false;        
   private Paint paint = new Paint();
   float x0=0;
    float x1=0;
    float y0=0;
    float y1=0;
    Random random = new Random();

   public MySurfaceView(Context context) {
    super(context);
    surfaceHolder = getHolder();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    paint.setColor(android.graphics.Color.WHITE);

   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {


       if(Blinea){
            if(event.getAction() == MotionEvent.ACTION_DOWN){
             path = new Path();
             path.moveTo(event.getX(), event.getY());
            }else if(event.getAction() == MotionEvent.ACTION_MOVE){
             path.lineTo(event.getX(), event.getY());
            }else if(event.getAction() == MotionEvent.ACTION_UP){
             path.lineTo(event.getX(), event.getY());
            }
            if(path != null){
                 canvas = surfaceHolder.lockCanvas();
                 canvas.drawPath(path, paint);
                 surfaceHolder.unlockCanvasAndPost(canvas);
                }

       }else if(Bcuadrado){
           if(event.getAction()==MotionEvent.ACTION_DOWN){
               x0=event.getX();
               y0=event.getY();
           }
           else if(event.getAction()==MotionEvent.ACTION_UP){
               x1=event.getX();
               y1=event.getY();
               canvas = surfaceHolder.lockCanvas();
               canvas.drawRect(x0, y0, x1, y1, paint);
               surfaceHolder.unlockCanvasAndPost(canvas);
           }

       }else if(Bcirculo){
           if(event.getAction()==MotionEvent.ACTION_DOWN){
               x0=event.getX();
               y0=event.getY();
           }
           else if(event.getAction()==MotionEvent.ACTION_UP){
               x1=event.getX();
               canvas=surfaceHolder.lockCanvas();
               canvas.drawCircle(x0, y0,(x1-x0), paint);
               surfaceHolder.unlockCanvasAndPost(canvas);
           }
       }else if(Bcolor){
             int r = random.nextInt(255);
             int g = random.nextInt(255);
             int b = random.nextInt(255);
             canvas=surfaceHolder.lockCanvas();
             paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
             surfaceHolder.unlockCanvasAndPost(canvas);
       }


    return true; 
   }
  }
}

2 个答案:

答案 0 :(得分:1)

  

似乎有2个表面要绘画,当你画一个,另一个消失。

这正是SurfaceView的工作方式 - 它是双缓冲的。您需要每次重绘整个帧。

答案 1 :(得分:0)

来自Android的文档:SurfaceHolder

  

在unlockCanvas()和lockCanvas()之间永远不会保留Surface的内容,因此,必须写入Surface区域中的每个像素。此规则的唯一例外是当指定脏矩形时,在这种情况下,将保留非脏像素。

画布不保存您之前写入的内容。每次调用unlock()时,都必须重新绘制所有内容。