arn*_*321 13 android android-vectordrawable
这是我的build.gradle
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 26
vectorDrawables.useSupportLibrary = true
}
Run Code Online (Sandbox Code Playgroud)
和布局的一部分
<ImageView
android:id="@+id/recents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:scaleType="fitCenter"
app:srcCompat="@drawable/anim_test"/>
Run Code Online (Sandbox Code Playgroud)
和类演员:
val np = convertView.findViewById<ImageView>(R.id.recents)
val anim = np.drawable as AnimatedVectorDrawableCompat
Run Code Online (Sandbox Code Playgroud)
这在Lolipop(sdk 21)上按预期工作,但在Nougat上失败说:
android.graphics.drawable.AnimatedVectorDrawable cannot be cast to android.support.graphics.drawable.AnimatedVectorDrawableCompat
Run Code Online (Sandbox Code Playgroud)
我没有得到的是,为什么当系统已经支持AnimatedVectorDrawable时,它在sdk级别21上返回AnimatedVectorDrawableCompat.为什么它会在Nougat中返回AnimatedVectorDrawable而不管指定vectorDrawables.useSupportLibrary = true.
Saf*_*han 10
简短答案:
使用AnimatedVectorDrawableCompat.registerAnimationCallback静态方法,它将为您完成工作。
(drawable as Animatable).start()
AnimatedVectorDrawableCompat.registerAnimationCallback(
drawable,
object : Animatable2Compat.AnimationCallback() {
override fun onAnimationEnd(drawable: Drawable?) {
postOnAnimation {
(drawable as Animatable).start()
}
}
})
Run Code Online (Sandbox Code Playgroud)
长答案:
当我尝试循环动画可绘制矢量时,我遇到了同样的问题。直到我发现支持库在不同的SDK级别返回了不同的类(AnimatedVectorDrawable和AnimatedVectorDrawableCompat)。
除了Nick Butcher的精彩博客文章,其他地方都没有记录:
https://medium.com/androiddevelopers/re-animation-7869722af206
他说:
有趣的是,尽管在API 21中引入了该类,但支持库当前仍使用API 24+上的本机版本和compat版本,这使它能够为API 21-23提供错误修复程序。
在博客文章中,作者还建议了其他方法来解决此问题,例如使用AnimatedVectorDrawableCompat#create方法和在运行时设置可绘制对象。
我建议您阅读全文。
希望这可以帮助。
无需进行检查,API<21您可以强制转换为Animatable两者AnimatedVectorDrawable并AnimatedVectorDrawableCompat实施
var anim = mImageView.drawable as Animatable
anim.start()
Run Code Online (Sandbox Code Playgroud)
小智 5
我这样处理:
public class MainActivity extends AppCompatActivity {
ImageView img;
Button show,play,stop;
AnimatedVectorDrawableCompat anim_show,anim_play,anim_stop;
Object canim_show,canim_play,canim_stop;
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
show = findViewById(R.id.show);
play = findViewById(R.id.play);
stop = findViewById(R.id.stop);
if(Build.VERSION.SDK_INT<21){
anim_show = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_show_animated_vector);
anim_play = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_play_animated_vector);
anim_stop = (AnimatedVectorDrawableCompat) getResources().getDrawable(R.drawable.xunfei_stop_animated_vector);
}else{
canim_show = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_show_animated_vector);
canim_play = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_play_animated_vector);
canim_stop = (AnimatedVectorDrawable) getResources().getDrawable(R.drawable.xunfei_stop_animated_vector);
}
show.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
if(Build.VERSION.SDK_INT<21){
img.setImageDrawable(anim_show);
anim_show.start();
}else{
img.setImageDrawable((AnimatedVectorDrawable) canim_show);
((AnimatedVectorDrawable)canim_show).start();
}
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2092 次 |
| 最近记录: |