DraggableView拖拽视图

[复制链接]
来自: MrlLee 分类: Android精品源码 上传时间: 2016-5-10 12:36:48

项目介绍:

介绍:具有转转与缩放倾斜效果的拖拽视图。
运行效果:
使用说明:1:实现 DragController.IDragViewGroup
[Java] 查看源文件 复制代码
package su.levenetc.android.draggableview;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Eugene Levenetc.
 */
public class DragController {

	private View selectedView;
	private PointF downEventPoint = new PointF();
	private DraggableView draggableView;
	private VelocityTracker velocityTracker = VelocityTracker.obtain();
	private T viewGroup;

	public DragController(T viewGroup) {
		this.viewGroup = viewGroup;
	}

	public boolean onTouchEvent(MotionEvent event) {

		final int action = event.getAction();
		final float x = event.getX();
		final float y = event.getY();
		final boolean isInDragSession = draggableView != null;

		if (action == MotionEvent.ACTION_DOWN && !isInDragSession) {

			selectedView = viewGroup.onDownEvent((int) x, (int) y);

			if (selectedView != null) {
				downEventPoint.set(x, y);
				addDraggable();
				return true;
			}

		} else if (isInDragSession && action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
			velocityTracker.clear();
			viewGroup.onDragEnd();
			return true;
		} else if (isInDragSession && action == MotionEvent.ACTION_MOVE) {
			draggableView.onMoveAction(event);
			return true;
		}

		return false;
	}

	private void addDraggable() {

		viewGroup.onDragStart();

		selectedView.buildDrawingCache();
		Bitmap bitmap = Bitmap.createBitmap(selectedView.getDrawingCache());

		float selectedViewX = selectedView.getX();
		float selectedViewY = selectedView.getY();
		downEventPoint.set(downEventPoint.x - selectedViewX, downEventPoint.y - selectedViewY);

		draggableView = viewGroup.createDraggableView(
				bitmap,
				velocityTracker,
				new PointF(selectedViewX, selectedViewY),
				downEventPoint
		);

		viewGroup.getContainerForDraggableView().addView(draggableView);

	}

	public void finishDrag() {
		viewGroup.getContainerForDraggableView().removeView(draggableView);
		draggableView = null;
		selectedView = null;
	}

	public DraggableView getDraggableView() {
		return draggableView;
	}

	public interface IDragViewGroup {

		/**
		 * Should be returned view which can be dragged
		 * or null if there is no such view
		 */
		View onDownEvent(int x, int y);

		/**
		 * @return ViewGroup where draggable view will be added
		 */
		ViewGroup getContainerForDraggableView();

		/**
		 * Calls after draggable view was created.
		 * Some animations could be started.
		 * Also selected view should be hided.
		 */
		void onDragStart();

		/**
		 * Calls after ACTION_UP or ACTION_CANCEL event.
		 * E.g. user finished dragging.
		 * Last position of draggable could be received with {@link IDragViewGroup#onMoveEvent}
		 * 

* !!! * Finally must be called {@link DragController#finishDrag()} * !!! */ void onDragEnd(); void onMoveEvent(float x, float y); DraggableView createDraggableView(Bitmap bitmap, VelocityTracker velocityTracker, PointF selectedViewPoint, PointF downEventPoint ); } }


2:创建 DragController实例子。
[Java] 查看源文件 复制代码
package su.levenetc.android.draggableview;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Eugene Levenetc.
 */
public class DragController {

	private View selectedView;
	private PointF downEventPoint = new PointF();
	private DraggableView draggableView;
	private VelocityTracker velocityTracker = VelocityTracker.obtain();
	private T viewGroup;

	public DragController(T viewGroup) {
		this.viewGroup = viewGroup;
	}

	public boolean onTouchEvent(MotionEvent event) {

		final int action = event.getAction();
		final float x = event.getX();
		final float y = event.getY();
		final boolean isInDragSession = draggableView != null;

		if (action == MotionEvent.ACTION_DOWN && !isInDragSession) {

			selectedView = viewGroup.onDownEvent((int) x, (int) y);

			if (selectedView != null) {
				downEventPoint.set(x, y);
				addDraggable();
				return true;
			}

		} else if (isInDragSession && action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
			velocityTracker.clear();
			viewGroup.onDragEnd();
			return true;
		} else if (isInDragSession && action == MotionEvent.ACTION_MOVE) {
			draggableView.onMoveAction(event);
			return true;
		}

		return false;
	}

	private void addDraggable() {

		viewGroup.onDragStart();

		selectedView.buildDrawingCache();
		Bitmap bitmap = Bitmap.createBitmap(selectedView.getDrawingCache());

		float selectedViewX = selectedView.getX();
		float selectedViewY = selectedView.getY();
		downEventPoint.set(downEventPoint.x - selectedViewX, downEventPoint.y - selectedViewY);

		draggableView = viewGroup.createDraggableView(
				bitmap,
				velocityTracker,
				new PointF(selectedViewX, selectedViewY),
				downEventPoint
		);

		viewGroup.getContainerForDraggableView().addView(draggableView);

	}

	public void finishDrag() {
		viewGroup.getContainerForDraggableView().removeView(draggableView);
		draggableView = null;
		selectedView = null;
	}

	public DraggableView getDraggableView() {
		return draggableView;
	}

	public interface IDragViewGroup {

		/**
		 * Should be returned view which can be dragged
		 * or null if there is no such view
		 */
		View onDownEvent(int x, int y);

		/**
		 * @return ViewGroup where draggable view will be added
		 */
		ViewGroup getContainerForDraggableView();

		/**
		 * Calls after draggable view was created.
		 * Some animations could be started.
		 * Also selected view should be hided.
		 */
		void onDragStart();

		/**
		 * Calls after ACTION_UP or ACTION_CANCEL event.
		 * E.g. user finished dragging.
		 * Last position of draggable could be received with {@link IDragViewGroup#onMoveEvent}
		 * 

* !!! * Finally must be called {@link DragController#finishDrag()} * !!! */ void onDragEnd(); void onMoveEvent(float x, float y); DraggableView createDraggableView(Bitmap bitmap, VelocityTracker velocityTracker, PointF selectedViewPoint, PointF downEventPoint ); } }


3:重写ViewGroup的onTouchEvent并调用DragController#onTouchEvent:
[Java] 查看源文件 复制代码
@Override public boolean onTouchEvent(MotionEvent event) {
    return dragController.onTouchEvent(event);
}

动画调整
SkewViewRotateView 中包含了许多改变rotation,倾斜度与缩放效果的值。   
下载

[XML] 查看源文件 复制代码
repositories {
    maven { url "https://jitpack.io" }
}
//...
dependencies {
    //...
    compile 'com.github.elevenetc:draggableview:v1.0.1'
}



相关源码推荐:

我来说两句
所有评论(3)
A5下载 2016-5-10 13:54:32
强烈支持楼主ing……
回复
bennyq 2016-5-10 14:40:16
试下看看
回复
lusanren110 2016-5-13 09:12:51
感谢分享,安卓巴士有你更精彩:)
回复
雪洗中关村 2016-7-11 11:26:02
精华内容,楼主V5!
回复
kingxiao 2016-9-5 14:34:21
我只是路过打酱油的。
回复
apkbus热心网友 2017-4-7 09:57:31
楼主用心了,内容非常精彩。
回复
apkbus热心网友 2018-12-5 07:12:20
楼主威武啊,安卓巴士有你更给力!
回复
提取码:  下载次数:3 状态:已购或VIP 售价:10(原价:10)金钱 下载权限:初级码农 
1021 0 3
代码贡献英雄榜
用户名 下载数
联系我们
首页/微信公众账号投稿
帖子代码编辑/版权问题
QQ:435399051,1294855032
如何获得代码达人称号?
如何成为签约作者?
领先的中文移动开发者社区
18620764416
7*24全天服务
意见反馈:1294855032@qq.com

扫一扫关注我们

Powered by Discuz! X3.2© 2001-2019 Comsenz Inc.( 粤ICP备15117877号 )