2017-06-08 16 views
0

我是新來的android編程。每當用戶觸摸屏幕時,我都會嘗試繪製新的位圖圖像。使用當前的代碼,它所做的只是改變位圖所在的位置。它並沒有形成一個新的。我怎麼能做一個新的?下面是代碼:如何在每次按下屏幕時繪製一個新的位圖

public class MainDrawingView extends View { 

    public float eventX; 
    public float eventY; 




    public AlertDialog.Builder alertThing; 

    Context context = getContext(); 
    //Bitmaps 
    public Bitmap redSquare; 

    public String x; 
    public String y; 
    //Colors 
    public boolean red = false; 
    public boolean blue = false; 
    //Squares 
    public boolean topRight = false; 
    public boolean topLeft = false; 
    public boolean bottomLeft = false; 
    public boolean bottomRight = false; 

    public int width = context.getResources().getDisplayMetrics().widthPixels; 
    public int height = context.getResources().getDisplayMetrics().heightPixels; 
    public int center = height/2; 
    public int widthDiv = width/2; 


    private Paint paint = new Paint(); 
    private Point pointStart = new Point(); 
    private Point pointEnd = new Point(); 

    public MainDrawingView(Context context, AttributeSet attrs){ 
     super(context, attrs); 
     paint.setAntiAlias(true); 
     paint.setStrokeWidth(5f); 
     paint.setColor(Color.BLACK); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setStrokeJoin(Paint.Join.ROUND); 

     alertThing = new AlertDialog.Builder(context); 


     //define bitmap 
     redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.red); 



    } 

    @Override 
    public void onDraw(Canvas canvas){ 
     //canvas.drawPath(path, paint); 
     canvas.drawLine(pointStart.x, pointStart.y, pointEnd.x, pointEnd.y,paint); 
     //Makes a straight line go through the center of the screen. 
     canvas.drawLine(0,height/2.5f, width,height/2.5f,paint); 
     //Makes a straight line go up and down. 
     canvas.drawLine(width/2.3f,0, width/2.3f, height,paint); 




     //Chnages the square colors. 

     // top left combo 
     if(topLeft){ 
     if(red){ 
      Toast.makeText(context,"jgjgjgjgjgjgjgjgjjg", Toast.LENGTH_SHORT).show(); 

      new Drawings(redSquare, canvas, eventX, eventY); 

     } 
     } 



    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
     //Gets the coords of the tap 

     eventX = event.getX(); 
     eventY = event.getY(); 

     int pointX = Math.round(eventX); 
     int pointY = Math.round(eventY); 


     x = String.valueOf(eventX); 
     y = String.valueOf(eventY); 


     //This is the top left "square" 
     if(eventY < center && eventX < widthDiv){ 
      alertThing.setTitle("edit square"); 
      alertThing.setMessage("please choose an option"); 
      alertThing.setPositiveButton("red", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        topLeft = true; 
        red = true; 




        invalidate(); 

       } 
      }); 
      alertThing.setNeutralButton("Chnange color to blue", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 

       } 
      }); 

      alertThing.create(); 
      alertThing.show(); 
     } 







     Toast.makeText(context, "center is :" +x, Toast.LENGTH_SHORT).show(); 



     switch(event.getAction()){ 

      case MotionEvent.ACTION_DOWN: 
       //Sets a new starting point 
      pointStart.set(pointX,pointY); 



       return true; 

      case MotionEvent.ACTION_UP: 
       //Contects the points 
       pointEnd.set(pointX,pointY); 
       break; 




      default: 
       return false; 
     } 
     invalidate(); 
     return true; 
    } 





} 

這裏是繪圖類繪製位圖:

public class Drawings { 






    public Drawings(Bitmap bitmap, Canvas canvas,float x, float y){ 




     canvas.drawBitmap(bitmap, x,y,null); 


    } 
} 

回答

1

你真的應該看什麼canvas.drawBitmap does.當然它改變了位置,由於觸摸位置也發生了變化。

您可以像定義位圖時那樣創建一個新的位圖。

但是在onDraw中創建新對象將會非常緩慢。

相關問題