2014-09-23 193 views
2

我最近實施了在Android開發者網站上提供的拖動和縮放代碼移到FOUND HERE自定義視圖不會用手勢

我遇到的問題是關於移動視圖一旦有能力已添加到佈局。圖像可通過手勢進行縮放,但不可移動。

如果有幫助,該視圖將被添加到片段內的FrameLayout。

有沒有人遇到類似的問題在自定義視圖中實現此Android示例?或者有人能告訴我我錯過了什麼,我無法移動我添加的視圖。

public class CustomImageView extends View { 

private static final int INVALID_POINTER_ID = 0; 
Drawable _drawable; 
// private static readonly int InvalidPointerId = -1; 
// private int _activePointerId = InvalidPointerId; 

private float _posX; 
private float _posY; 
private float mScaleFactor = 1.0f; 
private int mActivePointerId = INVALID_POINTER_ID; 

// gesture listeners 
private ScaleGestureDetector mScaleDetector; 
private float mLastTouchX; 
private float mLastTouchY; 
private float mPosY; 
private float mPosX; 

public CustomImageView(Context context, int resourceId) { 
    super(context, null, 0); 

    _drawable = getResources().getDrawable(resourceId); 
    _drawable.setBounds(0, 0, 200, 200); 
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
    // TODO Auto-generated constructor stub 
} 

protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    canvas.save(); 
    canvas.translate(_posX, _posY); 
    canvas.scale(mScaleFactor, mScaleFactor); 
    _drawable.draw(canvas); 
    canvas.restore(); 
} 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    // Let the ScaleGestureDetector inspect all events. 
    mScaleDetector.onTouchEvent(ev); 

    final int action = MotionEventCompat.getActionMasked(ev); 

    switch (action) { 
    case MotionEvent.ACTION_DOWN: { 
     final int pointerIndex = MotionEventCompat.getActionIndex(ev); 
     final float x = MotionEventCompat.getX(ev, pointerIndex); 
     final float y = MotionEventCompat.getY(ev, pointerIndex); 

     // Remember where we started (for dragging) 
     mLastTouchX = x; 
     mLastTouchY = y; 
     // Save the ID of this pointer (for dragging) 
     mActivePointerId = MotionEventCompat.getPointerId(ev, 0); 
     break; 
    } 

    case MotionEvent.ACTION_MOVE: { 
     // Find the index of the active pointer and fetch its position 
     final int pointerIndex = MotionEventCompat.findPointerIndex(ev, 
       mActivePointerId); 

     final float x = MotionEventCompat.getX(ev, pointerIndex); 
     final float y = MotionEventCompat.getY(ev, pointerIndex); 

     // Calculate the distance moved 
     final float dx = x - mLastTouchX; 
     final float dy = y - mLastTouchY; 

     mPosX += dx; 
     mPosY += dy; 

     invalidate(); 

     // Remember this touch position for the next move event 
     mLastTouchX = x; 
     mLastTouchY = y; 

     break; 
    } 

    case MotionEvent.ACTION_UP: { 
     mActivePointerId = INVALID_POINTER_ID; 
     break; 
    } 

    case MotionEvent.ACTION_CANCEL: { 
     mActivePointerId = INVALID_POINTER_ID; 
     break; 
    } 

    case MotionEvent.ACTION_POINTER_UP: { 

     final int pointerIndex = MotionEventCompat.getActionIndex(ev); 
     final int pointerId = MotionEventCompat.getPointerId(ev, 
       pointerIndex); 

     if (pointerId == mActivePointerId) { 
      // This was our active pointer going up. Choose a new 
      // active pointer and adjust accordingly. 
      final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 
      mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex); 
      mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex); 
      mActivePointerId = MotionEventCompat.getPointerId(ev, 
        newPointerIndex); 
     } 
     break; 
    } 
    } 
    return true; 
} 

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

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

     invalidate(); 
     return true; 
    } 
} 

}

這裏是XML,那我加入欣賞到root_layout是FrameLayout裏

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:clickable="true" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="@dimen/ab_height" 
     android:background="@color/red" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" > 

      <ImageButton 
       android:id="@+id/btn_Back" 
       android:layout_width="@dimen/width" 
       android:layout_height="fill_parent" 
       android:background="@color/red" 
       android:contentDescription="@string/desc" 
       android:src="@drawable/ic_navigation_back" /> 

      <LinearLayout 
       android:layout_width="@dimen/dim_1" 
       android:layout_height="fill_parent" 
       android:background="@color/red" > 
      </LinearLayout> 

      <TextView 
       android:id="@+id/text_name" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:ellipsize="end" 
       android:gravity="center" 
       android:singleLine="true" 
       android:tag="bold" 
       android:text="Pixagram" 
       android:textColor="@color/white" 
       android:textSize="@dimen/tex_size_xxxlarge" 
       android:textStyle="bold" /> 

      <LinearLayout 
       android:layout_width="@dimen/dim_1" 
       android:layout_height="fill_parent" 
       android:background="@color/red" > 
      </LinearLayout> 

      <ImageButton 
       android:id="@+id/btn_Accept" 
       android:layout_width="@dimen/width" 
       android:layout_height="fill_parent" 
       android:background="@color/red" 
       android:contentDescription="@string/desc" 
       android:src="@drawable/ic_navigation_accept" /> 
     </LinearLayout> 
    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="@dimen/dim_1" 
     android:background="@color/red" > 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/root_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" > 

     <ImageView 
      android:id="@+id/imageViewEdit" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/abc_ab_solid_dark_holo" /> 
    </FrameLayout> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="@color/red" 
     android:orientation="horizontal" > 

     <HorizontalScrollView 
      android:id="@+id/horizontalScrollView1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_gravity="bottom" 
      android:gravity="bottom" 
      android:background="@color/transparent" > 

      <LinearLayout 
       android:id="@+id/linearLayout1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="horizontal" 
       android:background="@color/transparent" > 

       <ImageButton 
        android:id="@+id/imageButton1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_ic_clear" /> 

       <ImageButton 
        android:id="@+id/imageButton2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_tab_selected_pressed_holo" /> 

       <ImageButton 
        android:id="@+id/imageButton3" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_ic_clear" /> 

       <ImageButton 
        android:id="@+id/imageButton4" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_tab_selected_pressed_holo" /> 

       <ImageButton 
        android:id="@+id/imageButton5" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_ic_clear" /> 

       <ImageButton 
        android:id="@+id/imageButton6" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_tab_selected_pressed_holo" /> 

       <ImageButton 
        android:id="@+id/imageButton7" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_ic_clear" /> 

       <ImageButton 
        android:id="@+id/imageButton8" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_tab_selected_pressed_holo" /> 

       <ImageButton 
        android:id="@+id/imageButton9" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_ic_clear" /> 

       <ImageButton 
        android:id="@+id/imageButton10" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/abc_tab_selected_pressed_holo" /> 
      </LinearLayout> 
     </HorizontalScrollView> 
    </LinearLayout> 
</LinearLayout> 

這似乎表明ACTION_MOVE不叫

09-23 23:14:46.310: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch Down 
09-23 23:14:46.350: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch UP 
09-23 23:14:47.300: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch Down 
09-23 23:14:47.790: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch UP 
09-23 23:14:48.000: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch Down 
09-23 23:14:48.030: I/ViewRootImpl(24235): ViewRoot's Touch Event :261 
09-23 23:14:48.670: I/ViewRootImpl(24235): ViewRoot's Touch Event :6 
09-23 23:14:48.710: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch UP 
09-23 23:14:48.980: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch Down 
09-23 23:14:49.320: I/ViewRootImpl(24235): ViewRoot's Touch Event : Touch UP 

回答

1

雖然我沒有經歷過所有的代碼小心挖,它看起來像在你的onDraw()方法,你是_posX_posY翻譯,但你不要在你的手勢操作的任何地方改變這些。改爲使用mPosXmPosY代替onDraw()

+0

這是完美的。這與使用兩個不同的變量一樣簡單。謝謝。 – 2014-09-24 12:40:45

相關問題