View(2) 的滑動(dòng)

參考鏈接 向劉老師學(xué)習(xí)英染,所以摘抄了老師的筆記毕泌,只想作為自己的技術(shù)積累蛙奖。

簡介

不管是哪種滑動(dòng)的方式基本思想都是類似的:當(dāng)觸摸事件傳到View時(shí)吹艇,系統(tǒng)記下觸摸點(diǎn)的坐標(biāo),手指移動(dòng)時(shí)系統(tǒng)記下移動(dòng)后的觸摸的坐標(biāo)并算出偏移量驮履,并通過偏移量來修改View的坐標(biāo)矫夷。
實(shí)現(xiàn)View滑動(dòng)有很多種方法,這篇文章主要講解六種滑動(dòng)的方法阱飘,分別是:layout()offsetLeftAndRight()與offsetTopAndBottom()师崎、LayoutParams丐巫、動(dòng)畫治筒、scollTo與scollByScroller启绰。

實(shí)現(xiàn) View 滑動(dòng)的六種方法

layout()

view進(jìn)行繪制的時(shí)候會(huì)調(diào)用onLayout()方法來設(shè)置顯示的位置,因此我們同樣也可以通過修改View的left脸侥、top滩援、right、bottom這四種屬性來控制View的坐標(biāo)谅辣。首先我們要自定義一個(gè)View,在onTouchEvent()方法中獲取觸摸點(diǎn)的坐標(biāo):

   public boolean onTouchEvent(MotionEvent event) {
        //獲取到手指處的橫坐標(biāo)和縱坐標(biāo)
        int x = (int) event.getX();
        int y = (int) event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = x;
                lastY = y;
                break;
...

接下來我們?cè)贏CTION_MOVE事件中計(jì)算偏移量厦幅,再調(diào)用layout()方法重新放置這個(gè)自定義View的位置就好了:

case MotionEvent.ACTION_MOVE:
    //計(jì)算移動(dòng)的距離
    int offsetX = x - lastX;
    int offsetY = y - lastY;
    //調(diào)用layout方法來重新放置它的位置
    layout(getLeft()+offsetX, getTop()+offsetY,
            getRight()+offsetX , getBottom()+offsetY);
    break;

當(dāng)我們每次移動(dòng)時(shí)都會(huì)調(diào)用layout()方法來對(duì)自己重新布局篙骡,從而達(dá)到移動(dòng)View的效果。

View的寬高是有top、left夺欲、right、bottom參數(shù)決定褐着。

自定義View的全部代碼(MovingView.kt):

package com.kevin.viewmovingtest

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout


/**
 * Created by Kobe on 2017/10/11.
 * Moving view
 */

class MovingView : View {

    companion object {
        private val TAG = "MovingView"
    }

    // 記錄點(diǎn)擊時(shí)的坐標(biāo)
    private var lastX = 0
    private var lastY = 0

    // 構(gòu)造函數(shù)
    constructor(context: Context?) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    // 觸碰監(jiān)聽
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        event?.let {
            // 獲取當(dāng)前坐標(biāo)
            val currentX: Int = event.x.toInt()
            val currentY: Int = event.y.toInt()
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    Log.d(TAG, "MotionEvent.ACTION_DOWN")
                    lastX = currentX
                    lastY = currentY
                }
                MotionEvent.ACTION_MOVE -> {
                    Log.d(TAG, "MotionEvent.ACTION_MOVE")
                    // 移動(dòng)距離
                    val offsetX = currentX - lastX
                    val offsetY = currentY - lastY
                    // 設(shè)置View的坐標(biāo)
                    Log.d(TAG, "Left:$left,Right:$right,Top:$top,Bottom:$bottom")

                    // 使用layout()
//                    layout(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)

                    // 使用offsetLeftAndRight()和offsetTopAndBottom()
//                    offsetLeftAndRight(offsetX)
//                    offsetTopAndBottom(offsetY)

                    // 使用LayoutParams
                    val lp: ConstraintLayout.LayoutParams = layoutParams as ConstraintLayout.LayoutParams
                    lp.leftMargin = offsetX + left
                    lp.topMargin = offsetY + top
                    layoutParams = lp
                }
            }
        }
        return true
    }
}

布局中引用自定義View:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/cl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.kevin.viewmovingtest.MainActivity">

    <com.kevin.viewmovingtest.MovingView
        android:id="@+id/mv_test"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp"
        android:layout_marginRight="0dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintVertical_bias="0.0"
        android:layout_marginLeft="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintHorizontal_bias="0.0" />

</android.support.constraint.ConstraintLayout>

offsetLeftAndRight()與offsetTopAndBottom()

這兩種方法和layout()方法效果方法差不多掌腰,使用也差不多狰住,我們將ACTION_MOVE中的代碼替換成如下代碼:

case MotionEvent.ACTION_MOVE:
    //計(jì)算移動(dòng)的距離
    int offsetX = x - lastX;
    int offsetY = y - lastY;
    //對(duì)left和right進(jìn)行偏移
    offsetLeftAndRight(offsetX);
    //對(duì)top和bottom進(jìn)行偏移
    offsetTopAndBottom(offsetY);
    break;

上面代碼塊對(duì)應(yīng)的變化如下:

MotionEvent.ACTION_MOVE -> {
      Log.d(TAG, "MotionEvent.ACTION_MOVE")
      // 移動(dòng)距離
      val offsetX = currentX - lastX
      val offsetY = currentY - lastY
      // 設(shè)置View的坐標(biāo)
      Log.d(TAG, "Left:$left,Right:$right,Top:$top,Bottom:$bottom")
//    layout(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)

      offsetLeftAndRight(offsetX)
      offsetTopAndBottom(offsetY)
}

LayoutParams(改變布局參數(shù))

LayoutParams主要保存了一個(gè)View的布局參數(shù),因此我們可以通過LayoutParams來改變View的布局的參數(shù)從而達(dá)到了改變View的位置的效果齿梁。同樣的我們將ACTION_MOVE中的代碼替換成如下代碼:

LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams();
              layoutParams.leftMargin = getLeft() + offsetX;
              layoutParams.topMargin = getTop() + offsetY;
              setLayoutParams(layoutParams);

上面對(duì)應(yīng)的代碼塊變化如下催植,由于我這里使用ConstraintLayout,所以和劉老師的有點(diǎn)不同勺择,需要在布局文件中做好約束:

MotionEvent.ACTION_MOVE -> {
      Log.d(TAG, "MotionEvent.ACTION_MOVE")
      // 移動(dòng)距離
      val offsetX = currentX - lastX
      val offsetY = currentY - lastY
      // 設(shè)置View的坐標(biāo)
      Log.d(TAG, "Left:$left,Right:$right,Top:$top,Bottom:$bottom")
      
      // 使用layout()
//  layout(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)

    // 使用offsetLeftAndRight()和offsetTopAndBottom()
// offsetLeftAndRight(offsetX)
// offsetTopAndBottom(offsetY)

    // 使用LayoutParams
      val lp: ConstraintLayout.LayoutParams = layoutParams as ConstraintLayout.LayoutParams
      lp.leftMargin = offsetX + left
      lp.topMargin = offsetY + top
      layoutParams = lp
}

動(dòng)畫

可以采用View動(dòng)畫來移動(dòng)创南,在res目錄新建anim文件夾并創(chuàng)建translate.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="300" android:duration="1000"/>
</set>

在代碼中引用:

mv_test.animation = AnimationUtils.loadAnimation(this, R.anim.translate)
ObjectAnimator.ofFloat(mv_test, "translationX", 0.toFloat(), 300.toFloat()).setDuration(1000).start()

使用scrollTo與scrollBy

  • scollTo(x,y)表示移動(dòng)到一個(gè)具體的坐標(biāo)點(diǎn)
  • scollBy(dx,dy)則表示移動(dòng)的增量為dx、dy

scollTo省核、scollBy移動(dòng)的是View的內(nèi)容稿辙,如果在ViewGroup中使用則是移動(dòng)他所有的子View。我們將ACTION_MOVE中的代碼替換成如下代碼:

((View)getParent()).scrollBy(-offsetX,-offsetY);

這里要實(shí)現(xiàn)CustomView隨著我們手指移動(dòng)的效果的話气忠,我們就需要將偏移量設(shè)置為負(fù)值邻储。

上面對(duì)應(yīng)的代碼塊變化如下:

MotionEvent.ACTION_MOVE -> {
      Log.d(TAG, "MotionEvent.ACTION_MOVE")
      // 移動(dòng)距離
      val offsetX = currentX - lastX
      val offsetY = currentY - lastY
      // 設(shè)置View的坐標(biāo)
      Log.d(TAG, "Left:$left,Right:$right,Top:$top,Bottom:$bottom")

      // 使用layout()
//     layout(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)

      // 使用offsetLeftAndRight()和offsetTopAndBottom()
//     offsetLeftAndRight(offsetX)
//     offsetTopAndBottom(offsetY)

     // 使用LayoutParams
//      val lp: ConstraintLayout.LayoutParams = layoutParams as ConstraintLayout.LayoutParams
//      lp.leftMargin = offsetX + left
//      lp.topMargin = offsetY + top
//      layoutParams = lp

       // 使用scrollTo與scrollBy
       (parent as View).scrollBy(-offsetX, -offsetY)
}

Scroller

用scollTo/scollBy方法來進(jìn)行滑動(dòng)時(shí),這個(gè)過程是瞬間完成的旧噪,所以用戶體驗(yàn)不大好吨娜。這里我們可以使用Scroller來實(shí)現(xiàn)有過度效果的滑動(dòng),這個(gè)過程不是瞬間完成的淘钟,而是在一定的時(shí)間間隔完成的宦赠。Scroller本身是不能實(shí)現(xiàn)View的滑動(dòng)的,它需要配合View的computeScroll()方法才能彈性滑動(dòng)的效果日月。

在這里我們實(shí)現(xiàn)CustomView平滑的向右移動(dòng)袱瓮。

  • 首先我們要初始化Scroller:
public CustomView(Context context, AttributeSet attrs) {
      super(context, attrs);
      mScroller = new Scroller(context);
  }
  • 接下來重寫computeScroll()方法,系統(tǒng)會(huì)在繪制View的時(shí)候在draw()方法中調(diào)用該方法爱咬,這個(gè)方法中我們調(diào)用父類的scrollTo()方法并通過Scroller來不斷獲取當(dāng)前的滾動(dòng)值尺借,每滑動(dòng)一小段距離我們就調(diào)用invalidate()方法不斷的進(jìn)行重繪,重繪就會(huì)調(diào)用computeScroll()方法精拟,這樣我們就通過不斷的移動(dòng)一個(gè)小的距離并連貫起來就實(shí)現(xiàn)了平滑移動(dòng)的效果:
@Override
public void computeScroll() {
    super.computeScroll();
    if(mScroller.computeScrollOffset()){
        ((View) getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
         //通過不斷的重繪不斷的調(diào)用computeScroll方法
         invalidate();
    }  
}
  • 調(diào)用Scroller.startScroll()方法燎斩。我們?cè)贑ustomView中寫一個(gè)smoothScrollTo()方法虱歪,調(diào)用Scroller.startScroll()方法,在2000毫秒內(nèi)沿X軸平移delta像素:
public void smoothScrollTo(int destX,int destY){
      int scrollX=getScrollX();
      int delta=destX-scrollX;
      //1000秒內(nèi)滑向destX
      mScroller.startScroll(scrollX,0,delta,0,2000);
      invalidate();
  }
  • 最后我們?cè)赩iewSlideActivity.java中調(diào)用CustomView的smoothScrollTo()方法:
//使用Scroll來進(jìn)行平滑移動(dòng)
mCustomView.smoothScrollTo(-400,0);

這里我們是設(shè)定CustomView沿著X軸向右平移400像素栅表。

invalidate() 說明一下
說明:請(qǐng)求重繪View樹笋鄙,即draw()過程,假如視圖發(fā)生大小沒有變化就不會(huì)調(diào)用layout()過程怪瓶,并且只繪制那些“需要重繪的”視圖萧落,即誰(View的話,只繪制該View 洗贰;ViewGroup找岖,則繪制整個(gè)ViewGroup)請(qǐng)求invalidate()方法,就繪制該視圖敛滋。
一般引起invalidate()操作的函數(shù)如下:

  1. 直接調(diào)用invalidate()方法许布,請(qǐng)求重新draw(),但只會(huì)繪制調(diào)用者本身绎晃。
  2. setSelection()方法 :請(qǐng)求重新draw()蜜唾,但只會(huì)繪制調(diào)用者本身。
  3. setVisibility()方法 : 當(dāng)View可視狀態(tài)在INVISIBLE轉(zhuǎn)換VISIBLE時(shí)庶艾,會(huì)間接調(diào)用invalidate()方法袁余,繼而繪制該View。
  4. setEnabled()方法 : 請(qǐng)求重新draw()咱揍,但不會(huì)重新繪制任何視圖包括該調(diào)用者本身泌霍。

按照上述,重新修改了代碼述召,如下:

  1. 創(chuàng)建全局變量mScroller
lateinit var mScroller: Scroller
  1. 初始化對(duì)象
private fun init(context: Context) {
    // 使用Scroller
    mScroller = Scroller(context)
}
  1. 重寫computeScroll()方法
override fun computeScroll() {
    super.computeScroll()
    if (mScroller.computeScrollOffset()) {
        (parent as View).scrollTo(mScroller.currX, mScroller.currY)
        //通過不斷的重繪不斷的調(diào)用computeScroll方法
        invalidate()
    }
}
  1. 生成接口smoothScrollTo()
fun smoothScrollTo(destX: Int, destY: Int) {
    val scrollX = scrollX
    val delta = destX - scrollX
    val scrollY = scrollY

//    mScroller.startScroll(scrollX,0,delta,0,4000)
    mScroller.startScroll(scrollX,scrollY,destX,destY,6000)
    invalidate()
}
  1. 使用該接口
mv_test.smoothScrollTo(-900,-200)
效果圖

源碼地址

MotionEvent.ACTION_DOWN 按下后只會(huì)調(diào)用一次,直到第二次按下

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蟹地,一起剝皮案震驚了整個(gè)濱河市积暖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌怪与,老刑警劉巖夺刑,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異分别,居然都是意外死亡遍愿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門耘斩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來沼填,“玉大人,你說我怎么就攤上這事括授∥塍希” “怎么了岩饼?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長薛夜。 經(jīng)常有香客問我籍茧,道長,這世上最難降的妖魔是什么梯澜? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任寞冯,我火速辦了婚禮,結(jié)果婚禮上晚伙,老公的妹妹穿的比我還像新娘吮龄。我一直安慰自己,他們只是感情好撬腾,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布螟蝙。 她就那樣靜靜地躺著,像睡著了一般民傻。 火紅的嫁衣襯著肌膚如雪胰默。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天漓踢,我揣著相機(jī)與錄音牵署,去河邊找鬼。 笑死喧半,一個(gè)胖子當(dāng)著我的面吹牛奴迅,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播挺据,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼取具,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了扁耐?” 一聲冷哼從身側(cè)響起暇检,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎婉称,沒想到半個(gè)月后块仆,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡王暗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年悔据,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片俗壹。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡科汗,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出绷雏,到底是詐尸還是另有隱情肛捍,我是刑警寧澤隐绵,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站拙毫,受9級(jí)特大地震影響依许,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜缀蹄,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一峭跳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缺前,春花似錦蛀醉、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至逝段,卻和暖如春垛玻,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奶躯。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國打工帚桩, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人嘹黔。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓账嚎,卻偏偏與公主長得像,于是被迫代替她去往敵國和親儡蔓。 傳聞我的和親對(duì)象是個(gè)殘疾皇子郭蕉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容