網站首頁 編程語言 正文
先上效果圖
源碼 github.com/woshiwzy/Ca…
實現原理:
1.繼承LinearLayout
2.重寫onLayout,onMeasure 方法
3.利用ValueAnimator 實施動畫
4.在動畫回調中requestLayout 實現動畫效果
思路:
1.用Bounds 對象記錄每一個CardView 對象的初始位置,當前位置,運動目標位置
2.點擊時計算出對應的view以及可能會產生關聯運動的View的運動的目標位置,從當前位置運動到目標位置,然后以這2個位置作為動畫參數實施ValueAnimator動畫,在動畫回調中觸發onLayout,達到動畫的效果。
重寫adView 方法
確保新添加的在這里確保所有的子view 都有一個初始化的bounds位置
@Override
public void addView(View child, ViewGroup.LayoutParams params) {
super.addView(child, params);
Bounds bounds = getBunds(getChildCount());
}
確保每個子View的測量屬性寬度填滿父組件
boolean mesured = false;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mesured == true) {//只需要測量一次
return;
}
mesured = true;
int childCount = getChildCount();
int rootWidth = getWidth();
int rootHeight = getHeight();
if (childCount > 0) {
View child0 = getChildAt(0);
int modeWidth = MeasureSpec.getMode(child0.getMeasuredWidth());
int sizeWidth = MeasureSpec.getSize(child0.getMeasuredWidth());
int modeHeight = MeasureSpec.getMode(child0.getMeasuredHeight());
int sizeHeight = MeasureSpec.getSize(child0.getMeasuredHeight());
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
childView.measure(MeasureSpec.makeMeasureSpec(sizeWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(sizeHeight, MeasureSpec.EXACTLY));
int top = (int) (i * (sizeHeight * carEvPercnet));
getBunds(i).setTop(top);
getBunds(i).setCurrentTop(top);
getBunds(i).setLastCurrentTop(top);
getBunds(i).setHeight(sizeHeight);
}
}
}
}
重寫onLayout 方法是關鍵
是動畫觸發的主要目的,這里layout參數并不是寫死的,而是計算出來的(通過ValueAnimator 計算出來的)
@Override
protected void onLayout(boolean changed, int sl, int st, int sr, int sb) {
int childCount = getChildCount();
if (childCount > 0) {
for (int i = 0; i < childCount; i++) {
View view = getChildAt(i);
int mWidth = view.getMeasuredWidth();
int mw = MeasureSpec.getSize(mWidth);
int l = 0, r = l + mw;
view.layout(l, getBunds(i).getCurrentTop(), r, getBunds(i).getCurrentTop() + getBunds(i).getHeight());
}
}
}
源碼
github: github.com/woshiwzy/Ca…
原文鏈接:https://juejin.cn/post/7073371960150851615
相關推薦
- 2022-07-26 用VScode編寫C++大型項目的方法步驟_C 語言
- 2022-07-26 Python使用psutil獲取系統信息_python
- 2022-07-19 Linux ifconfig命令配置和顯示Linux系統網卡的網絡參數
- 2023-02-12 Android畫中畫窗口開啟方法_Android
- 2022-06-25 CentOS?8?安裝調試KVM的詳細步驟_Kvm
- 2022-12-01 Go語言實戰之實現一個簡單分布式系統_Golang
- 2022-08-05 C語言庫函數qsort的使用詳解_C 語言
- 2023-04-22 python的open函數使用案例代碼_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支