Get current RotationAngle while Animation is running on Android -
i'm rotating imageview
using animation
defined xml file.
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareinterpolator="false" > <rotate android:interpolator="@android:anim/linear_interpolator" android:pivotx="50%" android:pivoty="50%" android:fromdegrees="0" android:todegrees="90" android:fillafter="true" /> </set>
although imageview
has getrotation()
method , returns first value has been set image object.
is there anyway current rotation degrees using xml animation? if not, should best way go it?
public class customimageview extends imageview { private objectanimator rotationanimator; public customimageview(context context) { super(context); } public customimageview(context context, attributeset attrs) { super(context, attrs); } public customimageview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } @override public void setrotation(float rotation) { super.setrotation(rotation); // } public synchronized void startanimation(int animationduration) { if (rotationanimator == null || !rotationanimator.isrunning()) { keyframe kf0 = keyframe.offloat(0f, 0f); keyframe kf2 = keyframe.offloat(0.5f, 180f); keyframe kf1 = keyframe.offloat(1f, 360f); propertyvaluesholder pvhrotation = propertyvaluesholder.ofkeyframe("rotation", kf0, kf1, kf2); rotationanimator = objectanimator.ofpropertyvaluesholder(this, pvhrotation); rotationanimator.setrepeatcount(objectanimator.infinite); rotationanimator.setinterpolator(new linearinterpolator()); rotationanimator.setduration(animationduration); rotationanimator.start(); } else { // running } }
version 2, since having problems
public class customimageview extends imageview { private objectanimator rotationanimator; private float myrotation; public customimageview(context context) { super(context); } public customimageview(context context, attributeset attrs) { super(context, attrs); } public customimageview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } public void setmyrotation(float rotation) { this.myrotation = rotation; log.d("customimage", "rotation: " + rotation); invalidate(); } public synchronized void startanimation(int animationduration) { if (rotationanimator == null || !rotationanimator.isrunning()) { keyframe kf0 = keyframe.offloat(0f, 0f); keyframe kf2 = keyframe.offloat(0.5f, 180f); keyframe kf1 = keyframe.offloat(1f, 360f); propertyvaluesholder pvhrotation = propertyvaluesholder.ofkeyframe("myrotation", kf0, kf1, kf2); rotationanimator = objectanimator.ofpropertyvaluesholder(this, pvhrotation); rotationanimator.setrepeatcount(objectanimator.infinite); rotationanimator.setinterpolator(new linearinterpolator()); rotationanimator.setduration(animationduration); rotationanimator.start(); } else { // running } } public synchronized void stopanimation() { if (rotationanimator != null) { rotationanimator.cancel(); rotationanimator = null; } } public synchronized boolean getanimationrunning() { return rotationanimator != null && rotationanimator.isrunning(); } @override protected void ondraw(canvas canvas) { canvas.rotate(myrotation, getwidth() / 2.0f, getheight() / 2.0f); super.ondraw(canvas); } }
sample logcat output:
04-22 22:48:55.475 16341-16341/animation.example.com.animationdemo d/customimage﹕ rotation: 358.44 04-22 22:48:55.490 16341-16341/animation.example.com.animationdemo d/customimage﹕ rotation: 0.48000813 04-22 22:48:55.505 16341-16341/animation.example.com.animationdemo d/customimage﹕ rotation: 2.4 04-22 22:48:55.525 16341-16341/animation.example.com.animationdemo d/customimage﹕ rotation: 4.44
i have packed code simple project: link
Comments
Post a Comment