失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > android实现图片向上展开和慢慢折叠的过度动画

android实现图片向上展开和慢慢折叠的过度动画

时间:2022-06-28 13:19:16

相关推荐

android实现图片向上展开和慢慢折叠的过度动画

需要实现的的效果

描述:一开始是完全显示的,然后高度慢慢减少,图片自上而下消失。

分析:这个效果看似很简单,但是实际上根据常规的android机制,是很难以实现的。

常规方案

Imageview变化大小,只会整体压缩,不可行外布局一个RelativeLayout,Imageview靠底部对齐,然后自上而下减少外布局高度,也会压缩图像,还是不可行用一个空白的布局慢慢遮挡图片,实现了现在的效果,但是时间占用了额外的空间。图像消失的部分是有内容需要显示的。不符合要求。

解决方案

想了很久,突然联想到MeasureSpec的三种情况(自适应,合父布局一样和绝对大小)。感觉图像设置为绝对大小并且靠底部对齐。然后自上而下减少外布局高度,应该可以实现效果。

首先

自定义绝对大小的Imageview

package com.example.gbq.test.view;import android.annotation.SuppressLint;import android.content.Context;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.widget.ImageView;import com.example.gbq.monUtil;@SuppressLint("AppCompatCustomView")public class AnimImageView extends ImageView {private static final int IMAGE_HEIGHT = 200;public AnimImageView(Context context) {super(context);}public AnimImageView(Context context, @Nullable AttributeSet attrs) {super(context, attrs, 0);}public AnimImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr, 0);}/*** 设置的高度最好是bitmap的高度,这样不好压缩变形。这里写死了* @param widthMeasureSpec 宽度* @param heightMeasureSpec 高度*/@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(widthMeasureSpec, CommonUtil.dip2px(IMAGE_HEIGHT));}}

布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"tools:context=".MainActivity"><Buttonandroid:id="@+id/bt_scale_big"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="60dp"android:text="@string/scale_big" /><Buttonandroid:id="@+id/bt_scale_small"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/bt_scale_big"android:layout_centerHorizontal="true"android:layout_marginTop="30dp"android:text="@string/scale_small" /><RelativeLayoutandroid:id="@+id/ll_content"android:layout_width="match_parent"android:layout_height="100dp"android:layout_alignParentBottom="true"><com.example.gbq.test.view.AnimImageViewandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_alignParentBottom="true"android:src="@mipmap/bg_anim_test" /></RelativeLayout></RelativeLayout>

动画实现

package com.example.gbq.test;import android.animation.ValueAnimator;import android.os.Bundle;import android.os.StrictMode;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.animation.LinearInterpolator;import android.widget.RelativeLayout;import com.example.gbq.monUtil;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private RelativeLayout mContentLayout;private RelativeLayout.LayoutParams mLayoutParams;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);if (BuildConfig.DEBUG) {StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());}findIds();}private void findIds() {mContentLayout = findViewById(R.id.ll_content);findViewById(R.id.bt_scale_big).setOnClickListener(this);findViewById(R.id.bt_scale_small).setOnClickListener(this);mLayoutParams = (RelativeLayout.LayoutParams)mContentLayout.getLayoutParams();}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_scale_big:animScaleBig();break;case R.id.bt_scale_small:animScaleSmall();break;default:break;}}private void animScaleBig() {ValueAnimator scaleBig = ValueAnimator.ofFloat(100, 200);scaleBig.setInterpolator(new LinearInterpolator());scaleBig.setDuration(1000);scaleBig.addUpdateListener(mListener);scaleBig.start();}private ValueAnimator.AnimatorUpdateListener mListener = new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {mLayoutParams.height = CommonUtil.dip2px((float) animation.getAnimatedValue());mContentLayout.setLayoutParams(mLayoutParams);}};private void animScaleSmall() {ValueAnimator scaleBig = ValueAnimator.ofFloat(200, 100);scaleBig.setInterpolator(new LinearInterpolator());scaleBig.setDuration(1000);scaleBig.addUpdateListener(mListener);scaleBig.start();}}

最终验证方案是可行的。

总结:要多读书,记住原理。再难得问题也是由基础知识解决党的。就像这个效果,看似简单,细想很难很难。但是实现效果的关键不过就是一行代码:

setMeasuredDimension(widthMeasureSpec, CommonUtil.dip2px(IMAGE_HEIGHT));

如果觉得《android实现图片向上展开和慢慢折叠的过度动画》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。