拖拽后的控件回不去!!帮忙看下代码是什么问题!谢谢!!!

麻烦抢救一下 发布于 2015/06/10 09:54
阅读 51
收藏 0
这是代码:
package com.example.test;

import com.example.test1.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class Main extends Activity implements OnTouchListener{
	ImageView[] iv = new ImageView[6];
	int[] index = { R.id.imagevb1, R.id.imagevb2, R.id.imagevb3, R.id.imagevb4,
			R.id.imagevb5 ,R.id.imagevb6};
	int screenWidth, screenHeight;//界面的大小
	int lastX, lastY;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		for (int i = 0; i < index.length; i++) {
			iv[i] = (ImageView) findViewById(index[i]);
			iv[i].setOnTouchListener(this);
		}
		Display dis = this.getWindowManager().getDefaultDisplay();
		screenWidth = dis.getWidth();
		screenHeight = dis.getHeight();
	}
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (v.getId()) {
		case R.id.imagevb1:
			get(iv[0], event);
			break;
		case R.id.imagevb2:
			get(iv[1], event);
			break;
		case R.id.imagevb3:
			get(iv[2], event);
			break;
		case R.id.imagevb4:
			get(iv[3], event);
			break;
		case R.id.imagevb5:
			get(iv[4], event);
			break;
		case R.id.imagevb6:
			get(iv[5], event);
			break;
		}
		return false;
	}

	int a1, a2, a3, a4;

	public void get(ImageView v, MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:// 表示用户开始触摸
			lastX = (int) event.getRawX();
			lastY = (int) event.getRawY();
			a1 = v.getLeft();
			a2 = v.getTop();
			a3 = v.getBottom();
			a4 = v.getRight();
			break;
		case MotionEvent.ACTION_MOVE:// 表示用户在移动(手指或者其他)
			int dx = (int) event.getRawX() - lastX;
			int 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();
			break;
		case MotionEvent.ACTION_UP:// 表示用户抬起了手指
			v.layout(a1, a2, a3, a4);
			break;

		}

	}

}



这是布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imagevb1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="12dp"
            android:layout_marginLeft="146dp"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:src="@drawable/block0" />

        <ImageView
            android:id="@+id/imagevb2"
            android:layout_width="80dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imagevb1"
            android:layout_margin="12dp"
            android:layout_marginLeft="146dp"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:src="@drawable/block1" />

        <ImageView
            android:id="@+id/imagevb3"
            android:layout_width="40dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imagevb2"
            android:layout_margin="12dp"
            android:layout_marginLeft="146dp"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:src="@drawable/block2" />

        <ImageView
            android:id="@+id/imagevb4"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imagevb3"
            android:layout_margin="12dp"
            android:layout_marginLeft="146dp"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:src="@drawable/block3" />

        <ImageView
            android:id="@+id/imagevb5"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imagevb4"
            android:layout_margin="12dp"
            android:layout_marginLeft="146dp"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:src="@drawable/block4" />

        <ImageView
            android:id="@+id/imagevb6"
            android:layout_width="80dp"
            android:layout_height="120dp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/imagevb5"
            android:layout_margin="12dp"
            android:layout_marginLeft="146dp"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:src="@drawable/block5" />
    </RelativeLayout>

</FrameLayout>



问题:我要的效果是拖动完控件后,手一松开,控件就回到原来的地方。但是现在出现的问题是!第一个控件可以做到我手一松开就回去,但是其他控件我手一松就不见了!对!不见了!!我去消失了!!!大家帮忙看看是什么问题!!谢谢!!

加载中
返回顶部
顶部