【发布时间】: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