简单的拖拽图片

今天试了一下图片拖拽,拖拽过去伴随着自动计数功能,很简单的小 程序。代码如下:

布局文件
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.tarena.dragimage.MainActivity">

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="26dp"
        android:layout_y="31dp"
        android:id="@+id/textView2"/>

    <RelativeLayout
        android:background="@android:color/holo_blue_bright"
        android:layout_width="109dp"
        android:layout_height="162dp"
        android:layout_x="33dp"
        android:layout_y="261dp"
        android:id="@+id/leftLayout">

    </RelativeLayout>

    <RelativeLayout
        android:background="@color/colorAccent"
        android:layout_width="109dp"
        android:layout_height="162dp"
        android:layout_x="228dp"
        android:layout_y="274dp">

        <RelativeLayout
            android:background="@color/colorAccent"
            android:layout_width="109dp"
            android:layout_height="162dp"
            android:layout_x="229dp"
            android:layout_y="264dp">

            <RelativeLayout
                android:background="@color/colorAccent"
                android:layout_width="109dp"
                android:layout_height="162dp"
                android:layout_x="50dp"
                android:layout_y="252dp"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="26dp"
                android:id="@+id/rightLayout">

            </RelativeLayout>
        </RelativeLayout>

    </RelativeLayout>

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="281dp"
        android:layout_y="35dp"
        android:id="@+id/textView3"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_x="77dp"
        android:layout_y="112dp"
        android:id="@+id/imageView2"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_x="166dp"
        android:layout_y="110dp"
        android:id="@+id/imageView"/>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_x="257dp"
        android:layout_y="111dp"
        android:id="@+id/imageView3"/>```


#####MainActivity
@BindViews({R.id.imageView, R.id.imageView2, R.id.imageView3})
List<ImageView> imageviews;

@BindView(R.id.textView2)
TextView leftCountTV;
@BindView(R.id.textView3)
TextView rightCountTV;
@BindView(R.id.activity_main)
AbsoluteLayout activityMain;
@BindView(R.id.leftLayout)
RelativeLayout leftLayout;
@BindView(R.id.rightLayout)
RelativeLayout rightLayout;
private ImageView dragIV;

int leftCount;
int rightCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    // 隐藏状态栏
    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager
            .LayoutParams.FLAG_FULLSCREEN);


    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            for (ImageView imageview : imageviews) {
                Rect r = new Rect();
                imageview.getGlobalVisibleRect(r);

                String s = "";


                if (r.contains((int) event.getRawX(), (int) event.getRawY())) {

                    dragIV = new ImageView(this);
                    //获取点击图片的bitmap
                    Bitmap b = ((BitmapDrawable) imageview.getDrawable()).getBitmap();
                    dragIV.setImageBitmap(b);
                    dragIV.setLayoutParams(imageview.getLayoutParams());
                    activityMain.addView(dragIV);
                }


            }

            break;
        case MotionEvent.ACTION_MOVE:

            if (dragIV != null) {
                dragIV.setX(event.getRawX()-dragIV.getWidth()/2);
                dragIV.setY(event.getRawY()-dragIV.getHeight()/2);
            }

            break;
        case MotionEvent.ACTION_UP:

            if (dragIV!=null) {
                Rect leftRect = new Rect();
                Rect rightRect = new Rect();
                leftLayout.getGlobalVisibleRect(leftRect);
                rightLayout.getGlobalVisibleRect(rightRect);
                if (leftRect.contains((int)event.getRawX(),(int)event.getRawY())) {

                leftCountTV.setText("" + ++leftCount);

                }else if (rightRect.contains((int)event.getRawX(),(int)event.getRawY())) {

                    rightCountTV.setText("" + ++rightCount);


                }else{//在外面松手
                    activityMain.removeView(dragIV);
                }
            }

            break;
    }


    return super.onTouchEvent(event);
}```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容