【问题标题】:Android Animated radial fadeAndroid 动画径向渐变
【发布时间】:2015-08-09 21:37:36
【问题描述】:

我正在尝试为图像设置动画,使其使用从外向内增长的径向渐变淡化为白色。例如,中心为透明且边缘为白色的径向渐变,然后向内折叠直到整个图像为白色。

我试图实现这一点的方法是在我的 ImageView 上覆盖一个 View。我已将 View 的背景设置为一个 drawablexml 文件,我已将其放入我的可绘制文件夹中,其中包含一个简单的白色到透明渐变:

"radialgrad.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="@android:color/transparent"
        android:endColor="@android:color/white"
        android:gradientRadius="1000dp"
        android:type="radial"
        />
</shape>

这是使用它的地方:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:background="@android:color/transparent"
    android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:layout_gravity="center_horizontal">

<ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:id="@+id/ivMain"
        android:src="@drawable/imagetohide"/>


<View
    android:layout_width="300dp"
    android:layout_height="300dp"
    android:layout_marginLeft="6dp"
    android:layout_marginTop="6dp"
    android:id="@+id/vRadial"
    android:background="@drawable/radialgrad"/>
</RelativeLayout>


</FrameLayout>

但我不确定如何从这里开始。在我的活动中,我使用 findViewById 来获取包含渐变的视图,但是如何更改它的背景属性 gradientRadius?我正在尝试使用:

ObjectAnimator anim = ObjectAnimator.ofFloat(vRadial, "gradientRadius", 1000, 0);
        anim.setDuration(1000);
                anim.start();

但它不起作用,而且看起来也不对。该属性属于 vRadial 的背景,而不是 vRadial 本身。

欢迎任何替代建议!

【问题讨论】:

    标签: android animation gradient fade radial-gradients


    【解决方案1】:

    您正在使用渐变属性作为​​ObjectAnimator.ofFloat() 的第二个参数。

    ObjectAnimator 类被设计为对View 属性进行动画处理,例如scaleX、translationX 等。

    你应该使用 'alpha' 属性,应用在 ImageView 上

    ObjectAnimator anim = ObjectAnimator.ofFloat(imgView, "alpha", 1f, 0f);
    

    【讨论】:

    • 感谢您的回复,我了解使用 alpha。但是,这会为整个径向渐变/图像视图的透明度设置动画。我正在寻找创建这种效果:!Image
    • 好的,所以重写 onDraw() 方法并逐帧指定渐变半径可能很有用。换句话说,增加梯度半径,然后调用 invalidate()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多