油漆帆布机器人 [英] paint canvas android

查看:160
本文介绍了油漆帆布机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在android系统正在开发,而我要做一个油漆为Android。

我使用的是code以下,当我执行code,平局的作品,但是,似乎有2面作画,当你在一个,另外一个平局消失了。

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

下面是code:

 导入了java.util.Random;进口android.annotation.Sup pressLint;
进口android.annotation.TargetApi;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Canvas;
进口android.graphics.Paint;
进口android.graphics.Path;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.SurfaceHolder;
进口android.view.SurfaceView;
进口android.view.View;
 进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.RelativeLayout;公共类MainActivity延伸活动{
  MySurfaceView mySurfaceView;
  按钮夸德拉多;
  按钮Circulo;
  按钮颜色;
  按钮凌特;
  私人布尔Bcuadrado,Bcirculo,Bcolor = FALSE;
  私人布尔Blinea = TRUE;
    帆布帆布=新的Canvas();  @TargetApi(11)
@燮pressLint(NewApi)
@覆盖
  公共无效的onCreate(捆绑savedInstanceState){
   super.onCreate(savedInstanceState);
       的setContentView(R.layout.activity_main);
       RelativeLayout的mainLayout =(RelativeLayout的)findViewById(R.id.main_layout_id);
       查看查看= getLayoutInflater()膨胀(R.layout.itemlayout,mainLayout,假的)。
       mainLayout.addView(视图);
       mySurfaceView =新MySurfaceView(本);
       夸德拉多=(按钮)findViewById(R.id.button1);
       Circulo =(按钮)findViewById(R.id.button2);
       颜色=(按钮)findViewById(R.id.button3);
       凌特=(按钮)findViewById(R.id.button4);
       INT W = view.getWidth();
       INT H = view.getHeight();
       浮X = view.getX();
       浮Y = view.getY();
       mySurfaceView.setY(100);
       mainLayout.addView(mySurfaceView);
       Cuadrado.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            如果(Bcuadrado == FALSE){
                Bcuadrado = TRUE;
                Bcirculo = FALSE;
                Bcolor = FALSE;
                Blinea = FALSE;
            }
        }
       });       Circulo.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            如果(!Bcirculo){
            Bcuadrado = FALSE;
            Bcirculo = TRUE;
            Bcolor = FALSE;
            Blinea = FALSE;
        }
    }
   });       Color.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
        如果(!Bcolor){
            Bcuadrado = FALSE;
            Bcirculo = FALSE;
            Bcolor = TRUE;
            Blinea = FALSE;
        }
    }
   });       Linea.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
        如果(!Blinea){
            Bcuadrado = FALSE;
            Bcirculo = FALSE;
            Bcolor = FALSE;
            Blinea = TRUE;
        }
    }
   });
}  类MySurfaceView扩展了SurfaceView {
   路径路径;
   SurfaceHolder surfaceHolder;
   挥发性布尔运行= FALSE;
   民营涂料粉刷=新的油漆();
   浮X0 = 0;
    浮X1 = 0;
    浮Y0 = 0;
    浮Y1 = 0;
    随机随机=新的随机();   公共MySurfaceView(上下文的背景下){
    超级(上下文);
    surfaceHolder = getHolder();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    paint.setColor(android.graphics.Color.WHITE);   }   @覆盖
   公共布尔onTouchEvent(MotionEvent事件){
       如果(Blinea){
            如果(event.getAction()== MotionEvent.ACTION_DOWN){
             路径=新路径();
             path.moveTo(event.getX(),event.getY());
            }否则如果(event.getAction()== MotionEvent.ACTION_MOVE){
             path.lineTo(event.getX(),event.getY());
            }否则如果(event.getAction()== MotionEvent.ACTION_UP){
             path.lineTo(event.getX(),event.getY());
            }
            如果(路径!= NULL){
                 画布= surfaceHolder.lockCanvas();
                 canvas.drawPath(路径,油漆);
                 surfaceHolder.unlockCanvasAndPost(画布);
                }       }否则如果(Bcuadrado){
           如果(event.getAction()== MotionEvent.ACTION_DOWN){
               X0 = event.getX();
               Y0 = event.getY();
           }
           否则如果(event.getAction()== MotionEvent.ACTION_UP){
               X1 = event.getX();
               Y1 = event.getY();
               画布= surfaceHolder.lockCanvas();
               canvas.drawRect(X0,Y0,X1,Y1,油漆);
               surfaceHolder.unlockCanvasAndPost(画布);
           }       }否则如果(Bcirculo){
           如果(event.getAction()== MotionEvent.ACTION_DOWN){
               X0 = event.getX();
               Y0 = event.getY();
           }
           否则如果(event.getAction()== MotionEvent.ACTION_UP){
               X1 = event.getX();
               画布= surfaceHolder.lockCanvas();
               canvas.drawCircle(X0,Y0,(X1-X0),漆);
               surfaceHolder.unlockCanvasAndPost(画布);
           }
       }否则如果(Bcolor){
             INT R = random.nextInt(255);
             INT G = random.nextInt(255);
             INT B = random.nextInt(255);
             画布= surfaceHolder.lockCanvas();
             paint.setColor(0xff000000 +(R<< 16)+(G<< 8)+ B);
             surfaceHolder.unlockCanvasAndPost(画布);
       }
    返回true;
   }
  }
}


解决方案

  

似乎有2面作画,当你在一个平局,另外一个就会消失。


这是究竟是如何 SurfaceView 工作 - 这是双缓冲。你需要每次重绘整个画面。

I'm developing in android, and I have to do a Paint for android.

I'm using the code below and, when I execute the code, the draw works, but, it seems that there are 2 surfaces to paint, and when you draw in one, the other one disappears.

I was looking for the exact error, but cannot find it.

Here is the code :

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

解决方案

it seems that there are 2 surfaces to paint, and when you draw in one, the other one disappears.

That is exactly how SurfaceView works - it's double buffered. You need to redraw whole frame each time.

这篇关于油漆帆布机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆