編輯:關於 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 的邊距與尺寸。理解之後,類似的視差滾動、頭像縮放都能用同一套思路完成。
以上就是本文的全部內容,希望對大家的學習有所幫助。
Android 記憶體洩漏的排查思路與實用工具
Java 虛擬機運行時通常有一個記憶體上限,一旦超過就會拋出 OutOfMemoryError。而這種情況背後往往存在記憶體洩漏。 我認為解決記憶體洩漏可以拆成兩個步驟:
Android FTP 多執行緒斷點續傳下載與上傳
FTP 斷點續傳原理FTP 沒有 HTTP 的位元組範圍標頭,但可透過 REST 命令指定續傳位置,並以 SIZE 取得檔案大小。單線程續傳時,會記錄上次中斷位置,重新連
Android 百日程序:Fragment 動態管理與生命週期
之前寫過 Fragment 的使用,Fragment 可以靜態聲明,也可以在運行時動態載入。這一章進一步看看如何動態更換 Fragment,以及 Fragment 生命週
Android 讀取 asset 大文件報錯的解決方案
在 Android 中,把文件放進 assets 目錄時其實存在大小限制。一旦單個文件超過 1M,就可能出 Data exceeds UNCOMPRESS_DATA_MA