Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 仿京東金融首頁:隨捲動縮小的頭像效果

編輯:關於 Android 程式設計

一、效果預覽

先看一下成果,GIF 錄製時有些卡頓,實際在真機上運行相當順暢。

二、實現方式

這個效果看起來有趣,原理其實很單純:只要監聽 ScrollView 在 Y 軸上的滑動距離,再在程式碼中動態調整頭像的位置與尺寸即可。

public class MainActivity extends AppCompatActivity {

    private CircleImageView ivPortrait;
    private ObservableScrollView scrollView;
    private ViewGroup.MarginLayoutParams marginLayoutParams;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        ivPortrait = (CircleImageView) findViewById(R.id.iv_portrait);
        scrollView = (ObservableScrollView) findViewById(R.id.scrollView);
        marginLayoutParams = new ViewGroup.MarginLayoutParams(ivPortrait.getLayoutParams());

        scrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
                // 計算頭像距頂部的距離
                int top = dp2px(70) - y;
                if (top < dp2px(10)) {
                    // 固定在標題欄
                    marginLayoutParams.setMargins(dp2px(20), dp2px(10), 0, 0);
                } else {
                    // 隨捲動向上移動
                    marginLayoutParams.setMargins(dp2px(20), dp2px(70) - y, 0, 0);
                }

                // 依上滑距離縮放頭像
                FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginLayoutParams);
                // 頭像最大 45dp,最小 30dp
                int height = dp2px(45) - y < dp2px(30) ? dp2px(30) : dp2px(45) - y;
                layoutParams.height = height;
                layoutParams.width = height;
                ivPortrait.setLayoutParams(layoutParams);
            }
        });
    }

    private int dp2px(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
    }
}

版面配置檔

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFF">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="#F2F4F7">
            ...
        </RelativeLayout>

        <com.yl.jdfinanceindex.ObservableScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="none">

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

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="80dp"
                    android:background="#F2F4F7">
                    ...
                </RelativeLayout>

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1000dp" />
            </LinearLayout>
        </com.yl.jdfinanceindex.ObservableScrollView>
    </LinearLayout>

    <com.yl.jdfinanceindex.CircleImageView
        android:id="@+id/iv_portrait"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="70dp"
        android:src="@mipmap/ic_portrait" />
</FrameLayout>

原生 ScrollView 並不支援滑動監聽,因此需要自訂一個 ObservableScrollView,把 onScrollChanged 對外暴露出來:

public class ObservableScrollView extends ScrollView {

    private ScrollViewListener scrollViewListener;

    public ObservableScrollView(Context context) {
        super(context);
    }

    public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

    public void setScrollViewListener(ScrollViewListener scrollViewListener) {
        this.scrollViewListener = scrollViewListener;
    }

    @Override
    protected void onScrollChanged(int x, int y, int oldx, int oldy) {
        super.onScrollChanged(x, y, oldx, oldy);
        if (scrollViewListener != null) {
            scrollViewListener.onScrollChanged(this, x, y, oldx, oldy);
        }
    }

    public interface ScrollViewListener {
        void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy);
    }
}

三、小結

整個效果的核心只有一句話:把滑動距離換算成 LayoutParams 的邊距與尺寸。理解之後,類似的視差滾動、頭像縮放都能用同一套思路完成。

以上就是本文的全部內容,希望對大家的學習有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved