2014-09-25 114 views
0

我正在製作一個可以裁剪人物圖像的應用程序。我的應用程序的佈局將給出更好的描述動態更改矩形大小android

enter image description here

在這裏,矩形將用於捕獲由其長度和寬度限定的圖像。矩形是可移動的。我將如何去重新調整矩形的大小。例如。在WhatsApp中,當您觸摸矩形內的區域時,矩形會移動。如果您觸摸矩形的邊緣,它可以重新調整適合裁剪的圖像大小。所以,我有兩個問題。 1)如何接收矩形邊緣上的觸摸事件和2)如何重新設置矩形的大小。我正在使用畫布繪製我的矩形。造成這種情況的代碼如下:

public class CustomView extends Views 
{ 


public CustomView(Context context) 
{ 
     super(context); 
} 

@Override 
protected void onDraw(Canvas canvas) 
{ 
     super.onDraw(canvas); 
     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);; 
     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(3); 
     paint.setStyle(Paint.Style.STROKE); 
     canvas.drawRect(0, 0, 300, 300, paint); 
} 
} 

回答

1

您應該實現OnTouchListener併爲您在MotionEvent.ACTION_DOWN如果觸摸矩形和修改左,右,底部和矩形的頂部MotionEvent.ACTION_MOVE

我提供了一個不經測試的例子:

public class CustomView extends View { 

    private float left=0,right = 300, top = 0, bottom = 300; 

    public CustomView(Context context) 
    { 
     super(context); 
     setOnTouchListener(new OnTouchListener() { 
      float oldX, oldY; 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       boolean touch = false; 
       switch (event.getAction()){ 
        case MotionEvent.ACTION_DOWN: 
         touch = isTouchBorderRect(); 
         break; 
        case MotionEvent.ACTION_UP: 
         touch = false; 
        case MotionEvent.ACTION_MOVE: 
         if (touch){ 
          float newX = event.getRawX(); 
          float newY = event.getRawY(); 
          float deltaX = newX-oldX; 
          float deltaY = newY-oldY; 

          left-=deltaX; 
          right +=deltaX; 
          bottom += deltaY; 
          top -= deltaY; 

         } 
         break; 

       } 
       return false; 
      } 
     }); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     super.onDraw(canvas); 
     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);; 
     paint.setColor(Color.BLUE); 
     paint.setStrokeWidth(3); 
     paint.setStyle(Paint.Style.STROKE); 
     canvas.drawRect(left, top, right, bottom, paint); 
    } 

} 

isTouchBorderRect()方法,你應該檢查你是否觸摸矩形。

此代碼未經測試,但顯示您想要開發的想法。