Android View实现滑动的方式

这篇具有很好参考价值的文章主要介绍了Android View实现滑动的方式。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

简述

实现View的滑动有三种方式

  1. 通过View本身提供的scrollTo/scrollBy方法实现滑动
  2. 通过动画给View施加平移效果来实现滑动
  3. 通过改变View LayoutParams使得View重新布局从而实现滑动

使用scrollTo/scrollBy

scrollTo:通过传递的参数实现绝对滑动
scrollBy:通过传递的参数实现相对滑动

scrollTo和scrollBy只能改变View内容的位置,而不能改变View所在布局中的位置。


    /**
     * The offset, in pixels, by which the content of this view is scrolled
     * horizontally.
     * Please use {@link View#getScrollX()} and {@link View#setScrollX(int)} instead of
     * accessing these directly.
     * {@hide}
     */
    @ViewDebug.ExportedProperty(category = "scrolling")
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    protected int mScrollX;
    /**
     * The offset, in pixels, by which the content of this view is scrolled
     * vertically.
     * Please use {@link View#getScrollY()} and {@link View#setScrollY(int)} instead of
     * accessing these directly.
     * {@hide}
     */
    @ViewDebug.ExportedProperty(category = "scrolling")
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    protected int mScrollY;
   
  /**
     * Set the scrolled position of your view. This will cause a call to
     * {@link #onScrollChanged(int, int, int, int)} and the view will be
     * invalidated.
     * @param x the x position to scroll to
     * @param y the y position to scroll to
     */
    public void scrollTo(int x, int y) {
        if (mScrollX != x || mScrollY != y) {
            int oldX = mScrollX;
            int oldY = mScrollY;
            mScrollX = x;
            mScrollY = y;
            invalidateParentCaches();
            onScrollChanged(mScrollX, mScrollY, oldX, oldY);
            if (!awakenScrollBars()) {
                postInvalidateOnAnimation();
            }
        }
    }

    /**
     * Move the scrolled position of your view. This will cause a call to
     * {@link #onScrollChanged(int, int, int, int)} and the view will be
     * invalidated.
     * @param x the amount of pixels to scroll by horizontally
     * @param y the amount of pixels to scroll by vertically
     */
    public void scrollBy(int x, int y) {
        scrollTo(mScrollX + x, mScrollY + y);
    }

    /**
     * Return the scrolled left position of this view. This is the left edge of
     * the displayed part of your view. You do not need to draw any pixels
     * farther left, since those are outside of the frame of your view on
     * screen.
     *
     * @return The left edge of the displayed part of your view, in pixels.
     */
    @InspectableProperty
    public final int getScrollX() {
        return mScrollX;
    }

    /**
     * Return the scrolled top position of this view. This is the top edge of
     * the displayed part of your view. You do not need to draw any pixels above
     * it, since those are outside of the frame of your view on screen.
     *
     * @return The top edge of the displayed part of your view, in pixels.
     */
    @InspectableProperty
    public final int getScrollY() {
        return mScrollY;
    }

从源码上看,scrollBy通过调用scrollTo方法,来实现了基于当前位置的相对滑动。
通过getScrollX、 getScrollY分别获取到mScrollX、mScrollY。
mScrollX、mScrollY单位为像素

使用动画

使用动画来移动View,主要操作View的translationX和translationY属性,可以采用属性动画或者平移动画。
属性动画需要Android3.0以上的版本。
动画并不能真正改变View的位置信息(四个顶点和宽高)。因此在一个View移动到新位置后,单击新位置无法触发onClick事件(平移)。
解决方法:两种方法

  1. 使用属性动画代替平移动画;
  2. 可以在新位置预先创建一个和目标View一模一样的View,他们不但外观一样连onClick事件也一样,当目标View完成平移动画后,就把目标View 设置GONE(textView.setVisibility(View.GONE) ),同时预先设置的View显示出来。

android 动画

平移动画

文件名:translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:zAdjustment="normal">

    <translate
        android:duration="100"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXDelta="100"
        android:toYDelta="100" />

</set>

fillAfter:作用View动画执行完是否恢复到动画前的状态。
true:View会停留在动画执行完的状态。
false: VIew会恢复到动画前的状态。

代码调用

 AnimationSet animationSet2=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.translate);
 textView.startAnimation(animationSet2);

属性动画

      ObjectAnimator.ofFloat(textView, "translationX", 0, 100).setDuration(10000).start();

改变布局参数 LayoutParams

    ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
    layoutParams.width+=100;
    layoutParams.height=100;
    textView.requestLayout();
    //或者 textView.setLayoutParams(layoutParams);

通过改变LayoutParams 的方式区实现View的滑动同样是一种很灵活的方法。不过需要根据实际情况做不同的处理。

总结

scrollTo/scrollBy: 操作简单,适合对View内容的滑动

是View提供的原生方法,其作用是专门用于View的滑动,比较方便地实现滑动效果并不影响内部元素的单击事件。只能滑动View的内容,并不能滑动View本身。

动画:操作简单,主要适用于没有交互的View和实现复杂的动画。

通过动画来实现View的滑动。优点:就是复杂的动画效果必须通过动画才能实现。使用平移动画的话需要注意的是View的单击事件(fillAfter 为true的情况)。

改变布局参数:操作稍微复杂,适用于有交互的View

主要适用有交互性的View.文章来源地址https://www.toymoban.com/news/detail-602907.html

到了这里,关于Android View实现滑动的方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Android View动画之LayoutAnimation的使用

    接前篇 Android View动画整理 ,本篇介绍 LayoutAnimation 的使用。 参考《安卓开发艺术探索》。 View 动画作用于 View 。 LayoutAnimation 则作用于 ViewGroup , 为 ViewGoup 指定一个动画,ViewGoup 的子 View 出场时就具体动画效果。 简言之,LayoutAnimation 是为 ViewGroup 的子View指定出场动画。 开

    2024年02月11日
    浏览(30)
  • [Android]自定义RecyclerView中View的动画

    官方有一个默认Item动画类DafaultItemAnimator,其中 DefaultItemAnimator 继承了SimpleItemAnimator 继承了 RecyclerView.ItemAnimator SimpleItemAnimator 它是一个包装类,用来判断当前的ViewHolder到底是执行移动、移除、添加或者改变等行为。 DefaultItemAnimator 是执行具体动画类,它负责将viewHolder初始化

    2024年02月11日
    浏览(51)
  • Android进阶 View事件体系(三):典型的滑动冲突情况和解决策略

    本篇文章为总结View事件体系的第三篇文章,前两篇文章的在这里: Android进阶 View事件体系(一):概要介绍和实现View的滑动 Android进阶 View事件体系(二):从源码解析View的事件分发 本篇文章主要是介绍两种基本的滑动冲突情况和对应的解决策略,内容有: 基本的滑动冲突

    2024年02月10日
    浏览(26)
  • android:RecyclerView交互动画(上下拖动,左右滑动删除)

    @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { //监听侧滑;1.删除数据,2.调用adapter.notifyItemRemoved(position) mMoveCallback.onItemRemove(viewHolder.getAdapterPosition()); } //改变选中的Item @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { //判断状态 if

    2024年04月12日
    浏览(29)
  • Android View的动画效果,上移展示和下移隐藏

    原文:Android View的动画效果,上移展示和下移隐藏-Stars-One的杂货小窝 项目里的一个小需求(实际是要和手势操作一起,上滑和下拉触发此动画效果),记录一下 PS: 本篇先记录下动画效果,下篇再将如何监听滑动手势 实际通过View的translationY的属性来实现 PS: withEndAction 方法实际也是设

    2024年03月22日
    浏览(35)
  • Android 实现滑动数字选择器

    Android 滑动数字选择器是一种用户界面控件,它允许用户从一系列数字中选择一个值。用户可以通过滑动手势或点击手势来选择数字。以下是一些关于 Android 滑动数字选择器的信息和链接: Android NumberPicker:这是 Android 框架提供的原生数字选择器控件。它可以通过 XML 或代码创

    2024年02月07日
    浏览(25)
  • Android自定义View之游戏摇杆键盘实现(一),快手android面试经验

    public class RemoteViewBg { private Bitmap bitmapBg; public RemoteViewBg(Bitmap bitmap) { bitmapBg = bitmap; } //背景的绘图函数 public void draw(Canvas canvas, Paint paint, Rect src0 ,Rect dst0 ) { canvas.drawBitmap(bitmapBg, src0, dst0, paint); } } 重写系统的触摸时间,判断触摸点在背景范围内还是背景范围外 @Override public b

    2024年04月12日
    浏览(37)
  • Android Studio实现滑动图片验证码

    源代码链接 效果: MainActivity SlideImageView activity_main.xml

    2024年02月13日
    浏览(32)
  • Android——禁止ViewPager的左右滑动功能实现

    Android——禁止ViewPager的左右滑动功能实现 在Android开发中,ViewPager是一种常用的滑动控件,用于实现页面的左右切换效果。然而,在某些场景中,我们可能需要禁止ViewPager的左右滑动功能,只允许通过其他方式进行页面切换。本文将介绍如何在Android中实现禁止ViewPager左右滑动

    2024年02月06日
    浏览(34)
  • Android 实现单指滑动、双指缩放照片

    最近接到一个查看大图的需求,现在图片展示还不够大,要求还要能缩小能放大还能保存照片。直接开始Google实现方式。 根据查询到的结果分为两种,一个是使用手势监听来实现,第二种监听触摸事件来实现 手势监听-- ScaleGestureDetector Google提供的手势监听类 触摸事件–OnT

    2024年02月11日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包