广告
返回顶部
首页 > 资讯 > 移动开发 >Android属性动画实现布局的下拉展开效果
  • 261
分享到

Android属性动画实现布局的下拉展开效果

布局动画Android 2022-06-06 08:06:33 261人浏览 独家记忆
摘要

在Android的3.0之后,Google又提出了属性动画的这样一个框架,他可以更好的帮助我们实现更丰富的动画效果。所以为了跟上技术的步伐,今天就聊一聊属性动画。 这一次的需求

Android的3.0之后,Google又提出了属性动画的这样一个框架,他可以更好的帮助我们实现更丰富的动画效果。所以为了跟上技术的步伐,今天就聊一聊属性动画。

这一次的需求是这样的:当点击一个View的时候,显示下面隐藏的一个View,要实现这个功能,需要将V iew的visibility属性设置gone为visible即可,但是这个过程是一瞬间的,并不能实现我们要的效果。所以,属性动画是个不错的方案。

先把效果贴上

第一个:


 第二个:

前面的这个是隐藏着,后面这个是显示的。当点击这个箭头的时候,来切换显示或者隐藏。

现在开始编码:

布局文件如下


<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.ltl.mpiggybank.MainActivity" >
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#458EFD"
    android:padding="10dip" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:gravity="center_vertical"
      android:text="下拉展开动画"
      android:textColor="#ffffff"
      android:textSize="20sp" />
  </RelativeLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#548AEA"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="20dip"
      android:text="昨日收益(元)"
      android:textColor="#ffffff"
      android:textSize="16sp" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="0.00"
      android:textColor="#ffffff"
      android:textSize="45sp" />
  </LinearLayout>
  <LinearLayout
    android:id="@+id/linear_hidden"
    android:layout_width="match_parent"
    android:layout_height="120dip"
    android:background="#548AEA"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="20dip"
      android:text="显示的View"
      android:textColor="#ffffff"
      android:textSize="16sp" />
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="0.00"
      android:textColor="#ffffff"
      android:textSize="35sp" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#548AEA"
    android:gravity="center"
    android:onClick="onClick"
    android:orientation="vertical" >
    <ImageView
      android:id="@+id/my_iv"
      android:layout_width="25dip"
      android:layout_height="25dip"
      android:layout_margin="20dip"
      android:src="@drawable/scroll" />
  </LinearLayout>
</LinearLayout>

这里面代码并不多,也很简单,三个线性布局,里面装载着各自的控件,并且还设置了ID。按钮是一个线性布局,采用了onClick自身的点击事件。接下来,当点击了这个线性布局的时候,需要隐藏的控件最终到达一个高度,这个就是我们的目标值,只需要通过布局中的dp转换成像素就行了。


mDensity = getResources().getDisplayMetrics().density;
mHiddenViewMeasuredHeight = (int) (mDensity * 120 + 0.5);

这里是120就是我们布局里面定义的高度。

然后给这个过程增加一个动画效果。


  ValueAnimator animator = ValueAnimator.ofInt(start, end);
    animator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator arg0) {
        int value = (int) arg0.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        layoutParams.height = value;
        v.setLayoutParams(layoutParams);
      }
    });

通过这样一个简单的ValueAnimator ,就可以很方便的实现显示和隐藏的动画效果了。
下面是完整的代码。


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends ActionBarActivity {
  private LinearLayout mHiddenLayout;
  private float mDensity;
  private int mHiddenViewMeasuredHeight;
  private ImageView mIv;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    mHiddenLayout = (LinearLayout) findViewById(R.id.linear_hidden);
    mIv = (ImageView) findViewById(R.id.my_iv);
    mDensity = getResources().getDisplayMetrics().density;
    mHiddenViewMeasuredHeight = (int) (mDensity * 120 + 0.5);
  }
  public void onClick(View v) {
    if (mHiddenLayout.getVisibility() == View.GONE) {
      animateOpen(mHiddenLayout);
      animationIvOpen();
    } else {
      animateClose(mHiddenLayout);
      animationIvClose();
    }
  }
  private void animateOpen(View v) {
    v.setVisibility(View.VISIBLE);
    ValueAnimator animator = createDropAnimator(v, 0,
        mHiddenViewMeasuredHeight);
    animator.start();
  }
  private void animationIvOpen() {
    RotateAnimation animation = new RotateAnimation(0, 180,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
        0.5f);
    animation.setFillAfter(true);
    animation.setDuration(100);
    mIv.startAnimation(animation);
  }
  private void animationIvClose() {
    RotateAnimation animation = new RotateAnimation(180, 0,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
        0.5f);
    animation.setFillAfter(true);
    animation.setDuration(100);
    mIv.startAnimation(animation);
  }
  private void animateClose(final View view) {
    int origHeight = view.getHeight();
    ValueAnimator animator = createDropAnimator(view, origHeight, 0);
    animator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        view.setVisibility(View.GONE);
      }
    });
    animator.start();
  }
  private ValueAnimator createDropAnimator(final View v, int start, int end) {
    ValueAnimator animator = ValueAnimator.ofInt(start, end);
    animator.addUpdateListener(new AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator arg0) {
        int value = (int) arg0.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
        layoutParams.height = value;
        v.setLayoutParams(layoutParams);
      }
    });
    return animator;
  }
}
您可能感兴趣的文章:Android实现简单的下拉阻尼效应示例代码Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能(附源码)Android实现三级联动下拉框 下拉列表spinner的实例代码Android中Spinner(下拉框)控件的使用详解Android下拉刷新ListView——RTPullListView(demo)Android PullToRefreshLayout下拉刷新控件的终结者Android中使用RecyclerView实现下拉刷新和上拉加载Android下拉刷新上拉加载控件(适用于所有View)Android官方下拉刷新控件SwipeRefreshLayout使用详解Android下拉阻尼效果实现原理及简单实例


--结束END--

本文标题: Android属性动画实现布局的下拉展开效果

本文链接: https://www.lsjlt.com/news/24649.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作