在画布上绘制动画(放大和缩小)图像

时间:2021-03-24 13:06:40

标签: java android animation canvas switchcompat

我正在尝试为两个可绘制对象设置动画,例如 enter image description here

这就是我在画布上绘制图像的方式

Drawable d1 = getResources().getDrawable(R.drawable.foobar1, null);
Drawable d2 = getResources().getDrawable(R.drawable.foobar2, null);
d1.setBounds(left, top, right, bottom);
d2.setBounds(left, top, right, bottom);
d1.draw(canvas);
d2.draw(canvas);

我尝试过这种动画制作方法。

   ImageView mThumbDrawable1= new ImageView(context);;
   ImageView mThumbDrawable2= new ImageView(context);
   mThumbDrawable1.setImageDrawable(d1);
   mThumbDrawable2.setImageDrawable(d2);
   Animation zoomin = AnimationUtils.loadAnimation(getContext(), R.anim.zoomin);
   Animation zoomout = AnimationUtils.loadAnimation(getContext(),R.anim.zoomout);
   mThumbDrawable1.setAnimation(zoomin);
   mThumbDrawable2.setAnimation(zoomout);
   mThumbDrawable1.startAnimation(zoomin);
   mThumbDrawable2.startAnimation(zoomout);

zoomin.xml

<?xml version="1.0" encoding="utf-8"?>
<scale  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="0.5"
    android:toXScale="1.5"
    android:fromYScale="0.5"
    android:toYScale="1.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="1000"
    android:fillAfter="true">
</scale>

zoomout.xml

<?xml version="1.0" encoding="utf-8"?>
<scale  xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1.5"
    android:toXScale="0.5"
    android:fromYScale="1.5"
    android:toYScale="0.5"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="2000"
    android:fillAfter="true">
</scale>

但它不起作用。但是如果我使用 view 来设置动画而不是 drawable 它确实有效。喜欢

   Animation zoomin = AnimationUtils.loadAnimation(getContext(), android.R.anim.zoomin);
   Animation zoomout = AnimationUtils.loadAnimation(getContext(), android.R.anim.zoomout);
   this.setAnimation(zoomin);
   this.startAnimation(zoomin);

但这与预期的结果相去甚远。 iam试图在检查和未选中按钮时从一个可绘制的滑动到另一个滑动。任何帮助是极大的赞赏。感谢您阅读我的问题。

0 个答案:

没有答案
相关问题