android+仿+左右拖动解锁动画,Android 类似向右滑动解锁控件

预览图:

299eae09dcc964fffbbe9522d21a8703.png

滑动后执行操作并且隐去,滑动距离不够则反弹回去。

这种滑动操作的控件基本都会使用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;

例:

android:id="@+id/get_order_drag_helper"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_alignParentBottom="true">

android:id="@+id/get_order_ll_slide"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/app_blue"

android:orientation="horizontal">

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"/>

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"/>

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"/>

Java代码中设置控件滑动事件:

SlideRightViewDragHelper dragHelper;

dragHelper = (SlideRightViewDragHelper) findViewById(R.id.xxxxx);

dragHelper.setOnReleasedListener(new SlideRightViewDragHelper.OnReleasedListener() {

@Override

public void onReleased() {

//TODO

}

});

至此此控件就完成了。

15932a3f82af3e213542e942ac8a7fec.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值