2014-09-03 2 views
2

когда iam draging image1 скрывает его под задней границей image2, и мне нужно установить image1 поверх image2 после перетаскивания.Как перетащить изображение поверх другого Image backgournd

вот код: image1.setOnTouchListener (новый View.OnTouchListener() {

 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
     @SuppressLint("NewApi") 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      layoutParams = (LayoutParams) image1.getLayoutParams(); 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       break; 
      case MotionEvent.ACTION_MOVE: 
       int x_cord = (int) event.getRawX(); 
       int y_cord = (int) event.getRawY(); 

       if (x_cord > windowwidth) { 
        x_cord = windowwidth; 
       } 
       if (y_cord > windowheight) { 
        y_cord = windowheight; 
       } 

       layoutParams.leftMargin = x_cord - 25; 
       layoutParams.topMargin = y_cord - 75; 

       image1.setLayoutParams(layoutParams); 
       break; 
      default: 
       break; 
      } 
      return true; 
     } 
    }); 

это XML-файл

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="300dp" 
    android:background="#888" 
    android:orientation="vertical" 
    > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
android:layout_gravity="center" 
     android:src="@drawable/image" /> 
</FrameLayout> 
+0

interchange imageView1 и imageView2 в вашем xml –

+0

его работающий после обмена в xml – Bunny

ответ

1

Я получил мой ответ:

использовал это

   image1.bringToFront(); 

для установки изображения перед другим изображением.

Смежные вопросы