国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android View實現(xiàn)滑動的方式

這篇具有很好參考價值的文章主要介紹了Android View實現(xiàn)滑動的方式。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

簡述

實現(xiàn)View的滑動有三種方式

  1. 通過View本身提供的scrollTo/scrollBy方法實現(xiàn)滑動
  2. 通過動畫給View施加平移效果來實現(xiàn)滑動
  3. 通過改變View LayoutParams使得View重新布局從而實現(xiàn)滑動

使用scrollTo/scrollBy

scrollTo:通過傳遞的參數(shù)實現(xiàn)絕對滑動
scrollBy:通過傳遞的參數(shù)實現(xiàn)相對滑動

scrollTo和scrollBy只能改變View內(nèi)容的位置,而不能改變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通過調(diào)用scrollTo方法,來實現(xiàn)了基于當前位置的相對滑動。
通過getScrollX、 getScrollY分別獲取到mScrollX、mScrollY。
mScrollX、mScrollY單位為像素

使用動畫

使用動畫來移動View,主要操作View的translationX和translationY屬性,可以采用屬性動畫或者平移動畫。
屬性動畫需要Android3.0以上的版本。
動畫并不能真正改變View的位置信息(四個頂點和寬高)。因此在一個View移動到新位置后,單擊新位置無法觸發(fā)onClick事件(平移)。
解決方法:兩種方法

  1. 使用屬性動畫代替平移動畫;
  2. 可以在新位置預(yù)先創(chuàng)建一個和目標View一模一樣的View,他們不但外觀一樣連onClick事件也一樣,當目標View完成平移動畫后,就把目標View 設(shè)置GONE(textView.setVisibility(View.GONE) ),同時預(yù)先設(shè)置的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動畫執(zhí)行完是否恢復(fù)到動畫前的狀態(tài)。
true:View會停留在動畫執(zhí)行完的狀態(tài)。
false: VIew會恢復(fù)到動畫前的狀態(tài)。

代碼調(diào)用

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

屬性動畫

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

改變布局參數(shù) LayoutParams

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

通過改變LayoutParams 的方式區(qū)實現(xiàn)View的滑動同樣是一種很靈活的方法。不過需要根據(jù)實際情況做不同的處理。

總結(jié)

scrollTo/scrollBy: 操作簡單,適合對View內(nèi)容的滑動

是View提供的原生方法,其作用是專門用于View的滑動,比較方便地實現(xiàn)滑動效果并不影響內(nèi)部元素的單擊事件。只能滑動View的內(nèi)容,并不能滑動View本身。

動畫:操作簡單,主要適用于沒有交互的View和實現(xiàn)復(fù)雜的動畫。

通過動畫來實現(xiàn)View的滑動。優(yōu)點:就是復(fù)雜的動畫效果必須通過動畫才能實現(xiàn)。使用平移動畫的話需要注意的是View的單擊事件(fillAfter 為true的情況)。

改變布局參數(shù):操作稍微復(fù)雜,適用于有交互的View

主要適用有交互性的View.文章來源地址http://www.zghlxwxcb.cn/news/detail-602907.html

到了這里,關(guān)于Android View實現(xiàn)滑動的方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Android View動畫之LayoutAnimation的使用

    Android View動畫之LayoutAnimation的使用

    接前篇 Android View動畫整理 ,本篇介紹 LayoutAnimation 的使用。 參考《安卓開發(fā)藝術(shù)探索》。 View 動畫作用于 View 。 LayoutAnimation 則作用于 ViewGroup , 為 ViewGoup 指定一個動畫,ViewGoup 的子 View 出場時就具體動畫效果。 簡言之,LayoutAnimation 是為 ViewGroup 的子View指定出場動畫。 開

    2024年02月11日
    瀏覽(21)
  • [Android]自定義RecyclerView中View的動畫

    官方有一個默認Item動畫類DafaultItemAnimator,其中 DefaultItemAnimator 繼承了SimpleItemAnimator 繼承了 RecyclerView.ItemAnimator SimpleItemAnimator 它是一個包裝類,用來判斷當前的ViewHolder到底是執(zhí)行移動、移除、添加或者改變等行為。 DefaultItemAnimator 是執(zhí)行具體動畫類,它負責將viewHolder初始化

    2024年02月11日
    瀏覽(20)
  • Android進階 View事件體系(三):典型的滑動沖突情況和解決策略

    Android進階 View事件體系(三):典型的滑動沖突情況和解決策略

    本篇文章為總結(jié)View事件體系的第三篇文章,前兩篇文章的在這里: Android進階 View事件體系(一):概要介紹和實現(xiàn)View的滑動 Android進階 View事件體系(二):從源碼解析View的事件分發(fā) 本篇文章主要是介紹兩種基本的滑動沖突情況和對應(yīng)的解決策略,內(nèi)容有: 基本的滑動沖突

    2024年02月10日
    瀏覽(25)
  • android:RecyclerView交互動畫(上下拖動,左右滑動刪除)

    android:RecyclerView交互動畫(上下拖動,左右滑動刪除)

    @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { //監(jiān)聽側(cè)滑;1.刪除數(shù)據(jù),2.調(diào)用adapter.notifyItemRemoved(position) mMoveCallback.onItemRemove(viewHolder.getAdapterPosition()); } //改變選中的Item @Override public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) { //判斷狀態(tài) if

    2024年04月12日
    瀏覽(89)
  • Android View的動畫效果,上移展示和下移隱藏

    Android View的動畫效果,上移展示和下移隱藏

    原文:Android View的動畫效果,上移展示和下移隱藏-Stars-One的雜貨小窩 項目里的一個小需求(實際是要和手勢操作一起,上滑和下拉觸發(fā)此動畫效果),記錄一下 PS: 本篇先記錄下動畫效果,下篇再將如何監(jiān)聽滑動手勢 實際通過View的translationY的屬性來實現(xiàn) PS: withEndAction 方法實際也是設(shè)

    2024年03月22日
    瀏覽(32)
  • Android 實現(xiàn)滑動數(shù)字選擇器

    Android 實現(xiàn)滑動數(shù)字選擇器

    Android 滑動數(shù)字選擇器是一種用戶界面控件,它允許用戶從一系列數(shù)字中選擇一個值。用戶可以通過滑動手勢或點擊手勢來選擇數(shù)字。以下是一些關(guān)于 Android 滑動數(shù)字選擇器的信息和鏈接: Android NumberPicker:這是 Android 框架提供的原生數(shù)字選擇器控件。它可以通過 XML 或代碼創(chuàng)

    2024年02月07日
    瀏覽(19)
  • Android自定義View之游戲搖桿鍵盤實現(xiàn)(一),快手android面試經(jīng)驗

    Android自定義View之游戲搖桿鍵盤實現(xiàn)(一),快手android面試經(jīng)驗

    public class RemoteViewBg { private Bitmap bitmapBg; public RemoteViewBg(Bitmap bitmap) { bitmapBg = bitmap; } //背景的繪圖函數(shù) public void draw(Canvas canvas, Paint paint, Rect src0 ,Rect dst0 ) { canvas.drawBitmap(bitmapBg, src0, dst0, paint); } } 重寫系統(tǒng)的觸摸時間,判斷觸摸點在背景范圍內(nèi)還是背景范圍外 @Override public b

    2024年04月12日
    瀏覽(17)
  • Android Studio實現(xiàn)滑動圖片驗證碼

    Android Studio實現(xiàn)滑動圖片驗證碼

    源代碼鏈接 效果: MainActivity SlideImageView activity_main.xml

    2024年02月13日
    瀏覽(29)
  • Android——禁止ViewPager的左右滑動功能實現(xiàn)

    Android——禁止ViewPager的左右滑動功能實現(xiàn) 在Android開發(fā)中,ViewPager是一種常用的滑動控件,用于實現(xiàn)頁面的左右切換效果。然而,在某些場景中,我們可能需要禁止ViewPager的左右滑動功能,只允許通過其他方式進行頁面切換。本文將介紹如何在Android中實現(xiàn)禁止ViewPager左右滑動

    2024年02月06日
    瀏覽(24)
  • Android 實現(xiàn)單指滑動、雙指縮放照片

    Android 實現(xiàn)單指滑動、雙指縮放照片

    最近接到一個查看大圖的需求,現(xiàn)在圖片展示還不夠大,要求還要能縮小能放大還能保存照片。直接開始Google實現(xiàn)方式。 根據(jù)查詢到的結(jié)果分為兩種,一個是使用手勢監(jiān)聽來實現(xiàn),第二種監(jiān)聽觸摸事件來實現(xiàn) 手勢監(jiān)聽-- ScaleGestureDetector Google提供的手勢監(jiān)聽類 觸摸事件–OnT

    2024年02月11日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包