拖动imageview来互换俩个图片

直接上代码:




package xutao.myapplication;

import android.content.ClipData;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    private ImageView mIv1;
    private LinearLayout mLl1;
    private ImageView mIv2;
    private LinearLayout mLl2;
    private Drawable normalShape;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        normalShape = getResources().getDrawable(R.drawable.shape);
        initView();
        initEvent();
    }

    private void initEvent() {
        mIv1.setOnTouchListener(MyTouchEvent);
        mIv2.setOnTouchListener(MyTouchEvent);
        mLl1.setOnDragListener(MyDragListener);
        mLl2.setOnDragListener(MyDragListener);
    }

    private void initView() {
        mIv1 = (ImageView) findViewById(R.id.iv1);
        mLl1 = (LinearLayout) findViewById(R.id.ll1);
        mIv2 = (ImageView) findViewById(R.id.iv2);
        mLl2 = (LinearLayout) findViewById(R.id.ll2);
    }

    View.OnTouchListener MyTouchEvent = new View.OnTouchListener()

    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ClipData data = ClipData.newPlainText("", "");
                View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
                v.startDrag(data, shadowBuilder, v, 0);
                v.setAlpha((float) 0.5);
                return true;
            } else {
                return false;
            }
        }
    };


    View.OnDragListener MyDragListener=new View.OnDragListener() {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            View visitorView = (View) event.getLocalState();
            ViewGroup visitorOwner = (ViewGroup) visitorView.getParent();
            ViewGroup visitedOwner = (ViewGroup) v;
            View visitedImage = (View)visitedOwner.getChildAt(0);

            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:

                    break;
                case DragEvent.ACTION_DRAG_ENTERED:
                    v.setAlpha((float)0.7);
                    v.setScaleX(0.7f);
                    v.setScaleY(0.7f);
                    v.setBackground(normalShape);
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    v.setAlpha((float)1.0);
                    v.setScaleX(1);
                    v.setScaleY(1);
                    v.setBackground(normalShape);
                    break;
                case DragEvent.ACTION_DROP:
                    visitorView.setAlpha((float)1.0);
                    visitedImage.setAlpha((float)1.0);

                    if (visitorOwner != visitedOwner) {
           /*
            * swap the imageviews from the containers
            */
                        visitedOwner.removeView(visitedImage);
                        visitorOwner.removeView(visitorView);
                        visitorOwner.addView(visitedImage);
                        LinearLayout container = (LinearLayout) v;
                        container.addView(visitorView);

                    } else {

                    }
                    break;
                case DragEvent.ACTION_DRAG_ENDED:
                    v.setBackgroundDrawable(normalShape);
                    v.setAlpha((float)1.0);
                    visitedImage.setAlpha((float)1.0);
                default:
                    break;
            }
            return true;
        }
    };

}




layout.XML



<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="xutao.myapplication.MainActivity">

    
    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
    <ImageView
        android:id="@+id/iv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
       />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/ll2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="100dp">

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="centerCrop"
            android:src="@drawable/logo"
            />
    </LinearLayout>
</LinearLayout>



demo下载链接:http://download.csdn.net/download/xutaojxx/10036214

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值