我在一个imageView上有两个imageView和一个图像,而其他ImageView是空的。我想拖动图像并放在空imageView上。我也想让Image回到第一位。简单地说我想让图像在两个imageViews之间移动。我的事情,当我拖动图像时,它也拖动imageView。我想只拖动图像而不是ImageVew因为我必须将图像移回到它的第一个位置。 我试过这个并面临问题,需要任何简单的解决方案吗?
private final class ChoiceTouchListner implements View.OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent event) {
if((event.getAction()==MotionEvent.ACTION_DOWN) && ((ImageView)v).getDrawable()!=null){
ClipData data= ClipData.newPlainText("","");
View.DragShadowBuilder shadowBuilder= new View.DragShadowBuilder(v);
v.startDrag(data,shadowBuilder,v,0);
return true;
}else{ return false;}
}
}
/////////////////////////////////////////////// /////////////////////////////////////
private class ChoiceDragListner implements View.OnDragListener{
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()){
case DragEvent.ACTION_DRAG_STARTED:
break; //no action necesary
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DROP:
ImageView imageView= (ImageView) event.getLocalState(); //the source image
((ImageView)v).setImageDrawable(getResources().getDrawable(R.drawable.blue));
// ((ImageView)v).setImageDrawable(null); //replace source by empty
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
}
return true;
}
}
答案 0 :(得分:0)
Android拖放 - 示例
你可以尝试这个例子。
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"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="400px"
android:gravity="center"
android:background="#00ff00"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/one"
android:textSize="30sp"
android:text="Drag Me" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomlinear"
android:layout_width="match_parent"
android:layout_height="400px"
android:gravity="center"
android:background="#00ffff"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/drop"
android:textSize="30sp"
android:text="Drop Here" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Total"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Sucess"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
Button drag;
LinearLayout drop;
TextView text,sucess;
int total , failure = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drag = (Button)findViewById(R.id.one);
drop = (LinearLayout)findViewById(R.id.bottomlinear);
text = (TextView)findViewById(R.id.Total);
sucess = (TextView)findViewById(R.id.Sucess);
drop.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
// TODO Auto-generated method stub
final int action = event.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_EXITED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DROP:{
failure = failure+1;
return(true);
}
case DragEvent.ACTION_DRAG_ENDED:{
total = total +1;
int suc = total - failure;
sucess.setText("Sucessful Drops :"+suc);
text.setText("Total Drops: "+total);
return(true);
}
default:
break;
}
return true;
}});
drag.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
// TODO Auto-generated method stub
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadow = new View.DragShadowBuilder(drag);
v.startDrag(data, shadow, null, 0);
return false;
}
});
}
}