android linearllayout 隐藏 动画,AnimatedLinearLayout:带删除动画的LinearLayout
本文介紹一個自定義ViewGroup:AnimatedLinearLayout,在普通的LinearLayout基礎上,它實現了刪除某一項后,后面的項依次前移或上移的動畫效果,效果如下:
AnimatedLinearLayout效果
代碼如下:
在onMeasure函數中加入init函數,該函數負責記錄AnimatedLinearLayout下直屬子view列表,并給每個子view添加點擊事件,實現該子view點擊消失,同時后面的view前移的效果。注意,這里由于AnimatedLinearLayout可能會使用wrap_content屬性值,會導致onMeasure方法執行多次,所以我們加入了一個標志位isInited,只在第一次執行onMeasure的時候進行初始化操作。
public class AnimatedLinearLayout extends LinearLayout {
...
private boolean isInited = false;
...
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!isInited) {
init();
isInited = true;
}
}
...
}
下面是init函數的實現,主要是兩個操作:將子view添加到directChildViewList,給子view添加點擊事件。點擊事件又分三步,執行給AnimatedLinearLayout設置的listener,隱藏被點擊的子view,將排在該view之后的view依次往前移動一個位置。這里移動的動畫使用ObjectAnimator實現,Android動畫技術可以參考這篇文章。首先記錄需要移動的view當前的偏移量curTranslationX,然后向左或向上移動一個距離。需要注意的是,getTranslationX獲取的是該view相對最初位置在X軸方向移動的距離,而不是該view距離屏幕左上角遠點的X軸距離,所以AnimatedLinearLayout里面所有子view初始的偏移量都是0,移動的次數越多,偏移量越大。
public class AnimatedLinearLayout extends LinearLayout {
private List directChildViewList = new ArrayList<>();
private OnClickListener directChildOnClickListener = null;
...
private void init() {
// 獲取直屬子view數量
int directChildCnt = getChildCount();
for (int i = 0; i < directChildCnt; i++) {
// 將直屬子view添加到list中
final View directChildView = getChildAt(i);
directChildViewList.add(directChildView);
// 給直屬子view添加點擊響應
directChildView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 如果設置了子view的點擊事件,先執行點擊事件
if (null != directChildOnClickListener) {
directChildOnClickListener.onClick(v);
}
// 然后隱藏該view
directChildView.setVisibility(INVISIBLE);
// 并將排在該view之后的view依次往前移動一個位置,移動的距離為點擊消失的view的寬度(或高度)
MarginLayoutParams layoutParams = (MarginLayoutParams ) directChildView.getLayoutParams();
int currentPos = directChildViewList.indexOf(directChildView);
for (int j = currentPos + 1; j < directChildViewList.size(); j++) {
View viewToMove = directChildViewList.get(j);
if (getOrientation() == HORIZONTAL) {
float moveLeftVal = directChildView.getWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
float curTranslationX = viewToMove.getTranslationX();
ObjectAnimator moveLeft = ObjectAnimator.ofFloat(viewToMove, "translationX", curTranslationX, curTranslationX - moveLeftVal);
moveLeft.setDuration(1000);
moveLeft.start();
} else {
float moveTopVal = directChildView.getHeight() + layoutParams.topMargin + layoutParams.bottomMargin;
float curTranslationY = viewToMove.getTranslationY();
ObjectAnimator moveTop = ObjectAnimator.ofFloat(viewToMove, "translationY", curTranslationY, curTranslationY - moveTopVal);
moveTop.setDuration(1000);
moveTop.start();
}
}
}
});
}
}
// 設置直屬子view點擊事件
public void setDirectChildOnClickListener(OnClickListener listener) {
directChildOnClickListener = listener;
}
}
AnimatedLinearLayout的使用:
布局文件
android:layout_width="match_parent"
android:layout_height="40dp"
android:scrollbars="none">
android:id="@+id/animated_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="遠上寒山石徑斜"
android:textSize="15sp"
android:layout_marginLeft="20dp" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="白云深處有人家"
android:textSize="15sp"
android:layout_marginLeft="20dp"/>
設置AnimatedLinearLayout子view點擊事件:
public class MainActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mAnimatedLinearLayoutHorizental.setDirectChildOnClickListener(getOnClickListener());
...
}
private View.OnClickListener getOnClickListener() {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, ((TextView)v).getText(), Toast.LENGTH_SHORT).show();
}
};
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android linearllayout 隐藏 动画,AnimatedLinearLayout:带删除动画的LinearLayout的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为p40pro怎么用鸿蒙,数码知识:华
- 下一篇: html 输入框变红色,input输入框