安卓給介面上的view新增動畫
阿新 • • 發佈:2018-12-18
動畫彈出動畫隱藏。(可以分享整個專案)
1.在RelativeLayout佈局中新增這樣一個佈局
<LinearLayout android:id="@+id/tips_layout" android:layout_width="170dp" android:layout_height="100dp" android:layout_marginLeft="-170dp" android:orientation="vertical" android:background="@drawable/tip_layout_drawable" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:gravity="center"> <TextView android:id="@+id/tip_textview" android:textSize="10dp" android:gravity="center" android:textColor="@color/colordarkGray" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/tips"/> </LinearLayout> <Button android:id="@+id/tip_button" android:layout_toRightOf="@id/tips_layout" android:layout_centerVertical="true" android:layout_width="8dp" android:layout_height="40dp" android:background="@color/colorMian”/>
則是在介面的左側以左建立了一個LinearLayout而Button剛好緊貼介面左側
2.獲取該view和需要點選的按鈕
private LinearLayout tip_layout;
private Button tip_button;
tip_layout = view.findViewById(R.id.tips_layout);
tip_button = view.findViewById(R.id.tip_button);
tip_button.setOnClickListener(this);
3.在按鈕點選方法裡面現實動畫彈出和隱藏
//設定說明的layout的位置 private void setTipFrame() { final RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)tip_layout.getLayoutParams(); ValueAnimator valueAnimator; if (layoutParams.leftMargin == -layoutParams.width){ valueAnimator = ValueAnimator.ofInt(-layoutParams.width,leftMargin); }else { leftMargin = layoutParams.leftMargin; valueAnimator = ValueAnimator.ofInt(leftMargin,-layoutParams.width); } valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { int width = (int)valueAnimator.getAnimatedValue(); layoutParams.leftMargin = width; tip_layout.setLayoutParams(layoutParams); } }); valueAnimator.setDuration(200); valueAnimator.start(); }