当前位置:网站首页>可以拖拽的ViewGroup,仿微信拖拽缩放关闭
可以拖拽的ViewGroup,仿微信拖拽缩放关闭
2022-08-02 14:07:00 【昵称审核中】
此处利用了ViewDragHelper类
内部设置了一个监听,松手进行关闭即可
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
public class DragViewGrouop extends FrameLayout {
private ViewDragHelper dragHelper;
private int mOldLeft;
private int mOldTop;
private int mHeight;
private float mScaleValue;
private float mMinRate = 0.4f;
public DragViewGrouop(@NonNull Context context) {
this(context, null);
}
public DragViewGrouop(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public DragViewGrouop(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr, 0);
init();
}
private void init() {
dragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
@Override
public boolean tryCaptureView(View child, int pointerId) {
return true;
}
@Override
public void onViewReleased(View releasedChild, float xvel, float yvel) {
if (dragListener != null && mScaleValue < mMinRate) {
dragListener.onViewReleased();
} else {
dragHelper.settleCapturedViewAt(mOldLeft, mOldTop);
invalidate();
ViewCompat.animate(releasedChild).scaleX(1).setDuration(0);
}
}
@Override
public void onViewCaptured(View capturedChild, int activePointerId) {
mOldLeft = capturedChild.getLeft();
mOldTop = capturedChild.getTop();
}
@Override
public int clampViewPositionVertical(View child, int top, int dy) {
mScaleValue = 1.00f - Math.abs((top - mOldTop) * 1.0f / (mHeight - mOldTop));
ViewCompat.animate(child).scaleX(mScaleValue).setDuration(0);
return top;
}
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {
return left;
}
});
}
@Override
public void computeScroll() {
super.computeScroll();
if (dragHelper != null && dragHelper.continueSettling(true)) {
invalidate();
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mHeight = h;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (dragHelper != null) {
return dragHelper.shouldInterceptTouchEvent(ev);
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (dragHelper != null) {
dragHelper.processTouchEvent(event);
}
return true;
}
private DragListener dragListener;
public void setOnDragListener(DragListener dragListener) {
this.dragListener = dragListener;
}
public interface DragListener {
void onViewReleased();
}
边栏推荐
猜你喜欢
随机推荐
浏览器报错数字代表的大概意思
St. Regis Takeaway Notes - Lecture 05 Getting Started with Redis
The Handler you really understand?
宏定义问题记录day2
spark on yarn
二级指针,数组指针,指针数组和函数指针
一文带你快速掌握Kotlin核心技能
Flink实现Exactly Once
2022最新交规记忆重点
C语言初级—从键盘接收一个整形并逆序输出
VS Code无法安装插件之Unable to install because, the extension '' compatible with current version
liunx下mysql遇到的简单问题
MySQL 8.0 新特性
C语言一维数组练习——将m个元素移动到数组尾部
1.RecyclerView是什么
C语言一级指针(补)
十分钟带你入门Nodejs
MySQL知识总结 (二) 存储引擎
UIWindow的makeKeyAndVisible不调用rootviewController 的viewDidLoad的问题
MySQL知识总结 (六) MySQL调优