如何从一个ImageView的拖动图像拖放到另一个图像视图。 [英] How to drag images from one imageview and drop to another image view.

查看:148
本文介绍了如何从一个ImageView的拖动图像拖放到另一个图像视图。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 public class Quiz extends Activity {

        int windowwidth;
        int windowheight;
        ImageView ima ima2;
        private View selected_item = null;
        private int offset_x = 0;
        private int offset_y = 0;

        private android.widget.LinearLayout.LayoutParams layoutParams1,
                layoutParams2;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.quiz);

            windowwidth = getWindowManager().getDefaultDisplay().getWidth();
            windowheight = getWindowManager().getDefaultDisplay().getHeight();

            System.out.println("width" + windowwidth);
            System.out.println("height" + windowheight);
   //on touch listener for image view 
            ima1 = (ImageView) findViewById(R.id.shuffledimage);
            ima1.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    layoutParams1 = (LinearLayout.LayoutParams) ima1
                            .getLayoutParams();
                    try {//action performed in image view
                        switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams1.leftMargin = x_cord - 25;
                            layoutParams1.topMargin = y_cord - 75;
                            ima1.setLayoutParams(layoutParams1);
                            break;

                        default:
                            break;
                        }
                    } catch (Exception e) {
                        // TODO: handle exception
                        e.getMessage();
                    }

                    return true;
                }
            });

            ima2 = (ImageView) findViewById(R.id.userimage);
            ima2.setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {

                    layoutParams2 = (LinearLayout.LayoutParams) ima2
                            .getLayoutParams();
                    try {
                        switch (event.getActionMasked()) {
                        case MotionEvent.ACTION_DOWN:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            int x_cord = (int) event.getRawX();
                            int y_cord = (int) event.getRawY();
                            if (x_cord > windowwidth) {
                                x_cord = windowwidth;
                            }
                            if (y_cord > windowheight) {
                                y_cord = windowheight;
                            }
                            layoutParams2.leftMargin = x_cord - 25;
                            layoutParams2.topMargin = y_cord - 75;
                            ima2.setLayoutParams(layoutParams2);
                            break;
                        default:
                            break;

                        }

                    } catch (Exception e) {
                        e.getMessage();
                    }
                    return true;

                }

            });

        }

    }

// xml文件

//xml file

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ipodbackground"
        android:orientation="vertical" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="@drawable/headingmain1"
            android:gravity="center_horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="65dp"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:background="#00000000" />

            <Button
                android:id="@+id/button2"
                android:layout_width="40dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:background="#00000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:background="@drawable/quiztitle1" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="75dp"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp" >

            <LinearLayout
                android:id="@+id/l1"
                android:layout_width="250dp"
                android:layout_height="wrap_content"
                android:layout_marginRight="25dp"
                android:background="@drawable/box"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="5dp"
                    android:text="TextView" />


                <ImageView
                    android:id="@+id/userimage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="160dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:src="@drawable/ic_launcher" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:layout_marginRight="5dp"
                android:layout_toRightOf="@+id/l1" >

                <ImageView
                    android:id="@+id/shuffledimage"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/iconsearch" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>

//
    我想将它从一个图像视图之间的ImageView和上面code部分图像拖放图像时,我拖累了可以正确拖累,但图像倾斜下降到源图像视图。有人请给予解决方案。

// i want to drag and drop image from one image view to another imageview and in above code section image can be dragged correctly when i drag but the image cant dropped to the source image view. anybody please give solution

推荐答案

有关拖放,请通过下面的链接。

For drag and drop please go through the following links .

的http:// blahti .word press.com / 2011/10/03 /拖放换Android的GridView控件/

http://www.twylah.com/dodulz/tweets/169426982533206016

这篇关于如何从一个ImageView的拖动图像拖放到另一个图像视图。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆