Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View Animation相当简单,不过只能支持简单的缩放、平移、旋转、透明度基本的动画,且有一定的局限性。比如:你希望View有一个颜色的切换动画;你希望可以使用3D旋转动画;你希望当动画停止时,View的位置就是当前的位置;这些View Animation都无法做到。
以3.0版本为界,之前的称之为传统动画,传统动画特点万变不离其宗,宗就是是它刚开始的位置.
之后出了属性动画
需求:
一个button和imageview相邻,点击button,控制imageview的显示和隐藏,并且让imageview从button中隐藏和开启
用传统动画的话可以使用旋转动画并且配以AnimationListener,但是效果不是类似抽屉抽拉的效果
今天我们来用Property Animation实现一个类似抽屉抽拉的效果的动画.
布局文件如下,很简单
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<LinearLayout
android:background="@android:color/holo_blue_bright"
android:onClick="llClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="click me"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:background="@android:color/holo_green_light"
android:id="@+id/llhiddview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hidden view"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
activity中
public class MainActivity extends ActionBarActivity {
LinearLayout llhidv;
int hidviewHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
llhidv = (LinearLayout) findViewById(R.id.llhiddview);
float disn = getResources().getDisplayMetrics().density;
hidviewHeight = (int) (disn * 40 + 0.5);
Log.e("lc---", hidviewHeight +"hidviewHeight");
}
public void llClick(View view) {
if (llhidv.getVisibility() == View.GONE) {
Log.e("lc---", llhidv.getVisibility() + "GONE");
open(llhidv);
} else {
Log.e("lc---", llhidv.getVisibility() + "fei gone");
close(llhidv);
}
}
/**
* 关闭--动画
*
* @param view
*/
private void close(final View view) {
int origheight = view.getHeight();
Log.e("lc---", origheight + "origheight");
ValueAnimator animator = createDropAnimator(view, origheight, 0);
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
Log.e("lc---", "" + "close");
}
});
animator.start();
}
/**
* 打开--动画
*
* @param view
*/
private void open(View view) {
view.setVisibility(View.VISIBLE);
ValueAnimator animator = createDropAnimator(view, 0, hidviewHeight);
Log.e("lc---", hidviewHeight +"hidviewHeight---open");
animator.start();
}
private ValueAnimator createDropAnimator(final View view, int start, int end) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(
new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// int value = (Integer)valueAnimator.getAnimatedValue();
int value = (Integer) valueAnimator.getAnimatedValue();// 得到的值
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
// layoutParams.height = Float.floatToIntBits(value);
layoutParams.height = value;
view.setLayoutParams(layoutParams);
}
}
);
return animator;
}
}
除此之外可能的问题,如果imageview的高度不是固定为40,怎么一开始就获取imageview的高度呢?
要知道view.getHeight()方法是view在绘制完成之后才会取得view的真是高度哦,view初始化的时候宽高可都是0哦!!!
http://blog.csdn.net/listening_music/article/details/7210146
http://blog.csdn.net/nailsoul/article/details/25909313
好的,解决完毕.