2014-04-02 40 views
0

我想在我的應用程序的同一視圖中同時使用雙指縮放和拖放n拖放。使用雙指縮放,在同一視圖上拖放

我能夠單獨實施它們,但是當我嘗試使用它們都不起作用。

這裏是拖動

class MyClickListener implements OnLongClickListener { 

    @Override 
    public boolean onLongClick(View view) { 

     ClipData.Item item = new ClipData.Item((CharSequence) view.getTag()); 

     String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
     ClipData data = new ClipData(view.getTag().toString(), mimeTypes, 
       item); 
     DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 

     view.startDrag(data, // data to be dragged 
       shadowBuilder, // drag shadow 
       view, // local data about the drag and drop operation 
       0 // no needed flags 
     ); 
     return true; 
    } 
} 

的代碼時,我在非定製的視圖中使用,而不指縮放這工作得很好。

當我使用具有scalegesture偵聽器的自定義視圖,然後將MyClickListener設置爲其LongClickListener時,我只獲取縮放功能,並且沒有開始拖動。

我試圖直接在視圖

public class TouchImageView extends View { 
long time = 0; 
private Drawable image; 
private float scaleFactor = 1.0f; 
private ScaleGestureDetector scaleGestureDetector; 

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

public TouchImageView(Context context, Drawable mImage) { 
    super(context); 

    image = mImage; 
    setFocusable(true); 
    image.setBounds(0, 0, image.getIntrinsicWidth(), 
      image.getIntrinsicHeight()); 
    scaleGestureDetector = new ScaleGestureDetector(context, 
      new ScaleListener()); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    // Set the image bounderies 
    canvas.save(); 
    canvas.scale(scaleFactor, scaleFactor); 
    image.draw(canvas); 
    canvas.restore(); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 

    if (event.getPointerCount() > 1) { 
     scaleGestureDetector.onTouchEvent(event); 
     invalidate(); 
    } else { 
     int action = MotionEventCompat.getActionMasked(event); 

     switch(action){ 
     case MotionEvent.ACTION_DOWN: 
      time = SystemClock.elapsedRealtime(); 
      Log.i(EJ.TAG, time+""); 
      break; 

     case MotionEvent.ACTION_UP: 
      long time1 = SystemClock.elapsedRealtime() - time; 

      if(time1 >= 1000){ 

       ClipData.Item item = new ClipData.Item((CharSequence) this.getTag()); 

       String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
       ClipData data = new ClipData(this.getTag().toString(), mimeTypes, 
         item); 
       DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(this); 

       this.startDrag(data, // data to be dragged 
         shadowBuilder, // drag shadow 
         this, // local data about the drag and drop operation 
         0 // no needed flags 
       ); 

       //Toast.makeText(context, "Long press", Toast.LENGTH_SHORT).show(); 

       return true; 
      } 

      break;    
     } 

    } 

    return true; 
} 

private class ScaleListener extends 
     ScaleGestureDetector.SimpleOnScaleGestureListener { 
    @Override 
    public boolean onScale(ScaleGestureDetector detector) { 
     scaleFactor *= detector.getScaleFactor(); 

     // don't let the object get too small or too large. 
     scaleFactor = Math.max(0.1f, Math.min(scaleFactor, 5.0f)); 

     invalidate(); 
     return true; 
    } 
} 

} 

我不知道這是否是正確的方法來檢測長按之後添加拖動代碼到自定義視圖。對此的任何幫助表示讚賞

回答

0

查看下面給出的演示捏縮放,翻譯和縮放功能。希望它有幫助。

Example 1

Example 2

你可以問,如果你有任何疑問:)。

快樂編碼!

+0

例1無法解決我的問題,它需要圖像填充父母的高度和寬度。我會有幾張圖片,我需要縮放和移動每張圖片。 – Amanni

+0

在示例2中,我想給出修正大小,我的意思是它不能放大,從給定的大小。那麼,我該如何執行它,請告訴我 –