2012-05-05 61 views
4

我試圖找到一種方法來實現拖放功能和旋轉在我的Android應用程序中的位圖下降/旋轉位圖。我希望能夠在畫布上拖動圖像並將其旋轉。下面是我如何將位圖我的畫布:Android的阻力,在畫布

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.dog); 
mCanvas.drawBitmap(bm,x-100,y-100 , mPaint); 

其中x & y是等於event.getX();/event.getY();

任何想法我怎麼能做到這一點?

在此先感謝! :)

回答

0

這是我的解決方案,也許它不是很酷,所以如果有人有建議與我分享。

public class BitmapDragAndDrop { 

BitmapDragAndDropListener listener; 

public BitmapDragAndDrop(BitmapDragAndDropListener listener) { 
    this.listener = listener; 
} 

public boolean onTouch(View view, MotionEvent motionEvent) { 
    switch (motionEvent.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      listener.onDragStart(motionEvent.getX(), motionEvent.getY()); 
      view.invalidate(); 
      return true; 
     case MotionEvent.ACTION_MOVE: 
      listener.onDragging(motionEvent.getX(), motionEvent.getY()); 
      view.invalidate(); 
      return true; 
     case MotionEvent.ACTION_UP: 
      listener.onDragEnd(listener.onDrop(motionEvent.getX(), motionEvent.getY())); 
      view.invalidate(); 
      return true; 
    } 
    return false; 
} 

public interface BitmapDragAndDropListener { 

    void onDragStart(float x, float y); 

    void onDragging(float x, float y); 

    boolean onDrop(float x, float y); 

    void onDragEnd(boolean isEntered); 
} 

} 


public class YourDrawer implements BitmapDragAndDrop.BitmapDragAndDropListener { 

Bitmap originalImage; //for restore initial state 
Bitmap drawingImage; //bitmap for drawing states. 
Canvas imageCanvas; 
Bitmap objectBitmap; 

public YourDrawer(Bitmap imageBmp, Bitmap objectBmp) { 
    this.originalImage = imageBmp.copy(Bitmap.Config.RGB_565, true); 
    this.drawingImage = imageBmp.copy(Bitmap.Config.ARGB_8888, true); 
    this.imageCanvas = new Canvas(drawingImage); 
    this.objectBitmap = objectBmp; 

    //Draw your object at standard place where you need 
    drawObject(); 
} 

private void restoreImageToOriginal() { 
    imageCanvas.drawBitmap(originalImage, 0, 0, null); 
} 

@Override 
public void onDragStart(float x, float y) { 
    //do whatever you want 
} 

@Override 
public void onDragging(float x, float y) { 
     restoreImageToOriginal(); 
     //Draw bitmap object at new coordinates 
     drawMyObject(x, y); 
    } 
} 

@Override 
public boolean onDrop(float x, float y) { 
    if (isRightPlace(x, y)) { 
     return true; 
    } else { 
     return false; 
    } 
} 

@Override 
public void onDragEnd(boolean isEntered) { 
    restoreImageToOriginal(); 
    if (isEntered) { 
     drawMyObjectAtLastCoordinates(); 
    } 
} 

public Bitmap getDrawingBitmap() { 
    return drawingImage; 
} 

} 

Ofc它不是複製和粘貼代碼。你需要一些你的實現添加到它。

在主類(片段或活動),您可以初始化代碼是這樣的:

YourDrawer yourDrawer = 
      new YourDrawer(originalImage, objectBitmap); 
    onTouchListener 
      = new BitmapDragAndDrop(yourDrawer); 
    imageView.setImageBitmap(yourDrawer.getDrawingBitmap())); 

    imageView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      if (onTouchListener != null) 
       onTouchListener.onTouch(view, motionEvent); 
      return false; 
     } 
    }); 

它只能解決拖放問題,而不是旋轉。但主要的想法是,即使工作了轉動。