1. 程式人生 > >Android 拖拽控制元件交換位置

Android 拖拽控制元件交換位置

 
package com.example.administrator.jiemiantest;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnTouchListener{

    int screenWidth;
    int screenHeight;
    int lastX;
    int lastY;
    ImageView sunwukong;
    ImageView prettyBoy;
    boolean sunwukong_resetLeftFlag = false;
    boolean sunwukong_resetRightFlag = false;
    boolean prettyBoy_resetLeftflag = false;
    boolean prettyBoy_resetRightflag = false;
    int rightMove_flag ;
    int leftMove_flag;
    int dx;
    int dy;
    @Override
    public void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels - 50;
        sunwukong = findViewById(R.id.sunwukong);
        sunwukong.setOnTouchListener(this);
        prettyBoy=findViewById(R.id.prettyBoy);
        prettyBoy.setOnTouchListener(this);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        int action=event.getAction();
        Log.i("@@@@@@", "Touch:"+action);
        //Toast.makeText(DraftTest.this, "λ�ã�"+x+","+y, Toast.LENGTH_SHORT).show();
        switch(action){
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();

                break;
            /**
             * layout(l,t,r,b)
             * l  Left position, relative to parent
             t  Top position, relative to parent
             r  Right position, relative to parent
             b  Bottom position, relative to parent
             * */
            case MotionEvent.ACTION_MOVE:
                 dx =(int)event.getRawX() - lastX;
                 dy =(int)event.getRawY() - lastY;

                int left = v.getLeft() + dx;
                int top = v.getTop() + dy;
                int right = v.getRight() + dx;
                int bottom = v.getBottom() + dy;
                if(left < 0){
                    left = 0;
                    right = left + v.getWidth();
                }
                if(right > screenWidth){
                    right = screenWidth;
                    left = right - v.getWidth();
                }
                if(top < 0){
                    top = 0;
                    bottom = top + v.getHeight();
                }
                if(bottom > screenHeight){
                    bottom = screenHeight;
                    top = bottom - v.getHeight();
                }
                v.layout(left, top, right, bottom);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                switch (v.getId()){
                    case R.id.sunwukong:
                        if(dx > 0){
                            if(sunwukong.getLeft() >=150 ){
                                if(300 == prettyBoy.getLeft()) {
                                    prettyBoy.layout(0, 0, 300, 300);
                                    Animation translateAnimation = new TranslateAnimation(300, 0, 0, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    prettyBoy.startAnimation(translateAnimation);
                                    rightMove_flag = 1;
                                }
                            }
                        }else{
                            if(sunwukong.getLeft()<=150){
                                if(0 == prettyBoy.getLeft()) {
                                    prettyBoy.layout(300, 0, 600, 300);
                                    Animation translateAnimation = new TranslateAnimation(-300, 0, 0, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    prettyBoy.startAnimation(translateAnimation);
                                    leftMove_flag = 1;
                                }
                            }
                        }
                        break;
                    case R.id.prettyBoy:
                        if(dx > 0){
                            if(prettyBoy.getLeft() >= 150){
                                if(300 ==sunwukong.getLeft()) {
                                    sunwukong.layout(0, 0, 300, 300);
                                    Animation translateAnimation = new TranslateAnimation(300, 0, 0, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    sunwukong.startAnimation(translateAnimation);
                                    rightMove_flag = 1;
                                }
                            }
                        }else{
                            if(prettyBoy.getLeft() <= 150){
                                if(0 == sunwukong.getLeft()) {
                                    sunwukong.layout(300, 0, 600, 300);
                                    Animation translateAnimation = new TranslateAnimation(-300, 0, 0, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    sunwukong.startAnimation(translateAnimation);
                                    leftMove_flag = 1;
                                }
                            }
                        }
                }
                break;
            case MotionEvent.ACTION_UP:

                        switch (v.getId()){
                            case R.id.sunwukong:
                                if(dx > 0){
                                    if(sunwukong.getLeft() <=150 ){
                                        sunwukong_resetLeftFlag =true;
                                    }
                                    if(sunwukong.getLeft()>=300){
                                        sunwukong_resetRightFlag = true;
                                    }
                            }else{
                                if(sunwukong.getLeft()>=150){
                                    sunwukong_resetRightFlag = true;
                                }

                            }
                                 if(1 == rightMove_flag){
                                     rightMove_flag = 0;
                                     int x1 = sunwukong.getLeft();
                                     int x2 = sunwukong.getTop();
                                     sunwukong.layout(300,0,600,300);
                                     Animation translateAnimation = new TranslateAnimation(x1-300, 0, x2, 0);
                                     translateAnimation.setFillAfter(true);
                                     translateAnimation.setDuration(600);
                                     sunwukong.startAnimation(translateAnimation);
                                     break;
                                 }
                                 if(sunwukong_resetLeftFlag){
                                     sunwukong_resetLeftFlag=false;
                                     int x1 = sunwukong.getLeft();
                                     int high = sunwukong.getTop();
                                     sunwukong.layout(0,0,300,300);
                                     Animation translateAnimation = new TranslateAnimation(x1, 0, high, 0);
                                     translateAnimation.setFillAfter(true);
                                     translateAnimation.setDuration(600);
                                     sunwukong.startAnimation(translateAnimation);
                                     break;
                                 }
                                 if(1 ==leftMove_flag){
                                     leftMove_flag =0;
                                     int x1 = sunwukong.getLeft();
                                     int x2 = sunwukong.getTop();
                                     sunwukong.layout(0,0,300,300);
                                     Animation translateAnimation = new TranslateAnimation(x1, 0, x2, 0);
                                     translateAnimation.setFillAfter(true);
                                     translateAnimation.setDuration(600);
                                     sunwukong.startAnimation(translateAnimation);
                                     break;
                                 }
                                 if(sunwukong_resetRightFlag){
                                     sunwukong_resetRightFlag = false;
                                     int x1 = sunwukong.getLeft();
                                     int x2 = sunwukong.getTop();
                                     sunwukong.layout(300,0,600,300);
                                     Animation translateAnimation = new TranslateAnimation(x1-300, 0, x2, 0);
                                     translateAnimation.setFillAfter(true);
                                     translateAnimation.setDuration(600);
                                     sunwukong.startAnimation(translateAnimation);
                                     break;
                                 }

                                break;
                            case R.id.prettyBoy:
                                if(dx > 0){
                                    if(prettyBoy.getLeft() <= 150){
                                        prettyBoy_resetLeftflag =true;
                                    }
                                    if(prettyBoy.getLeft() >= 300){
                                        prettyBoy_resetRightflag = true;
                                    }
                                }else{
                                    if(prettyBoy.getLeft() >= 150){
                                        prettyBoy_resetRightflag = true;
                                    }
                                }
                                if(1 == rightMove_flag){
                                    rightMove_flag = 0;
                                    int x1 = prettyBoy.getLeft();
                                    int x2 = prettyBoy.getTop();
                                    prettyBoy.layout(300,0,600,300);
                                    Animation translateAnimation = new TranslateAnimation(x1-300, 0, x2, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    prettyBoy.startAnimation(translateAnimation);
                                    break;
                                }
                                if(prettyBoy_resetLeftflag){
                                    prettyBoy_resetLeftflag=false;
                                    int x1 = prettyBoy.getLeft();
                                    int high = prettyBoy.getTop();
                                    prettyBoy.layout(0,0,300,300);
                                    Animation translateAnimation = new TranslateAnimation(x1, 0, high, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    prettyBoy.startAnimation(translateAnimation);
                                    break;
                                }
                                if(1 ==leftMove_flag){
                                    leftMove_flag =0;
                                    int x1 = prettyBoy.getLeft();
                                    int x2 = prettyBoy.getTop();
                                    prettyBoy.layout(0,0,300,300);
                                    Animation translateAnimation = new TranslateAnimation(x1, 0, x2, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    prettyBoy.startAnimation(translateAnimation);
                                    break;
                                }
                                if(prettyBoy_resetRightflag){
                                    prettyBoy_resetRightflag = false;
                                    int x1 = prettyBoy.getLeft();
                                    int x2 = prettyBoy.getTop();
                                    prettyBoy.layout(300,0,600,300);
                                    Animation translateAnimation = new TranslateAnimation(x1-300, 0, x2, 0);
                                    translateAnimation.setFillAfter(true);
                                    translateAnimation.setDuration(600);
                                    prettyBoy.startAnimation(translateAnimation);
                                    break;
                                }
                                break;
                        }
                break;
        }

        return false;
    }
}