Rotating ImageView in Android < API Level 11

2019-01-28 00:01发布

问题:

So in API Level 11 Google introduced the ability to rotate an ImageView (Yay, after they introduced the possibility to Animate such a rotation, yay smart thinking, yay!)

But how should I go about to rotate an ImageView using e.g. API level 8? I can't use setRotation() as described above.

回答1:

RotationAnimation was present since Api level 1

RotateAnimation animation = new RotateAnimation(from, to,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(1);
animation.setFillAfter(true);

imageView.startAnimation(animation );


回答2:

I started with creating BitMap and rotating the canvas/matrix, however this was not a good solution. Finally ended up just swapping the drawable if conditions are met. I should say this is an ExpandableListView where cells are reused when drawing.

if (isExpanded) {
        ImageView view = (ImageView) convertView.findViewById(R.id.ImageView);
        view.setImageResource(R.drawable.quickactions_button_normal_down);
    }

    if (!isExpanded) {
        ImageView view = (ImageView) convertView.findViewById(R.id.ImageView);          
        view.setImageResource(R.drawable.quickactions_button_normal);
    }

I'm not usually a Android developer but I'm really amazed that it is possible to animate a rotation, but not statically set the rotation of a drawable. Logically the first is a subset of the second and not the other way around.