且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何逐步从顶部往下显示ImageView的

更新时间:2023-10-22 13:57:34

我不是很熟悉Android的动画,但一(有点hackish的)办法是包装在一个 ClipDrawable 和动画制作的水平值。例如:

I'm not very familiar with android animations, but one(a little hackish) way is to wrap the image in a ClipDrawable and animate its level value. For example:

<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" />

然后在code,你将有:

Then in code you would have:

// 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);

animateImage 的Runnable 目标:

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);
    }
}