2017-07-06 87 views
0

在我正在使用的應用程序中,在相對佈局中有2個獨立的圖像視圖。我想有兩個圖像的移動和轉動響應給定的動作事件的XML我的活動是:防止圖像視圖在觸摸和旋轉上重疊

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.vishalbisht.dragfeature.MainActivity"> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:id="@+id/Recycle" 
    android:layout_alignParentLeft="true" /> 
<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/Ring" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" /> 

</RelativeLayout> 

雖然該計劃的部分是:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener { 

ImageView jwl,rlo; 
private final String TAG="DRAGFEATURE"; 
String Mode; 
Bitmap bitmap,rotaBitmap; 
BitmapDrawable bdr; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    jwl= (ImageView) findViewById(R.id.Recycle); 
    rlo= (ImageView) findViewById(R.id.Ring); 
    jwl.setImageResource(R.drawable.icon); 
    rlo.setImageResource(R.drawable.ring); 
    jwl.setOnTouchListener(this); 
    rlo.setOnTouchListener(this); 
} 

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    int eId = event.getActionMasked(); 
    Mode = "Move"; 
    switch (eId) { 
     case MotionEvent.ACTION_MOVE: 
       Log.v(TAG,"move image"); 
       drag(v, event); 
       break; 
     case MotionEvent.ACTION_DOWN: 
       r = r + 13; 
       rotate(v); 
       break; 
     default:break; 
    } 
    if (v != jwl && v != rlo) 
     Log.v(TAG,"Not Valid"); 

    return true; 
} 

private int r =2; 

private void rotate(View v) { 
    if(v==jwl) 
     bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.icon); 
    else 
     bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.ring); 
    int w = bitmap.getWidth(); 
    int h = bitmap.getHeight(); 
    Matrix matrix = new Matrix(); 
    matrix.preRotate(-r); 
    rotaBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,true); 
    bdr = new BitmapDrawable(rotaBitmap); 
    if(v==jwl) 
     jwl.setImageDrawable(bdr); 
    else 
     rlo.setImageDrawable(bdr); 
} 

private void drag(View v, MotionEvent event) { 
    RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams) jwl.getLayoutParams(); 
    Log.v(TAG,"Start of Move"); 
    int x = (int) event.getRawX(); 
    int y = (int) event.getRawY(); 
    mParams.leftMargin = x - 150; 
    mParams.topMargin = y - 210; 
    if(v==jwl) 
     jwl.setLayoutParams(mParams); 
    else 
     rlo.setLayoutParams(mParams); 

} 
} 

當我觸摸或拖動左圖像(回收),那麼它的罰款,但當我點擊環,然後兩個圖像重疊和移動/旋轉在一起。爲什麼會發生這種情況,以及如何解決它?

回答

1
@Override 
    public boolean onTouch(View view, MotionEvent event) { 
     int pointerCount = event.getPointerCount(); 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
//    downCounter++; 
//    Log.v("","Action Down"+downCounter); 

       startClickTime = Calendar.getInstance().getTimeInMillis(); 

       dX = view.getX() - event.getRawX(); 
       dY = view.getY() - event.getRawY(); 

       final float rawX = event.getRawX(); 
       final float rawY = event.getRawY(); 

       // Remember where we started 
       mLastTouchX = rawX; 
       mLastTouchY = rawY; 
       break; 

      case MotionEvent.ACTION_MOVE: 
//    moveCounter++; 
//    Log.v("", "Action Move "+moveCounter); 
       if (pointerCount == 1) { 

        final float x1 = event.getRawX(); 
        final float y1 = event.getRawY(); 

        // Calculate the distance moved 
        final float dx = x1 - mLastTouchX; 
        final float dy = y1 - mLastTouchY; 

        // Make sure we will still be the in parent's container 
        Rect parent = new Rect(0, 0, root.getWidth(), root.getHeight()); 

        //Find what the bounds of view will be after moving 
        int newLeft = (int) (view.getX() + dx), 
          newTop = (int) (view.getY() + dy), 
          newRight = newLeft + view.getWidth(), 
          newBottom = newTop + view.getHeight(); 

        if (!parent.contains(newLeft, newTop, newRight, newBottom)) { 
         Log.v("", "Out of Bounds"); 
        } else { 
         // Move the object 
         view.animate(). 
           x(x1 + dX). 
           y(y1 + dY). 
           setDuration(0). 
           start(); 
         // Remember this touch position for the next move event 
         mLastTouchX = x1; 
         mLastTouchY = y1; 
         // Invalidate to request a redraw 
         root.invalidate(); 
         break; 
        } 
       } 
//    if(mode==2) 
       if (pointerCount == 2) 
       { 
        long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime; 
        if(clickDuration > MAX_CLICK_DURATION) { 
         //Log.v(TAG, "for rotation"); 

         float degree = rotation(event); 

         //Log.v(TAG, "setting at angle" + degree); 

         view.setRotation(view.getRotation() + degree); 
         break; 

        } 
       } 
       break; 

      case MotionEvent.ACTION_UP: 
//    upCounter++; 
//    Log.v(TAG,"Action Up"+upCounter); 


      /* case MotionEvent.ACTION_POINTER_DOWN: 
       Log.v(TAG, "ACTION POINTER DOWN"); 
       if (pointerCount == 2) { 
        flagFirstTouch = 1; 
       } 
       break;*/ 

     } 
     return true; 
    } 

我用這個排序我的問題,我希望這可以幫助別人。

此外,保持雙方在父RelativeLayout.The frameLayouts內單獨frameLayouts圖像視圖可以有兩種高度andwidth

0

如下改變你的佈局XML:

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:id="@+id/Recycle" 
    android:layout_alignParentLeft="true" /> 
<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/Ring" 
    android:layout_toRightOf="@+id/Recycle" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" /> 
+0

我只是想你的解決方案,而這是移動ImageView的ID肯定更好屬性WRAP_CONTENT:回收當我觸摸Imageview ID時,我仍然遇到同樣的問題:響鈴或拖動它。 –