簡述
實現(xiàn)View的滑動有三種方式
- 通過View本身提供的scrollTo/scrollBy方法實現(xiàn)滑動
- 通過動畫給View施加平移效果來實現(xiàn)滑動
- 通過改變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事件(平移)。
解決方法:兩種方法
- 使用屬性動畫代替平移動畫;
- 可以在新位置預(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的情況)。文章來源:http://www.zghlxwxcb.cn/news/detail-602907.html
改變布局參數(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)!