我有五个线性布局..我把一个图像放在centerlayout中,现在如果用户点击粉色布局图像应该在那个布局中传输,我使用了动画,那么任何人都可以帮我吗?
public class MainActivity extends Activity implements OnTouchListener,OnDragListener{
private static final String LOGCAT = null;
private LinearLayout centers;
private ImageView imageview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
centers=(LinearLayout)findViewById(R.id.centLayout);
imageview1=(ImageView)findViewById(R.id.textView1);
findViewById(R.id.textView1).setOnTouchListener(this);
findViewById(R.id.pinkLayout).setOnDragListener(this);
findViewById(R.id.yellowLayout).setOnDragListener(this);
findViewById(R.id.blackLayout).setOnDragListener(this);
findViewById(R.id.centLayout).setOnDragListener(this);
findViewById(R.id.redLayout).setOnDragListener(this);
centers.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
imageview1.setVisibility(View.VISIBLE);
imageview1.bringToFront();
int y = (int)event.getY();
int x = (int)event.getX();
imageview1.getLayoutParams().height = 20;
imageview1.getLayoutParams().width = 20;
imageview1.layout(x, y, x+48, y+48);
return false;
}
});
}
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(null, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
}
else {
return false;
}
}
public boolean onDrag(View layoutview, DragEvent dragevent) {
int action = dragevent.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
Log.d(LOGCAT, "Drag event started");
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.d(LOGCAT, "Drag event entered into "+layoutview.toString());
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.d(LOGCAT, "Drag event exited from "+layoutview.toString());
break;
case DragEvent.ACTION_DROP:
Log.d(LOGCAT, "Dropped");
View view = (View) dragevent.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) layoutview;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.d(LOGCAT, "Drag ended");
break;
default:
break;
}
return true;
}
}
activity_main.xml中
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/center"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/pinkLayout"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:background="#FF8989"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/blackLayout"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_below="@+id/pinkLayout"
android:layout_above="@+id/yellowLayout"
android:background="#000000"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:id="@+id/centLayout"
android:layout_width="170dp"
android:layout_height="match_parent"
android:layout_below="@+id/pinkLayout"
android:layout_above="@+id/yellowLayout"
android:layout_toRightOf="@+id/blackLayout"
android:background="#D30000"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/textView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/spadeone"
android:layout_gravity="center"
android:layout_marginLeft="55dp"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/redLayout"
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_below="@+id/pinkLayout"
android:layout_above="@+id/yellowLayout"
android:layout_toRightOf="@+id/blackLayout"
android:background="#000000"
android:orientation="vertical"
android:layout_marginLeft="170dp"
>
</LinearLayout>
<LinearLayout
android:id="@+id/yellowLayout"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:background="#FFCC00"
android:orientation="vertical"
android:layout_alignParentBottom="true"
>
</LinearLayout>
</RelativeLayout>
move_up.xml
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toYDelta="-65%p"
android:duration="800" />
</set>
答案 0 :(得分:0)
获取特定位置的Rect坐标并检查拖动的项目坐标是否与Rect坐标位置发生冲突。您可以使用Rect.contains()api进行检查。如果返回true,则可以显示警报。
if (locationRect.contains(drag.left, drag.top, drag.right, drag.bottom)) {
// do what you want to do now
}
要跟踪拖动视图的位置,请将以下内容添加到拖动侦听器
case DragEvent.ACTION_DRAG_EXITED :
x_cord = (int) event.getX(); // add to your dragEvent
y_cord = (int) event.getY(); // add to your dragEvent
case DragEvent.ACTION_DRAG_LOCATION :
x_cord = (int) event.getX(); // add to your dragEvent
y_cord = (int) event.getY(); // add to your dragEvent