【发布时间】:2018-12-14 12:26:40
【问题描述】:
我正在尝试将矢量路径动画化到我的 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>
我的 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>
我在 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>
最后我的 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);
// testObj = (TextView) findViewById(R.id.testObj);
// testObj.setVisibility(View.INVISIBLE);
reddot = (ImageView) findViewById(R.id.reddot);
Drawable d = reddot.getBackground();
if (d instanceof AnimatedVectorDrawable) {
Log.d("testanim", "onCreate: instancefound" );
animation = (AnimatedVectorDrawable) d;
animation.start();
}
}
}
【问题讨论】:
-
有错误信息可以分享吗?
-
我告诉他们没有错误信息
-
尝试使用
reddot.getDrawable(); -
试过了,它返回null,因为vectoranimator文件作为背景添加到mainactivity xml中的imageview
标签: android android-vectordrawable animatedvectordrawable