我有一個ImageView
和Button
在LinearLayout
。我想拖動屏幕上的ImageView
,但仍然保留Button
。但由於某些原因,當我拖動我的ImageView
Button
一起拖動它。圖像拖動導致按鈕拖動
有人可以幫忙嗎?
代碼:
activity_main.xml中
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:weightSum=10
android:id="@+id/main">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_pic"
android:layout_weight="2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.5"/>
<ImageView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/btn_click"
android:layout_weight="2"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnTouchListener
{
ImageView img;
private ViewGroup mRrootLayout;
private int _xDelta;
private int _yDelta;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRrootLayout = (ViewGroup) findViewById(R.id.main);
img = (ImageView) mRrootLayout.findViewById(R.id.image);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300);
img.setLayoutParams(layoutParams);
img.setOnTouchListener(this);
}
編輯
public boolean onTouch(View view, MotionEvent event) {
switch (view.getId()) {
case R.id.logo:
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view
.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
break;
}
mRrootLayout.invalidate();
break;
case R.id.fade:
return false;
}
return true;
}
哈是的。我改變了這一點。但是,你有沒有解決辦法? – user2456977 2015-02-11 13:38:30