拖放图像并在android中移动它们的位置(drag and drop images and shift their position in android)

系统教程 行业动态 更新时间:2024-06-14 16:57:40
拖放图像并在android中移动它们的位置(drag and drop images and shift their position in android)

我有一个布局,我已经放置了图像视图和文本视图文本视图位置固定在布局上但图像视图不固定当我拖动和图像和放在其他地方所有图像的位置自动更改。

例如:我有4个图像,我拖动3个图像并放在1个图像的位置,然后1个图像将放置在第2个位置,第2个图像放在第3个等等。任何一个有代码请提供我没有代码这个我只是创建一个包含图像视图和文本视图的简单布局

提前致谢。

i have a layout and i have placed image view and text view on it text view position are fixed on the layout but image view are not fixed when i drag and image and drop at some other place the position of all the images automatically changed .

example : i have 4 images and i drag 3 image and drop at the place of 1 image then 1 image will placed at 2nd position and second image at third and so on.any one has code please provide i have no code for this i just create a simple layout that contains image view and text view

thanks in advance.

最满意答案

这是我的答案

公共类MainActivity扩展Activity {

private ImageView option1, option2, choice1, choice2; TextView tv1, tv2; int mScrollDistance; MyScrollView myScrollView; LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); tv1.setTag("0"); tv2.setTag("0"); // views to drag option1 = (ImageView) findViewById(R.id.option_1); option2 = (ImageView) findViewById(R.id.option_2); // views to drop onto choice1 = (ImageView) findViewById(R.id.choice_1); choice2 = (ImageView) findViewById(R.id.choice_2); // set touch listeners option1.setOnLongClickListener(new ChoiceTouchListener()); option2.setOnLongClickListener(new ChoiceTouchListener()); option1.setOnDragListener(new ChoiceDragListener()); option2.setOnDragListener(new ChoiceDragListener()); option1.setTag("1"); option2.setTag("1"); // set drag listeners choice1.setOnLongClickListener(new ChoiceTouchListener()); choice2.setOnLongClickListener(new ChoiceTouchListener()); choice1.setOnDragListener(new ChoiceDragListener()); choice2.setOnDragListener(new ChoiceDragListener()); choice1.setTag("1"); choice2.setTag("1"); tv1.setOnDragListener(new ChoiceDragListener()); tv2.setOnDragListener(new ChoiceDragListener()); myScrollView = (MyScrollView) findViewById(R.id.scroll); myScrollView .setOnScrollViewListener(new MyScrollView.OnScrollViewListener(){ @Override public void onScrollChanged1(OnScrollViewListener listener) { // TODO Auto-generated method stub mScrollDistance = myScrollView.getScrollY(); } }); } private final class ChoiceTouchListener implements OnLongClickListener { @SuppressLint("NewApi") public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { // setup drag ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder shadowBuilder = new View.DragShadowBuilder( view); // start dragging the item touch ed view.startDrag(data, shadowBuilder, view, 0); } return true; } @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); // start dragging the item touch ed v.startDrag(data, shadowBuilder, v, 0); return true; } } private class ChoiceDragListener implements OnDragListener { @SuppressWarnings("deprecation") @Override public boolean onDrag(View v, DragEvent event) { // handle drag events switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: // no action necessary System.out.println("Started"); break; case DragEvent.ACTION_DRAG_ENTERED: System.out.println("ENtered"); // no action necessary break; case DragEvent.ACTION_DRAG_EXITED: // no action necessary System.out.println("Exited"); break; case DragEvent.ACTION_DRAG_LOCATION: int y = Math.round(v.getY());// + Math.round(event.getY()); int translatedY = y - mScrollDistance; // System.out.printf("translated", "" + translatedY + " " // + mScrollDistance + " " + y); int threshold = 50; // make a scrolling up due the y has passed the threshold if (translatedY < 200) { // make a scroll up by 30 px myScrollView.smoothScrollBy(0, -30); } // make a autoscrolling down due y has passed the 500 px border if (translatedY + threshold > 500) { // make a scroll down by 30 px myScrollView.smoothScrollBy(0, 30); } break; case DragEvent.ACTION_DROP: View view = (View) event.getLocalState(); if (((String) v.getTag()).equals("0")) { } else if (((String) v.getTag()).equals("1")) { ImageView dropTarget = (ImageView) v; // view being dragged and dropped ImageView dropped = (ImageView) view; Drawable temp_img = dropped.getBackground(); dropped.setBackgroundDrawable(dropTarget.getBackground()); dropTarget.setBackgroundDrawable(temp_img); } break; case DragEvent.ACTION_DRAG_ENDED: // no action necessary break; default: break; } return true; } }

这是第二个使用的类

public class MyScrollView extends ScrollView { public OnScrollViewListener mListener; public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub super.onScrollChanged(l, t, oldl, oldt); if (mListener != null) { mListener.onScrollChanged1(mListener); } } public void setOnScrollViewListener(OnScrollViewListener listener) { mListener = listener; } public static interface OnScrollViewListener { public void onScrollChanged1(OnScrollViewListener listener); }

}

here is my answer

public class MainActivity extends Activity {

private ImageView option1, option2, choice1, choice2; TextView tv1, tv2; int mScrollDistance; MyScrollView myScrollView; LinearLayout layout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv1 = (TextView) findViewById(R.id.tv1); tv2 = (TextView) findViewById(R.id.tv2); tv1.setTag("0"); tv2.setTag("0"); // views to drag option1 = (ImageView) findViewById(R.id.option_1); option2 = (ImageView) findViewById(R.id.option_2); // views to drop onto choice1 = (ImageView) findViewById(R.id.choice_1); choice2 = (ImageView) findViewById(R.id.choice_2); // set touch listeners option1.setOnLongClickListener(new ChoiceTouchListener()); option2.setOnLongClickListener(new ChoiceTouchListener()); option1.setOnDragListener(new ChoiceDragListener()); option2.setOnDragListener(new ChoiceDragListener()); option1.setTag("1"); option2.setTag("1"); // set drag listeners choice1.setOnLongClickListener(new ChoiceTouchListener()); choice2.setOnLongClickListener(new ChoiceTouchListener()); choice1.setOnDragListener(new ChoiceDragListener()); choice2.setOnDragListener(new ChoiceDragListener()); choice1.setTag("1"); choice2.setTag("1"); tv1.setOnDragListener(new ChoiceDragListener()); tv2.setOnDragListener(new ChoiceDragListener()); myScrollView = (MyScrollView) findViewById(R.id.scroll); myScrollView .setOnScrollViewListener(new MyScrollView.OnScrollViewListener(){ @Override public void onScrollChanged1(OnScrollViewListener listener) { // TODO Auto-generated method stub mScrollDistance = myScrollView.getScrollY(); } }); } private final class ChoiceTouchListener implements OnLongClickListener { @SuppressLint("NewApi") public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { // setup drag ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder shadowBuilder = new View.DragShadowBuilder( view); // start dragging the item touch ed view.startDrag(data, shadowBuilder, view, 0); } return true; } @Override public boolean onLongClick(View v) { // TODO Auto-generated method stub ClipData data = ClipData.newPlainText("", ""); DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v); // start dragging the item touch ed v.startDrag(data, shadowBuilder, v, 0); return true; } } private class ChoiceDragListener implements OnDragListener { @SuppressWarnings("deprecation") @Override public boolean onDrag(View v, DragEvent event) { // handle drag events switch (event.getAction()) { case DragEvent.ACTION_DRAG_STARTED: // no action necessary System.out.println("Started"); break; case DragEvent.ACTION_DRAG_ENTERED: System.out.println("ENtered"); // no action necessary break; case DragEvent.ACTION_DRAG_EXITED: // no action necessary System.out.println("Exited"); break; case DragEvent.ACTION_DRAG_LOCATION: int y = Math.round(v.getY());// + Math.round(event.getY()); int translatedY = y - mScrollDistance; // System.out.printf("translated", "" + translatedY + " " // + mScrollDistance + " " + y); int threshold = 50; // make a scrolling up due the y has passed the threshold if (translatedY < 200) { // make a scroll up by 30 px myScrollView.smoothScrollBy(0, -30); } // make a autoscrolling down due y has passed the 500 px border if (translatedY + threshold > 500) { // make a scroll down by 30 px myScrollView.smoothScrollBy(0, 30); } break; case DragEvent.ACTION_DROP: View view = (View) event.getLocalState(); if (((String) v.getTag()).equals("0")) { } else if (((String) v.getTag()).equals("1")) { ImageView dropTarget = (ImageView) v; // view being dragged and dropped ImageView dropped = (ImageView) view; Drawable temp_img = dropped.getBackground(); dropped.setBackgroundDrawable(dropTarget.getBackground()); dropTarget.setBackgroundDrawable(temp_img); } break; case DragEvent.ACTION_DRAG_ENDED: // no action necessary break; default: break; } return true; } }

} here is the second class used

public class MyScrollView extends ScrollView { public OnScrollViewListener mListener; public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub super.onScrollChanged(l, t, oldl, oldt); if (mListener != null) { mListener.onScrollChanged1(mListener); } } public void setOnScrollViewListener(OnScrollViewListener listener) { mListener = listener; } public static interface OnScrollViewListener { public void onScrollChanged1(OnScrollViewListener listener); }

}

更多推荐

本文发布于:2023-04-13 12:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/e83dc631eac11198e7acadfe7e07a46a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   拖放   中移动   图像   位置

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!