1

我正在用 android 开发,我必须Paint为 android 做一个。

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

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

这是代码:

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; 
   }
  }
}
4

2 回答 2

1

似乎有 2 个要绘制的表面,当您绘制一个时,另一个消失了。

这正是SurfaceView它的工作原理——它是双缓冲的。每次都需要重绘整个帧。

于 2012-12-15T20:47:45.037 回答
0

来自 Android 的文档:SurfaceHolder

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

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

于 2012-12-15T20:50:55.443 回答