溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在Android中使用ScrollView實現一個下拉彈回動畫效果

發布時間:2021-03-08 14:15:29 來源:億速云 閱讀:169 作者:Leah 欄目:移動開發

本篇文章給大家分享的是有關怎么在Android中使用ScrollView實現一個下拉彈回動畫效果,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

Android是什么

Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。

一.自定義View的設計代碼

package com.lwz.mathbox.weight;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

/**
 * 實現了可以有下拉彈回的ScrollView的自定義View
 */
public class SpringScrollView extends ScrollView {

  private View inner;// 孩子

  private float y;// 坐標

  private Rect normal = new Rect();// 矩形空白

  public SpringScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  /***
   * 根據 XML 生成視圖工作完成.該函數在生成視圖的最后調用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate
   * 方法,也應該調用父類的方法,使該方法得以執行.
   */
  @Override
  protected void onFinishInflate() {
    if (getChildCount() > 0) {
      inner = getChildAt(0);// 獲取其孩子
    }
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
    if (inner != null) {
      commOnTouchEvent(ev);
    }
    return super.onTouchEvent(ev);
  }

  /***
   * 觸摸事件
   *
   * @param ev
   */
  public void commOnTouchEvent(MotionEvent ev) {
    int action = ev.getAction();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        y = ev.getY();// 獲取點擊y坐標
        break;
      case MotionEvent.ACTION_UP:
        if (isNeedAnimation()) {
          animation();
        }
        break;
      case MotionEvent.ACTION_MOVE:
        final float preY = y;
        float nowY = ev.getY();
        int deltaY = (int) (preY - nowY);// 獲取滑動距離

        y = nowY;
        // 當滾動到最上或者最下時就不會再滾動,這時移動布局
        if (isNeedMove()) {
          if (normal.isEmpty()) {
            // 填充矩形,目的:就是告訴this:我現在已經有了,你松開的時候記得要執行回歸動畫.
            normal.set(inner.getLeft(), inner.getTop(),
                inner.getRight(), inner.getBottom());
          }
          // 移動布局
          inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2,
              inner.getRight(), inner.getBottom() - deltaY / 2);
        }
        break;

      default:
        break;
    }
  }

  /***
   * 開啟動畫移動
   */
  public void animation() {
    // 開啟移動動畫
    TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
        normal.top);
    ta.setDuration(300);
    inner.startAnimation(ta);
    // 設置回到正常的布局位置
    inner.layout(normal.left, normal.top, normal.right, normal.bottom);
    normal.setEmpty();// 清空矩形
  }

  /***
   * 是否需要開啟動畫
   * <p>
   * 如果矩形不為空,返回true,否則返回false.
   *
   * @return
   */
  public boolean isNeedAnimation() {
    return !normal.isEmpty();
  }

  /***
   * 是否需要移動布局 inner.getMeasuredHeight():獲取的是控件的高度
   * getHeight():獲取的是當前控件在屏幕中顯示的高度
   *
   * @return
   */
  public boolean isNeedMove() {
    int offset = inner.getMeasuredHeight() - getHeight();
    int scrollY = getScrollY();
    // 0是頂部,后面那個是底部
    if (scrollY == 0 || scrollY == offset) {
      return true;
    }
    return false;
  }

}

二.簡單調用示例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  //包名+類型
  <com.lwz.mathbox.weight.SpringScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_margin="10dp"
      android:orientation="vertical">

      <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:gravity="top"
        android:hint="輸入文字"
        android:minLines="4"
        android:singleLine="false"
        android:textSize="14sp" />

      <TextView
        android:id="@+id/tv_size"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:gravity="right"
        android:text="0/255" />

      </LinearLayout>
  </com.lwz.mathbox.weight.SpringScrollView>
</LinearLayout>

以上就是怎么在Android中使用ScrollView實現一個下拉彈回動畫效果,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女