android 自定义View(滑动解锁)

1,实现效果 

 

 

【1】scrollTo & scrrllBy的特点

  •    实际上上面2个api滚动是的view绘制的内容,view本身并没有动

  •    scrollTo当x传入正数往做移动.传入负数往右移动. (偏移量x = 0-i)

  •    scrollBy 在上一次移动的基础上在移动X

 

2,实现逻辑

【1】创建一个类,继承view ,重写onMeasure 和 onDraw  

public class LockView extends View {



    public LockView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

    

    //在这个方法里面测量当前view的大小

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    }

    //往当前的view上画内容

    @Override

    protected void onDraw(Canvas canvas) {

        //[1]由于背景我已经在布局中加上 所以只需要往当前的view上画 滑块就可以了

     

    }

【2】MainActivity 的布局中声明使用自定义view

  •   android:background="@drawable/lockviewbg" 设置了背景

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <com.xiaoshuai.lockview.LockView

        android:id="@+id/lockView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:background="@drawable/lockviewbg"

        android:layout_centerHorizontal="true"

        android:layout_alignParentBottom="true"

        />

</RelativeLayout>

【3】布局的绘制

  • LockView构造方法,BitmapFactory获取图片资源图片

        //[1]获取背景图片和 滑动块的图

        lockViewBg = BitmapFactory.decodeResource(getResources(), R.drawable.lockviewbg);

        slideBg = BitmapFactory.decodeResource(getResources(), R.drawable.switch_button);

  • 在onMeasure进行测量背景图片的大小

    //在这个方法里面测量当前view的大小

    @Override

    protected void onMeasure(int widthMeasu
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 中,我们可以使用 `GestureDetector` 类来监听手势事件,从而实现左右滑动的监听。下面是一个简单的示例代码: ```java import android.content.Context; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; public class CustomSwipeListener implements View.OnTouchListener { private GestureDetector gestureDetector; public CustomSwipeListener(Context context) { gestureDetector = new GestureDetector(context, new SwipeGestureListener()); } @Override public boolean onTouch(View view, MotionEvent motionEvent) { return gestureDetector.onTouchEvent(motionEvent); } private class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener { private static final int SWIPE_THRESHOLD = 100; private static final int SWIPE_VELOCITY_THRESHOLD = 100; @Override public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) { boolean result = false; float diffX = event2.getX() - event1.getX(); float diffY = event2.getY() - event1.getY(); if (Math.abs(diffX) > Math.abs(diffY)) { if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (diffX > 0) { onSwipeRight(); } else { onSwipeLeft(); } result = true; } } else { if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { onSwipeDown(); } else { onSwipeUp(); } result = true; } } return result; } } public void onSwipeRight() { // 处理向右滑动的逻辑 } public void onSwipeLeft() { // 处理向左滑动的逻辑 } public void onSwipeUp() { // 处理向上滑动的逻辑 } public void onSwipeDown() { // 处理向下滑动的逻辑 } } ``` 你可以将 `CustomSwipeListener` 应用到任何 `View` 上,例如一个 `LinearLayout` 或者一个 `ImageView`,然后在 `onSwipeRight()` 和 `onSwipeLeft()` 方法中处理向右滑动和向左滑动的逻辑。同样地,你可以在 `onSwipeUp()` 和 `onSwipeDown()` 方法中处理向上滑动和向下滑动的逻辑。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值