如何从上到下逐步显示ImageView

时间:2012-08-26 03:52:24

标签: android android-imageview

有没有办法从上到下逐步显示ImageView, 像这样:

enter image description here

抱歉蹩脚的动画。

1 个答案:

答案 0 :(得分:26)

我对android动画并不是很熟悉,但是一种(一种小的hackish)方式是将图像包装在ClipDrawable中并为其level值设置动画。例如:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/clip_source" />

clip_source是可绘制的:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:drawable="@drawable/your_own_drawable"
    android:gravity="bottom" />

然后在代码中你会:

// a field in your class
private int mLevel = 0;

ImageView img = (ImageView) findViewById(R.id.imageView1);
mImageDrawable = (ClipDrawable) img.getDrawable();
mImageDrawable.setLevel(0);
mHandler.post(animateImage);

animateImageRunnable对象:

private Runnable animateImage = new Runnable() {

        @Override
        public void run() {
            doTheAnimation();
        }
    };

doTheAnimation方法:

private void doTheAnimation() {
    mLevel += 1000;
    mImageDrawable.setLevel(mLevel);
    if (mLevel <= 10000) {
        mHandler.postDelayed(animateImage, 50);
    } else {
        mHandler.removeCallbacks(animateImage);
    }
}