使用属性动画以及自定义view实现图片的显示与隐藏

效果图为:点击加号按钮,加号隐藏,减号旋转显示,另外三张图片也旋转一定角度显示

点击减号按钮,减号隐藏,加号旋转显示,另外三张图片也旋转一定角度隐藏


首先放五张图片,使用RelativeLayout布局,因为可以使这五张图片重叠起来

customer.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <RelativeLayout
        android:layout_marginRight="20dp"
       android:gravity="right"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/image_pingbi"
        android:src="@drawable/shiled"
        />

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/image_copy"
        android:src="@drawable/copylink"
        />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/image_report"
        android:src="@drawable/report"
        />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/image_jian"
        android:src="@drawable/packuphorizontal"
        />
    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/image_show"
        android:src="@drawable/show"
        />

</RelativeLayout>
</LinearLayout>

使用自定义view的方式,继承RelativeLayout,引入customer.xml布局

public class CustomerView extends RelativeLayout {
    private ImageView image_show;
    private ImageView image_jian;
    private ImageView image_report;
    private ImageView image_copy;
    private ImageView image_pingbi;
    public CustomerView(Context context) {
        super(context);
    }

    public CustomerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.customer, this, false);
        image_show = view.findViewById(R.id.image_show);
        image_jian = view.findViewById(R.id.image_jian);
        image_report = view.findViewById(R.id.image_report);
        image_copy = view.findViewById(R.id.image_copy);
        image_pingbi = view.findViewById(R.id.image_pingbi);

        //加号按钮的点击事件,展示另外三张图片
        image_show.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                image_jian.setVisibility(View.VISIBLE);
                image_show.setVisibility(View.GONE);
                showMenu();
            }
        });

        //减号的点击事件,隐藏另外三张图片
        image_jian.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                image_jian.setVisibility(View.GONE);
                image_show.setVisibility(View.VISIBLE);
                hideMenu();
            }
        });

        addView(view);
    }

    public CustomerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    //属性动画,,展示出来
    public void showMenu(){
        //三个平移动画 平移出来
        ObjectAnimator firstAnimator = ObjectAnimator.ofFloat(image_pingbi
                ,"translationX",0,-65*3);
        ObjectAnimator secondAnimator = ObjectAnimator.ofFloat(image_copy
                ,"translationX",0,-65*2);
        ObjectAnimator thirdAnimator = ObjectAnimator.ofFloat(image_report
                ,"translationX",0,-65*1);

        //旋转动画
        ObjectAnimator rotation1 = ObjectAnimator.ofFloat(image_jian, "rotation", 0, 135, 0);
        ObjectAnimator rotation2 = ObjectAnimator.ofFloat(image_report, "rotation", 0, 180, 0);
        ObjectAnimator rotation3 = ObjectAnimator.ofFloat(image_copy, "rotation", 0, 180, 0);
        ObjectAnimator rotation4 = ObjectAnimator.ofFloat(image_pingbi, "rotation", 0, 180, 0);

        //组合动画
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(800);//动画时长
        animatorSet.setInterpolator(new OvershootInterpolator());
        //设置动画一起播放
        animatorSet.playTogether(rotation1,rotation2,rotation3,rotation4,firstAnimator,secondAnimator,thirdAnimator);

        animatorSet.start();

    }

    public void hideMenu(){
        //三个平移回去
        ObjectAnimator firstAnimator = ObjectAnimator.ofFloat(image_pingbi
                , "translationX", image_report.getTranslationX(), 0);
        ObjectAnimator secondAnimator = ObjectAnimator.ofFloat(image_copy
                , "translationX", image_copy.getTranslationX(), 0);
        ObjectAnimator thirdAnimator = ObjectAnimator.ofFloat(image_report
                , "translationX", image_pingbi.getTranslationX(), 0);
        ObjectAnimator rotation1 = ObjectAnimator.ofFloat(image_show, "rotation", 0, 135, 0);
        ObjectAnimator rotation2 = ObjectAnimator.ofFloat(image_copy, "rotation", 0, 180, 0);
        ObjectAnimator rotation3 = ObjectAnimator.ofFloat(image_pingbi, "rotation", 0, 180, 0);
        ObjectAnimator rotation4 = ObjectAnimator.ofFloat(image_report, "rotation", 0, 180, 0);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(800);
        animatorSet.setInterpolator(new OvershootInterpolator());
        animatorSet.playTogether(rotation1,rotation2,rotation3,rotation4,firstAnimator,secondAnimator,thirdAnimator);

        animatorSet.start();
    }

}
在activity_main.xml里面引入CustomerView的布局

 <com.example.day180131zuoye.customview.CustomerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>




  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自定义CollapsingToolbarLayout动画,需要做以下步骤: 1. 创建一个可扩展的CollapsingToolbarLayout。 2. 为CollapsingToolbarLayout定义一个自定义的布局,这个布局将会在CollapsingToolbarLayout折叠时显示。 3. 在自定义布局中添加视图,如ImageView和TextView。 4. 创建一个自定义的Behavior类,它将控制CollapsingToolbarLayout的折叠和展开动画。 5. 在自定义Behavior中,根据CollapsingToolbarLayout的状态来控制自定义布局中视图的动画。 下面是一个简单的示例代码,演示如何自定义CollapsingToolbarLayout动画: ``` public class CustomCollapsingToolbarBehavior extends AppBarLayout.ScrollingViewBehavior { private static final int ANIMATION_DURATION = 600; private ImageView mImageView; private TextView mTitleView; private int mTitleExpandedMarginStart; private int mTitleCollapsedMarginStart; private int mSubtitleExpandedMarginStart; private int mSubtitleCollapsedMarginStart; public CustomCollapsingToolbarBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { if (dependency instanceof AppBarLayout) { return true; } return super.layoutDependsOn(parent, child, dependency); } @Override public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { if (dependency instanceof AppBarLayout) { initViews(child); updateViews((AppBarLayout) dependency); } return super.onDependentViewChanged(parent, child, dependency); } private void initViews(View child) { if (mImageView == null) { mImageView = child.findViewById(R.id.image_view); } if (mTitleView == null) { mTitleView = child.findViewById(R.id.title_text_view); } if (mTitleExpandedMarginStart == 0) { mTitleExpandedMarginStart = ((LayoutParams) mTitleView.getLayoutParams()).leftMargin; } if (mTitleCollapsedMarginStart == 0) { mTitleCollapsedMarginStart = mTitleView.getWidth() / 2; } if (mSubtitleExpandedMarginStart == 0) { mSubtitleExpandedMarginStart = ((LayoutParams) mSubtitleView.getLayoutParams()).leftMargin; } if (mSubtitleCollapsedMarginStart == 0) { mSubtitleCollapsedMarginStart = mSubtitleView.getWidth() / 2; } } private void updateViews(AppBarLayout appBarLayout) { int totalScrollRange = appBarLayout.getTotalScrollRange(); int offset = Math.abs(appBarLayout.getTop()); float percent = (float) offset / (float) totalScrollRange; // animate ImageView mImageView.setScaleX(1 - percent); mImageView.setScaleY(1 - percent); // animate Title TextView LayoutParams params = (LayoutParams) mTitleView.getLayoutParams(); params.leftMargin = (int) (mTitleExpandedMarginStart + (mTitleCollapsedMarginStart - mTitleExpandedMarginStart) * percent); mTitleView.setLayoutParams(params); // animate Subtitle TextView LayoutParams params = (LayoutParams) mSubtitleView.getLayoutParams(); params.leftMargin = (int) (mSubtitleExpandedMarginStart + (mSubtitleCollapsedMarginStart - mSubtitleExpandedMarginStart) * percent); mSubtitleView.setLayoutParams(params); } } ``` 在这个示例代码中,我们创建了一个CustomCollapsingToolbarBehavior类,继承自AppBarLayout.ScrollingViewBehavior。在这个类中,我们重写了layoutDependsOn()方法和onDependentViewChanged()方法,用于设置CollapsingToolbarLayout的依赖和更新自定义布局中的视图。 在initViews()方法中,我们初始化了自定义布局中的ImageView和TextView,并获取了它们在展开和折叠状态下的左边距。 在updateViews()方法中,我们根据AppBarLayout的状态来控制自定义布局中各个视图的动画。例如,我们根据AppBarLayout的偏移量来缩放ImageView,并根据AppBarLayout的偏移量计算Title和Subtitle TextView的左边距,从而实现动画效果。 最后,我们需要在布局文件中将CustomCollapsingToolbarBehavior应用到CollapsingToolbarLayout上: ``` <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior=".CustomCollapsingToolbarBehavior" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="48dp" app:expandedTitleMarginEnd="64dp" app:expandedTitleTextAppearance="@android:style/TextAppearance.Large.Inverse" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/image_view" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" app:layout_collapseMode="parallax" /> <LinearLayout android:id="@+id/linear_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" app:layout_collapseMode="parallax"> <TextView android:id="@+id/title_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Title" android:textColor="@android:color/white" android:textSize="24sp" /> <TextView android:id="@+id/subtitle_text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Subtitle" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> </android.support.design.widget.CollapsingToolbarLayout> ``` 在这个布局文件中,我们将CustomCollapsingToolbarBehavior应用到CollapsingToolbarLayout上,并定义了一个自定义布局,包含一个ImageView和两个TextView。我们还在布局文件中定义了CollapsingToolbarLayout的其他属性,如contentScrim、expandedTitleMarginStart、expandedTitleMarginEnd等。 这样,我们就可以自定义CollapsingToolbarLayout的动画了。当AppBarLayout滚动时,ImageView和TextView将会以动画的形式显示隐藏

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值