android - How to override setImageResource correctly so that it keeps the rotation of an ImageButton? -
i have created custom view class extends imagebutton. did because there new button class allowed me animations.
the actual problem have following: when click on button, want change image drawable. however, when set new image this
private void setcustombuttonicon() { if (menabled) { mycustombutton.setimageresource(r.drawable.mybutton_off); } else { mycustombutton.setimageresource(r.drawable.mybutton_on); } }
then rotation had, since may have been rotated during animation, not preserved.
now, obvious idea override setimageresource
in custom button class. how can rotation of button preserved?
i think problem related issue:
most likely, using animation class animations. used rotationanimation superclass, did
rotateanimation animation = new rotateanimation( fromdegrees, todegress, pivotx, pivoty);
and applied animation custom button.
however, turns out, only animates bitmap representation of button. or, in other words, actual button/ view not rotated , remains in orientation.
therefore, every call ondraw()
, happens when click on button, "reset look" of button.
so, want use objectanimator
. animate whole view , fix problem preserving state of object.
you can use animator this, example:
objectanimator.offloat(mybutton, "rotation", fromdegrees, -todegrees) .setduration(animationduration) .start();
note: objectanimator
added in api 11. if targeting older versions should use jake whartons nineoldandroids library. (download .jar
file , add libs folder in project.)
Comments
Post a Comment