Android拖放(无法洗牌图像)
我尝试实现android拖放。我得到了以下输出: 我有两个布局,从第一个布局我可以拖动图标或图像到下一个布局。我的问题是不能洗牌我的图像和合并视图。任何建议都会对我有帮助。 多谢各位Android拖放(无法洗牌图像),android,android-activity,Android,Android Activity,我尝试实现android拖放。我得到了以下输出: 我有两个布局,从第一个布局我可以拖动图标或图像到下一个布局。我的问题是不能洗牌我的图像和合并视图。任何建议都会对我有帮助。 多谢各位 public class MainActivity extends Activity { private ImageView myImage,myImage1,myImage2,myImage3; private static final String IMAGEVIEW_TAG = "The A
public class MainActivity extends Activity {
private ImageView myImage,myImage1,myImage2,myImage3;
private static final String IMAGEVIEW_TAG = "The Android Logo";
private static final String IMAGEVIEW_TAG1 = "The Android Logo1";
private static final String IMAGEVIEW_TAG2 = "The Android Logo2";
private static final String IMAGEVIEW_TAG3 = "The Android Logo3";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
myImage = (ImageView)findViewById(R.id.image);
myImage1 = (ImageView)findViewById(R.id.image1);
myImage2 = (ImageView)findViewById(R.id.image2);
myImage3 = (ImageView)findViewById(R.id.image3);
// Sets the tag
myImage.setTag(IMAGEVIEW_TAG);
myImage1.setTag(IMAGEVIEW_TAG1);
myImage2.setTag(IMAGEVIEW_TAG2);
myImage3.setTag(IMAGEVIEW_TAG3);
// set the listener to the dragging data
myImage.setOnLongClickListener(new MyClickListener());
myImage1.setOnLongClickListener(new MyClickListener());
myImage2.setOnLongClickListener(new MyClickListener());
myImage3.setOnLongClickListener(new MyClickListener());
findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
}
private final class MyClickListener implements OnLongClickListener {
// called when the item is long-clicked
@Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
// create it from the object's tag
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data, //data to be dragged
shadowBuilder, //drag shadow
view, //local data about the drag and drop operation
0 //no needed flags
);
view.setVisibility(View.INVISIBLE);
return true;
}
}
class MyDragListener implements OnDragListener {
Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);
@Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(targetShape); //change the shape of the view
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
// if the view is the bottomlinear, we accept the drag item
if(v == findViewById(R.id.bottomlinear)) {
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
//change the text
// TextView text = (TextView) v.findViewById(R.id.text);
// text.setText("The item is dropped");
GridLayout containView = (GridLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
Context context = getApplicationContext();
Toast.makeText(context, "You can't drop the image here",
Toast.LENGTH_LONG).show();
break;
}
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
}
您无法洗牌图像和合并视图的原因是您没有正确处理放置事件 看看这个拖放式开源示例:
谢谢..我有一个问题,我只想从上到下拆分屏幕..我的意思是从第一个屏幕我想将图像拖到下一个屏幕..而不是寻呼机..任何建议都会有帮助