Android View滑動(dòng)的七種方式總結(jié)

前言

Android中經(jīng)常看到一些炫酷的效果,這些效果很多伴隨著View的滑動(dòng)。我們想要做出這樣的效果,掌握View的滑動(dòng)方式必不可少,本篇總結(jié)了View的滑動(dòng)的7中方式并對(duì)其進(jìn)行分析。

  • 使用scrollTo/scrollBy
  • 使用scroller
  • 使用動(dòng)畫,其中包括View動(dòng)畫和屬性動(dòng)畫
  • 改變布局參數(shù)
  • 使用layout
  • 使用offsetTopAndBottom/offsetLeftAndRight
  • 使用ViewDragHelper

假如我們要把一個(gè)ImageView從向右移動(dòng)300px

布局文件內(nèi)容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contain"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.linda.test.MainActivity">

    <ImageView
        android:id="@+id/image"
        android:src="@mipmap/ic_launcher_round"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Activity內(nèi)容:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imageView = (ImageView) findViewById(R.id.image);
        LinearLayout linearLayout = (LinearLayout) findViewById(R.id.contain);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //這里用不同的方式去實(shí)現(xiàn)。
            }
        });
    }
}

以上代碼很簡(jiǎn)單,下面我們用上面提到的七種方式去實(shí)現(xiàn)。

1. 使用scrollTo/scrollBy

linearLayout.scrollTo(-300,0);
linearLayout.scrollBy(-150,0);
linearLayout.scrollBy(-150,0);

如上,很簡(jiǎn)單有么有,

2.使用Scroller
關(guān)于Scroller的使用會(huì)單獨(dú)寫一篇去分析。

3.使用動(dòng)畫,其中包括View動(dòng)畫和屬性動(dòng)畫

  • View動(dòng)畫:
//這里用不同的方式去實(shí)現(xiàn)。
TranslateAnimation animation = new TranslateAnimation(0, 300, 0, 0);
animation.setDuration(1500);//設(shè)置動(dòng)畫時(shí)長(zhǎng)
animation.setFillAfter(true);//讓ImageView停留在動(dòng)畫結(jié)束的位置
imageView.startAnimation(animation);

注意:View動(dòng)畫是對(duì)View的影像進(jìn)行操作,并不能改變View真實(shí)的位置。

  • 屬性動(dòng)畫:
ObjectAnimator.ofFloat(imageView,"translationX",0,300).setDuration(1500).start();

3.0之前實(shí)現(xiàn)需要借助開源動(dòng)畫庫(kù)nineoldandroids,現(xiàn)在基本上兼容到3.0以后了吧~~,因此3.0之前的情況就不說(shuō)了

屬性動(dòng)畫是直接對(duì)View的屬性進(jìn)行操作,其改變了View的真實(shí)位置。
第一個(gè)參數(shù)代表我們要操作的目標(biāo)View。
第二個(gè)參數(shù)代表我們要操作的屬性名稱,這里的translationX是指View左上角相對(duì)于父View水平方向的偏移量。
第三個(gè)參數(shù)代表屬性起始值。
第四個(gè)參數(shù)代表屬性的結(jié)束值。

4.改變布局參數(shù)

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) imageView.getLayoutParams();
params.leftMargin += 300;
imageView.setLayoutParams(params);//也可以用imageView.requestLayout();

上述代碼的作用是讓ImageView的marginLeft增加了300px。此種方式會(huì)改變View的位置參數(shù)left、right、top、bottom的值,因?yàn)?code>imageView.setLayoutParams(params);內(nèi)部會(huì)調(diào)用requestLayout方法進(jìn)行重新測(cè)量、布局、繪制。在布局的過(guò)程會(huì)對(duì)mLeft、mTop、mRight、mBottom這四個(gè)變量重新賦值,即left、right、top、bottom的值。

使用layout方法

int mLeft = imageView.getLeft();//獲取ImageView左上角初始橫坐標(biāo)
int mTop = imageView.getTop();//獲取ImageView左上角初始縱坐標(biāo)
int mRight = imageView.getRight();//獲取ImageView右下角初始橫坐標(biāo)
int mBottom = imageView.getBottom();//獲取ImageView右下角初始縱坐標(biāo)
image.layout(mLeft + 300,mTop,mRight + 300,mBottom);//重新布局

使用layout方法會(huì)直接改變View的位置,其內(nèi)部會(huì)把四個(gè)參數(shù)分別賦值給mLeft、mTop、mRight、mBottom四個(gè)變量,四個(gè)頂點(diǎn)的位置也就相應(yīng)改變。我們可以概括成如下偽代碼:

targetView.layout(targetView.getLeft()+offsetX,targetView.getTop()+offsetY,targetView.getRight()+offsetX,targetView.getBottom()+offsetY);

使用offsetTopAndBottom/offsetLeftAndRight

imageView.offsetLeftAndRight(300);  //將imageView沿水平正方向偏移300px
imageView.offsetTopAndBottom(300); //將iamgeView沿豎直正方向偏移300px

此種方式也會(huì)改變View的位置參數(shù)。

使用ViewDragHelper
本篇暫不做介紹了,以后可能會(huì)專門寫一篇關(guān)于ViewDragHelper的博客。

總結(jié)

以上方式均能實(shí)現(xiàn)View的滑動(dòng),具體使用哪種方式,我們需要視情況而定。

  • scrollTo/scrollBy: 操作簡(jiǎn)單,適合對(duì)View內(nèi)容的滑動(dòng)。
  • View動(dòng)畫: 適合沒(méi)有交互的View,方便實(shí)現(xiàn)復(fù)雜的動(dòng)畫效果。
  • 屬性動(dòng)畫:操作簡(jiǎn)單,適用于有交互的情況,方便實(shí)現(xiàn)復(fù)雜的動(dòng)畫效果。
  • 布局參數(shù)、layout、offsetTopAndBottom/offsetLeftAndRight: 真實(shí)改變了View的位置,適合有交互的情況。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容