无缩放动画ImageView宽度

时间:2012-12-13 13:24:13

标签: android android-layout

我正在尝试为ImageView设置动画,以便从左到右慢慢显示。 当I asked this before我无法解释我想要的东西时,所以这一次我使用HTML / JS创建了所需的效果:

http://jsfiddle.net/E2uDE/

在Android中获得此效果的最佳方式是什么?

我尝试更改scaleType,然后将ScaleAnimation直接应用于该ImageView:

布局:

<ImageView
    android:id="@+id/graphImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"
    android:background="@android:color/transparent"
    android:contentDescription="@string/stroom_grafiek"
    />

爪哇:

scale = new ScaleAnimation((float)0,
        (float)1, (float)1, (float)1,
        Animation.RELATIVE_TO_SELF, (float)0,
        Animation.RELATIVE_TO_SELF, (float)1);
scale.setDuration(1000);

graphImage.startAnimation(scale);

但是这个stil会缩放图像。

我也尝试将ImageView包装在FrameLayout中,希望我能为FrameLayout设置动画:

<FrameLayout
    android:layout_width="70dp"
    android:layout_height="fill_parent"
    android:clipChildren="true"">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|left|clip_horizontal" />
</FrameLayout>

这仍然会尝试扩展我的ImageView以适应FrameLayout。

2 个答案:

答案 0 :(得分:9)

有几种方法可以做到这一点 - 一种方法是简单地更改绘制图像的ImageView画布上的剪辑(即允许视图绘制的区域)。缓慢增加绘制边界的右边缘将产生与示例链接中相同的效果。

我写了一些example code来说明这种技巧。如果你下载/构建/运行项目,点击图像将运行揭示动画。

查看CoveredImageView类的onDraw方法,该方法具有以下基本结构:

@Override
protected void onDraw(Canvas canvas) {
    ...
    canvas.getClipBounds(mRect);
    mRect.right = ...;
    canvas.clipRect(mRect);
    ...
    super.onDraw(canvas);
    ...
    invalidate();
}

调整剪辑,然后调用invalidate(),导致再次调用onDraw()。

我还编写了一些代码来插入经过时间的正确剪辑值,这不依赖于任何特定版本的Android。但是,作为一个注释,Android 3.0中有一个新的动画框架和一个ValueAnimator类,它可以帮助执行这些任务。

答案 1 :(得分:0)

您可以将您的imageview与另一个重叠,例如一个带有纯白色drawable的图像。然后,您可以为重叠的imageview设置动画,而不必担心比例,直到它的宽度为0并将其删除。为此,您必须将imageView放在RelativeLayout ofc。

相关问题