Drag---长按控件进行拖动

运行效果图:


Drag---长按控件进行拖动_第1张图片

自定义的控件,新建一个被拖动View控件,Dragging.java:


import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

//自定义的View类型的子类,作为被拖动View
public class Dragging extends View {
	//View的构造方法
	public Dragging(Context context) {
		super(context);
	}
	//带参的构造方法
	public Dragging(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
}

自定义控件,新建一个拖动完放置的View控件,pass.java:



package com.bzu.test064view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
//自定义存放拖动的View
public class Pass extends View {

	public Pass(Context context) {
		super(context);
	}

	public Pass(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

}


修改activity_main.xml文件,如下:




    

    

	

    
    




修改MainActivity.java文件,如下:




import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.DragEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;

public class MainActivity extends Activity {
	private View ViewD;
	private View ViewP;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findById();
		setListener();
	}
	
	private void setListener() {
		//设置监听器,长按后拖动
		ViewD.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View view) {
				ViewD.startDrag(null, new DragShadowBuilder(view), view, 0);
				return true;
			}
		});
		//开始拖动时回调的函数,setOnDragListener
		ViewP.setOnDragListener(new OnDragListener() {
			@Override
			public boolean onDrag(View mDropview, DragEvent event) {
				boolean result=false;
				switch (event.getAction()) {
				//开始拖动时
				case DragEvent.ACTION_DRAG_STARTED:{
					Log.e("Dragging", "View开始被拖动");
					result=true;
					break;
				}
				//被拖动的额对象进入当前view对象时
				case DragEvent.ACTION_DRAG_ENTERED:{
					Log.e("Dragging", "被拖动的View对象进入当前的View");
					break;
				}
				//被拖动的View进入当前的View后,位置改变时回调
				case DragEvent.ACTION_DRAG_LOCATION:{
					Log.e("Dragging", "被拖动的view进入当前view");
					break;
				}
				//拖动的View被放入当前的View时回调
				case DragEvent.ACTION_DROP:{
					Log.e("Dragging", "拖动的View被放入当前View");
					View mDragView=(View) event.getLocalState();
					//得到拖动视图的背景
					Drawable mDragViewBackGround=mDragView.getBackground();
					mDragView.setBackgroundDrawable(mDropview.getBackground());
					mDropview.setBackgroundDrawable(mDragViewBackGround);
					break;
				}
				//拖动结束时
				case DragEvent.ACTION_DRAG_ENDED:{
					Log.e("Dragging", "View拖动结束");
					break;
				}
				//拖动完成时
				case DragEvent.ACTION_DRAG_EXITED:{
					Log.e("Dragging", "View拖动退出");
					break;
				}
				default :{
					break;
				}
				}
				return result;
			}
		});
	}

	private void findById() {
		ViewD=findViewById(R.id.ViewD);
		ViewP=findViewById(R.id.ViewP);
	}
}


Drag---长按控件进行拖动_第2张图片



你可能感兴趣的:(Android)