Android 类似向右滑动解锁控件
来源:互联网 发布:美国商科 知乎 编辑:程序博客网 时间:2024/05/19 05:40
预览图:
滑动后执行操作并且隐去,滑动距离不够则反弹回去。
这种滑动操作的控件基本都会使用ViewDragHelper,而这个控件的代码也十分简单:
package com.yanbang.laiba.widget;import android.content.Context;import android.graphics.Point;import android.support.v4.widget.ViewDragHelper;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.LinearLayout;/** * 向右滑动控件 * Created by Jing on 2016/2/16. */public class SlideRightViewDragHelper extends LinearLayout { private ViewDragHelper viewDragHelper; private View child; private Point childPosition = new Point(); private Point childEndPosition = new Point(); private OnReleasedListener onReleasedListener; private int oldX; public SlideRightViewDragHelper(Context context, AttributeSet attrs) { super(context, attrs); //新建viewDragHelper ,viewGroup, 灵敏度,回调(子view的移动) viewDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() { @Override public boolean tryCaptureView(View child, int pointerId) { return true; } @Override public int clampViewPositionHorizontal(View child, int left, int dx) { oldX = left; return Math.max(0, left); } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { if(oldX > 200){ viewDragHelper.settleCapturedViewAt(childEndPosition.x, childEndPosition.y); invalidate(); //必须刷新,因为其内部使用的是mScroller.startScroll,所以别忘了需要invalidate()以及结合computeScroll方法一起。 if(onReleasedListener != null) onReleasedListener.onReleased(); }else{ viewDragHelper.settleCapturedViewAt(childPosition.x, childPosition.y); //反弹 invalidate(); } super.onViewReleased(releasedChild, xvel, yvel); } }); } @Override protected void onFinishInflate() { super.onFinishInflate(); child = getChildAt(0); } @Override //用viewDragHelper拦截-true public boolean onInterceptTouchEvent(MotionEvent ev) { return viewDragHelper.shouldInterceptTouchEvent(ev); } @Override //viewDragHelper拦截事件 public boolean onTouchEvent(MotionEvent event) { viewDragHelper.processTouchEvent(event); return true; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); //定位一开始的坐标 childPosition.x = child.getLeft(); childPosition.y = child.getTop(); //滑动成功后定位坐标 childEndPosition.x = child.getRight(); childEndPosition.y = child.getTop(); } @Override public void computeScroll() { super.computeScroll(); if (viewDragHelper.continueSettling(true)) { invalidate(); } } public void setOnReleasedListener(OnReleasedListener onReleasedListener){ this.onReleasedListener = onReleasedListener; } public interface OnReleasedListener{ void onReleased(); }}
此控件可直接在xml中应用,滑动区域包含在其中,作为childView;
例:
<com.zyj.test.widget.SlideRightViewDragHelper android:id="@+id/get_order_drag_helper" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true"> <LinearLayout android:id="@+id/get_order_ll_slide" android:layout_width="match_parent" android:layout_height="50dp" android:background="@color/app_blue" android:orientation="horizontal"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@mipmap/get_order_slide_right" android:layout_marginLeft="20dp"/> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_gravity="center" android:textColor="@color/white" android:text="滑动" android:textStyle="bold" android:layout_marginLeft="10dp"/> <TextView android:id="@+id/get_order_tv_left_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="@color/white" android:text="确认(30s)" android:textStyle="bold" android:layout_marginRight="15dp"/> </LinearLayout> </com.zyj.test.widget.SlideRightViewDragHelper>
Java代码中设置控件滑动事件:
SlideRightViewDragHelper dragHelper;dragHelper = (SlideRightViewDragHelper) findViewById(R.id.xxxxx);dragHelper.setOnReleasedListener(new SlideRightViewDragHelper.OnReleasedListener() { @Override public void onReleased() { //TODO }});
至此此控件就完成了。
0 0
- Android 类似向右滑动解锁控件
- Android滑动解锁控件
- 自定义View向右滑动解锁
- android仿ipone滑动解锁控件
- Android Jelly Bean滑动解锁控件实现
- Android自定义控件之滑动解锁
- Android自定义控件之滑动解锁
- android图片向右滑动
- 类似滑动苹果解锁Text
- 类似滑动苹果解锁Text
- Android 向右滑动关闭页面
- Android Activity向右滑动返回
- Android向右滑动关闭界面
- android 向右滑动关闭Activity
- CAGradientLayer实现向右滑动解锁的动画效果
- Android自定义控件之滑动解锁九宫格
- android滑动解锁
- Android 滑动解锁
- kidd风的IOS日志之常用小功能小结
- ios tabbar 文字位置
- oracle存储过程编写
- 活得更像一个人,我的十六年学习之路——北漂18年(44)
- 友元函数和友元类
- Android 类似向右滑动解锁控件
- 网络编程4---poll与epoll的区别
- ios开发之app内启动用户评价
- oracle游标的使用
- ByteBuffer用法小结
- iOS 开发 -- Swift (八) 闭包
- iOS在图片中添加水印
- Android开发中关于小米2s手机的调试问题
- salesforce学习资源