我可以使用xml文件更改AnimatedVectorDrawable的fillColor.
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/my_svg">
<target
android:animation="@animator/myanimator"
android:name="color" />
</animated-vector>
<?xml version="1.0" encoding="utf-8"?>
<set>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:propertyName="fillColor"
android:valueType="intType"
android:interpolator="@android:interpolator/decelerate_cubic"
android:valueFrom="@color/blue"
android:valueTo="@color/green" />
</set>
Run Code Online (Sandbox Code Playgroud)
mImageView = (ImageView)findViewById(R.id.ImageView);
Drawable drawable = mImageView.getDrawable();
if (drawable instanceof Animatable) {
((Animatable) drawable).start();
}
Run Code Online (Sandbox Code Playgroud)
但有没有办法在运行时改变颜色?
android android-drawable android-5.0-lollipop animatedvectordrawable
我试图AnimatedVectorDrawable
用作放置在窗口背景中的启动动画。我使用https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable.html中给出的官方示例。它出现但没有动画。
Window
可以在后台进行动画吗?
animation android android-vectordrawable animatedvectordrawable
我正在尝试为我的 android 应用程序中的不同路径设置动画矢量路径以进行测试,但它无法正常工作。屏幕上不显示动画,也不显示任何动画。
我的矢量文件是:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="8dp"
android:height="5dp"
android:viewportWidth="8"
android:viewportHeight="5">
<path
android:name="redot"
android:pathData="M2.5,2.5L6,2.5"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="#E61B1B"
android:fillType="evenOdd"
android:strokeLineCap="round"/>
</vector>
Run Code Online (Sandbox Code Playgroud)
我的 VectorAnimation 文件是:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:targetApi="lollipop"
android:drawable="@drawable/ic_reddot">
<target
android:animation="@anim/redanim"
android:name="redot"/>
</animated-vector>
Run Code Online (Sandbox Code Playgroud)
我在 anim 文件夹中的动画文件是:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="10000"
android:propertyName="pathData"
android:valueFrom="M2.5,2.5L6,2.5"
android:valueTo="M2.5,2.5L31,2.5"
android:valueType="pathType" />
</set>
Run Code Online (Sandbox Code Playgroud)
最后我的 MainActivityCode 如下:
public class MainActivity extends AppCompatActivity {
private TextView testObj;
private ImageView reddot;
private AnimatedVectorDrawable animation;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AnimatedVectorDrawableCompat在我的Android应用中实现动画,以实现与API> = 21的兼容性。
我希望动画在活动期间循环播放。我可以播放动画,并且在API> = 25时也可以正常播放。但是当我在使用API 21至24的设备或仿真器上运行该动画时,我只会看到一次动画。如果我在回调方法中设置一个断点,则会看到它也执行回调,但是动画不会重复。
我发现该动画在另一个线程上运行,因为它不会阻止UI。
这是方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view1);
final AnimatedVectorDrawableCompat anim = AnimatedVectorDrawableCompat.create(this, R.drawable.avd_pass_inside);
imageView.setImageDrawable(anim);
anim.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
anim.start();
}
});
anim.start();
}
Run Code Online (Sandbox Code Playgroud)
根据我的阅读,使用Compat库应该使该功能适用于14以上的所有API级别,但是我什至不必去那儿,因为我的应用程序的其余部分都要求至少达到21。
是否有某种方法(最好是非hacky :))可以在这些API级别上保持一致地工作?是虫子吗?我错过了什么?