Activity:
import android.app.Activity;
import android.os.Bundle;import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout allView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
allView = (LinearLayout) findViewById(R.id.all_view);
for (int i = 0; i < allView.getChildCount(); i++) {
allView.getChildAt(i).setVisibility(View.GONE);
}
animView(allView.getChildCount() - 1, 200);
}
private void animView(final int position, final long deloy) {
if (position < 0)
return;
View view = allView.getChildAt(position);
view.setVisibility(View.VISIBLE);
TranslateAnimation transAnimation = (TranslateAnimation) AnimationUtils
.loadAnimation(this, R.anim.anim_in_from_top);
transAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
animView(position - 1, deloy / 2);
}
}, deloy / 2);
}
// -deloy / 3 + 100 * position
// (long) -deloy / 3 + 200 * position
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
view.startAnimation(transAnimation);
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/all_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff0000ff"
android:gravity="center"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ffff00ff"
android:gravity="center"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv3"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff00ff00"
android:gravity="center"
android:text="@string/hello_world" />
<TextView
android:id="@+id/tv4"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#ff00ffff"
android:gravity="center"
android:text="@string/hello_world" />
</LinearLayout>
动画文件:
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromYDelta="-100%p"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toYDelta="0%p" >
</translate>
本文介绍了一种使用Android实现动画视图滑动效果的方法,通过创建自定义的LinearLayout和动画逻辑,使得视图在指定位置以平滑的方式显示,并在显示后触发递归调用,依次显示其余视图。

860

被折叠的 条评论
为什么被折叠?



