0

它幾乎是6天裝盤移動的佈局,但沒有成功可言,很多試圖從互聯網幫助,但沒有幫助..Android的工作室 - 移動視圖(線性佈局)

enter image description here

我使用OnTouchListener將llPic1和llPic2移至ivNjeri上方。 因此,需要在ivNjeri之上移動自殺的球員。

有了這個代碼,它的振動移動和llPic1和llPic2去ivNjeri下:

float dx = 0, dy = 0, x = 0, y = 0; 

@Override 
public boolean onTouch(View view, MotionEvent event) { 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: { 
      x = event.getX(); 
      y = event.getY(); 
      dx = x - view.getX(); 
      dy = y - view.getY(); 
     } 
     break; 
     case MotionEvent.ACTION_MOVE: { 
      view.setX(event.getX() - dx); 
      view.setY(event.getY() - dy); 
     } 
     break; 
     } 
     return true; 
    } 

enter image description here

我也想了很多的其他代碼,但沒有工作,任何幫助將是非常非常appriciated :)

+0

你是想用這種方式動畫觀看嗎?或者你的目標是什麼? –

+0

是的,我想用戶上面大的ImageView .. –

回答

1

你應該只使用1 ViewGroup持有的所有圖像,包括圖像是重疊和圖像移動。

爲什麼圖像不能通過大圖像?

右側的小圖像位於嵌套的LinearLayout的內部,它將小圖像限制在LinearLayout之內。這就是爲什麼你可以將圖像移動到兒童LinearLayout的內部,但不能超出邊界。

一個例子使用RelativeLayout和大圖像上固定的寬度來解決它:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/bigimage" 
     android:layout_width="200dp" 
     android:layout_height="300dp" 
     android:src="@drawable/ic_launcher"/> 

    <ImageView 
     android:id="@+id/smallimage" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:src="@drawable/ic_launcher" 
     android:layout_toRightOf="@id/bigimage"/> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:src="@drawable/ic_launcher" 
     android:layout_toRightOf="@id/bigimage" 
     android:layout_below="@id/smallimage"/> 

</RelativeLayout> 

如果你想保持精確的體重,檢查出 https://developer.android.com/reference/android/support/percent/PercentFrameLayout.html https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html

這可以給你控制體重並且可以保留1 ViewGroup中的所有視圖。

+0

你救了我的命它的工作原理應該沒關係,現在我可以讓這個小圖片填滿屏幕右側(半屏第一個半屏第二個)? –

+0

'DisplayMetrics顯示= this.getResources()getDisplayMetrics(); int height = display.heightPixels; int halfheight = height/2; ivSmallImage.getLayoutParams()高度= halfheight。 ivSmallImage。requestLayout(); 它適用於bigImage但不是這一個 –

+0

如果使用PercentRelativeLayout,那將是非常容易的,但如果你不希望使用的依賴,你可能需要做數學和使用ViewTreeObserver。 – Emma

0

如果您只是想,爲什麼你不使用Animation它這樣的動畫視圖:

TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,0.0f, 0.0f);   
// new TranslateAnimation(xFrom,xTo, yFrom,yTo) 

animation.setDuration(5000); // animation duration 
animation.setRepeatCount(1); // animation repeat count 
animation.setRepeatMode(1); // repeat animation (left to right, right to left) 
//animation.setFillAfter(true);  

llPic1.startAnimation(animation); // start animation 
+0

這將由itselft下那麼大視圖中移動視圖,並再次移動視圖(或LinearLayout中使用的ImageView OnTouchListener),如圖像2 –

+0

這將通過你給位置移動您的看法它給構造函數。而移動線性佈局保持你的PIC1和PIC2以上ivNjeri您的組件樹 –

0

你可以試試這個教程link。我認爲你需要刪除您的圖片1個或PIC2(如果你在拖動後要disapeare),然後你應該添加在ivNjeri另外一個圖像視圖