张圣华的博客

菜鸟修炼中

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

运行效果图:




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

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

//自定义的View类型的子类,作为被拖动View
public class Dragging <strong>extends View</strong> {
	//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 <strong>extends View</strong> {

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

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

}


修改activity_main.xml文件,如下:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="拖动效果的展示" />
<!-- 放置拖动对象 -->
    <com.bzu.test064view.Dragging 
        android:id="@+id/ViewD"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:background="@color/bule"/>
<!-- 定义空白区域 -->
	<TextView 
	    android:layout_width="50dip"
	    android:layout_height="50dip"/>
<!-- 放置安放拖动对象 -->
    <com.bzu.test064view.Pass 
        android:id="@+id/ViewP"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:background="@color/black"/>
    
</LinearLayout>



修改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);
	}
}






阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zsh157621866651/article/details/51565503
文章标签: drag
个人分类: Android开发
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

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

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭