以动画的方式实现View的展开和缩放
2016-03-12 17:42
393 查看
<pre name="code" class="java"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">Activity代码如下</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;"> </span></span>
<span style="font-size:18px;">import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.widget.Button; import android.widget.LinearLayout; public class MainActivity extends AppCompatActivity { private LinearLayout contentPanel; private Button expand_btn; private int height; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contentPanel= (LinearLayout) findViewById(R.id.contentPanel); expand_btn= (Button) findViewById(R.id.expand_btn); contentPanel.measure(0, 0); height=contentPanel.getMeasuredHeight(); } public void to(View v){ if(contentPanel.isShown()){ contraction(); }else{ expand(); } } /** * 收缩 */ private void contraction(){ AnimatorSet set=new AnimatorSet(); //根据值的改变,动态设置view的高度 ValueAnimator valueAnimator=ValueAnimator.ofInt(height,0); valueAnimator.setDuration(1000); valueAnimator.setInterpolator(new AccelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int height = (int) animation.getAnimatedValue(); Log.e("height", height + ""); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height); contentPanel.setLayoutParams(params); } }); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { contentPanel.setVisibility(View.GONE); expand_btn.setText("展开"); } }); //将view从不透明变成透明 ObjectAnimator alpha=ObjectAnimator.ofFloat(contentPanel,"alpha",1f,0f); alpha.setDuration(1000); set.play(valueAnimator).with(alpha); set.start(); } /** * 展开 */ private void expand(){ contentPanel.setVisibility(View.VISIBLE); AnimatorSet set=new AnimatorSet(); ValueAnimator valueAnimator=ValueAnimator.ofInt(0,height); valueAnimator.setDuration(1000); valueAnimator.setInterpolator(new AccelerateInterpolator()); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int height = (int) animation.getAnimatedValue(); Log.e("height", height + ""); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height); contentPanel.setLayoutParams(params); } }); valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { expand_btn.setText("收起"); } }); //将view从透明变成不透明 ObjectAnimator alpha=ObjectAnimator.ofFloat(contentPanel,"alpha",0f,1f); alpha.setDuration(1000); set.play(valueAnimator).with(alpha); set.start(); } } </span></span>
XML代码:
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span></span>
<span style="font-size:18px;"> xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/expand_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="to" android:text="展开"/> <LinearLayout android:id="@+id/contentPanel" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111111111111111111111111111111111111"/> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""/> </LinearLayout> </span></span>
相关文章推荐
- GitHub创建新仓库
- 利用C++类实现顺序表
- MATLAB GUI设计之静态文本框多行显示的问题
- 关于文本分类的若干方法
- Android音乐播放器的开发实例
- 构建之法阅读笔记01
- 解决MySQL出现大量unauthenticated user的问题
- 随机数的四则运算2
- MakeFile 学习日记(一)——makefile基本概念
- Android Material Design(5) CoordinatorLayout,AppBarLayout,Toolbar,CollapsingToolbarLayout的使用
- opencv图像反转/截取
- 内部排序算法总结
- 快速排序
- 基础总结
- 论TextView的超链接使用、显示表情、Html语法支持、跑马灯效果的实现
- 四则运算2
- 进度条01
- Windows系统下搭建Jenkins环境
- VC之美化界面篇
- Git远程仓库简介