安卓给界面上的view添加动画

动画弹出动画隐藏。(可以分享整个项目)

       

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();
    }

猜你喜欢

转载自blog.csdn.net/weixin_39339407/article/details/85051366