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另外一个图像视图