2013-10-31 81 views
0

我有一個位圖設置爲圖像視圖。當我觸摸位圖的一個點並拖動到另一個點時,我想要將該拖動區域提取到另一個位圖中。在Android中裁剪拖動的區域

一些谷歌搜索後,我發現Canvas必須在這種情況下使用。我怎樣才能做到這一點?我可以使用下面的代碼:

@Override 
public boolean onTouchEvent (MotionEvent event) { 

    if (event.getAction() == MotionEvent.ACTION_DOWN) { 

    start_x = event.getX(); 
    start_y = event.getY();  

    } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 

    //create the line from start_x and start_y to the current location 
    //don't forget to invalidate the View otherwise your line won't get redrawn 

    } else if (event.getAction() == MotionEvent.ACTION_UP) { 

    //might not need anything here 

    } 
    return true; 
} 

回答

0

我的回答提出了不使用畫布,使用onTouch()的圖像視圖:

public class Test extends Activity { 

    ArrayList<Integer> array_x = new ArrayList<Integer>(); 
    ArrayList<Integer> array_y = new ArrayList<Integer>(); 
    Button btnArray; 
    Bitmap charscropped; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test); 

     ImageView img = (ImageView) findViewById(R.id.imageView1); 
     final ImageView img2 = (ImageView) findViewById(R.id.imageCroppd); 
     img.setImageBitmap(CaptureActivity.staticBitmap); 

     btnArray = (Button) findViewById(R.id.btn_for_array); 
     btnArray.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       for (int i = 0; i < array_x.size(); i++) { 
        Log.e("Array Index-" + i, " - " + array_x.get(i)); 
       } 

       int xBegin = array_x.get(0); 
       int xEnd = array_x.get(array_x.size()-1); 
       int yBegin = array_y.get(0); 
       int yEnd = array_y.get(array_y.size()-1); 
       CaptureActivity.staticBitmap.getWidth(); 
       CaptureActivity.staticBitmap.getHeight(); 

       //charscropped = Bitmap.createBitmap(CaptureActivity.staticBitmap, xBegin, yBegin, img2.getWidth(), img2.getHeight()); 
       charscropped = Bitmap.createBitmap(CaptureActivity.staticBitmap, xBegin, yBegin-20, 200, 40); 
       img2.setImageBitmap(charscropped); 
      } 

     }); 

     img.setOnTouchListener(new OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       int x = (int) event.getX(); 
       int y = (int) event.getY(); 
       array_x.add(x); 
       return array_y.add(y); 
      } 
     }); 

    } 

}