You can animate final objects. Reading your code by email is a bit
difficult but from glancing at it, it looks like you set a duration of
0ms. You initialize the circumference field only after the first
layout, but you start the animation immediately. Remove the
setDuration() call to the ObjectAnimator to see if it helps (there is
a default duration if you don't call setDuration()).

On Sun, May 20, 2012 at 11:19 AM, duadinam <[email protected]> wrote:
> I'm new to Android development, and I've run into a problem with a personal
> project I'm working on.  I'm making a rotary dialer app, and I've opted to
> implement it using PropertyAnimation. (Specifically, the ObjectAnimator). I
> chose this over drawing with matrices because I wanted to add a 'snap back
> animation' for ACTION_UP events, and I want to be able to stop the user from
> making a full 360 rotation. Touch events are translated into frames of the
> ObjectAnimators animation. For some reason, the dailer just doesn't spin.
>  However, values from my Logcat messages look pretty good.  I've posted my
> code, and some sample values from my Logcat, and I'll gladly send more
> Logcats if you like.  My Logcat just had a litter of kittens and I can't
> keep them all!
>
>
> (You'll notice that I've instainted the 'wheel view' twice.  I need to
> declare the wheelview as final for the ViewTreeObserver.  I thought maybe
> you couldn't do property animation on a final object; so I declared one for
> sizing and the other for animating. Also, something seems to be a bit off
> with my spin values; the circumference of the circle is 169DIP, but the the
> maximum play time I'm seeing is around 139.  Still all the values are
> positive so I should be seeing animation).
>
> Thank you in advance for your help
>
> package com.david.ozersky.wheel;
>
> import android.animation.ObjectAnimator;
> import android.animation.ValueAnimator;
> import android.animation.ValueAnimator.AnimatorUpdateListener;
> import android.app.Activity;
> import android.graphics.Point;
> import android.graphics.Rect;
> import android.os.Bundle;
> import android.util.Log;
> import android.view.Display;
> import android.view.MotionEvent;
> import android.view.View;
> import android.view.View.OnTouchListener;
> import android.view.ViewTreeObserver;
> import android.view.ViewTreeObserver.OnGlobalLayoutListener;
> import android.widget.ImageView;
> import android.widget.LinearLayout;
>
>
> public class RotaryDialerActivity extends Activity implements
> OnTouchListener, AnimatorUpdateListener {
>
> private static final String TAG = "RotaryDialerActivity";
> private float wheel_center_y;
> private float wheel_center_x;
> //density of pixels per dot of length
> private float SCALE;
> //ImageView of dialer;
>
> private int wheel_width;
> private int wheel_height;
> private double circumference;
> private ObjectAnimator animator;
> private float angleOffset;
> private int radius;
>
> private ImageView dialer;
>
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.main);
>
> SCALE = this.getResources().getDisplayMetrics().density;
>
> init();
>
> }
>
> private void init() {
>
> // final ImageView wheel_to_measure = (ImageView) findViewById(R.id.wheel);
> final ImageView dialer_dims = (ImageView) findViewById(R.id.wheel);
> dialer = (ImageView) findViewById(R.id.wheel);
> ViewTreeObserver dialer_obs = dialer_dims.getViewTreeObserver();
> dialer_obs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
>             @Override
>             public void onGlobalLayout() {
>
>             setWheel_width((int) (dialer_dims.getWidth()/SCALE));
>         setWheel_height((int) (dialer_dims.getHeight()/SCALE));
>         setRadius(getWheel_width()/2);
>         setCircumference(getRadius() * 2 * Math.PI);
>
>                 ViewTreeObserver obs = dialer_dims.getViewTreeObserver();
>                 obs.removeGlobalOnLayoutListener(this);
>             }
>         });
>
>
> final LinearLayout touch_pad = (LinearLayout)
> findViewById(R.id.layout_touch_capture);
> touch_pad.setOnTouchListener(this);
> touch_pad.requestFocus();
>
> //touch_pad covers the entire width of the display, and the height of the
> image. The center of the dailer is in the center of the touchpad.
>         ViewTreeObserver tp_obs = touch_pad.getViewTreeObserver();
>         tp_obs.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
>             @Override
>             public void onGlobalLayout() {
>
>         setWheel_center_y(touch_pad.getMeasuredHeight()/2/SCALE);
>         setWheel_center_x(touch_pad.getMeasuredWidth()/2/SCALE);
>
>                 ViewTreeObserver obs = touch_pad.getViewTreeObserver();
>                 obs.removeGlobalOnLayoutListener(this);
>             }
>         });
>
>         Log.d(TAG, "circumference = " + circumference);
>
> animator = ObjectAnimator.ofFloat(dialer, "rotation", 0f,
> 270f).setDuration((long) circumference * 100);
> animator.addUpdateListener(this);
> animator.start();
>
> }
>
> /**
> * @return the circumference
> */
> public double getCircumference() {
> return circumference;
> }
>
> /**
> * @param circumference the circumference to set
> */
> public void setCircumference(double circumference) {
> this.circumference = circumference;
> }
>
> /**
> * @return the radius
> */
> public int getRadius() {
> return radius;
> }
>
> /**
> * @param radius the radius to set
> */
> public void setRadius(int radius) {
> this.radius = radius;
> }
>
> /**
> * @return the wheel_center_y
> */
> public float getWheel_center_y() {
> return wheel_center_y;
> }
>
> /**
> * @param wheel_center_y the wheel_center_y to set
> */
> public void setWheel_center_y(float wheel_center_y) {
> this.wheel_center_y = wheel_center_y;
> }
>
> /**
> * @return the wheel_center_x
> */
> public float getWheel_center_x() {
> return wheel_center_x;
> }
>
> /**
> * @param wheel_center_x the wheel_center_x to set
> */
> public void setWheel_center_x(float wheel_center_x) {
> this.wheel_center_x = wheel_center_x;
> }
>
> /**
> * @return the wheel_width
> */
> public int getWheel_width() {
> return wheel_width;
> }
>
> /**
> * @param wheel_width the wheel_width to set
> */
> public void setWheel_width(int wheel_width) {
> this.wheel_width = wheel_width;
> }
>
> /**
> * @return the wheel_height
> */
> public int getWheel_height() {
> return wheel_height;
> }
>
> /**
> * @param wheel_height the wheel_height to set
> */
> public void setWheel_height(int wheel_height) {
> this.wheel_height = wheel_height;
> }
>
> private long getRotation(float x, float y) {
>
> long angle = (long) (x + y);
>
> return angle;
> }
>
> @Override
> public boolean onTouch(View v, MotionEvent event) {
>
> float touch_x = event.getX()/SCALE;
> float touch_y = event.getY()/SCALE;
> Log.d(TAG, "touch_x = " + touch_x);
> Log.d(TAG, "touch_y = " + touch_y);
> switch(event.getAction()) {
> case(MotionEvent.ACTION_DOWN):
> if (isInBounds(touch_x, touch_y)) {
> //get the starting 'offset'.  calculate the angle off the initial down
> touch. We will subtract that angle from the overall angle
> //to get the angle of moved arc
> angleOffset = (float) Math.toDegrees(Math.atan2(touch_x - wheel_center_x,
> wheel_center_y - touch_y));
> Log.d(TAG, "angle offset = " + angleOffset);
> } else {
> // if the touch is out of bounds do not update the animation.
>
> }
>
> break;
>
> case(MotionEvent.ACTION_MOVE):
>
> if (isInBounds(touch_x, touch_y)) {
> long newDegree = (long) Math.toDegrees(Math.atan2(touch_x - wheel_center_x,
> wheel_center_y - touch_y));
> Log.d(TAG, "newDegree = " + newDegree);
> long degree = (long) ((long) Math.toDegrees(Math.atan2(touch_x -
> wheel_center_x, wheel_center_y - touch_y))  - angleOffset);
> Log.d(TAG, "angleOffset = " + angleOffset);
> Log.d(TAG, "degree = " + degree);
> if (degree < 0) {
> //If the degree is negative use complimentary angle
> degree = 360 - Math.abs(degree);
> }
>    seek(degree * 360/circumference * 100);
> }
> break;
> case(MotionEvent.ACTION_UP):
>
> double degreeOffset = Math.toDegrees(Math.atan2(touch_x - wheel_center_x,
> wheel_center_y - touch_y)  - angleOffset);
> Log.d(TAG, "degreeOffset in ACTION_UP= " + degreeOffset);
> Log.d(TAG, "currentPlayTime = " + animator.getCurrentPlayTime());
> // animator.reverse();
>
> break;
>
> }
> return true;
> }
>
> private void seek(double frame) {
> animator.setCurrentPlayTime((long) frame);
> }
>
> private boolean isInBounds(float touch_x, float touch_y) {
> // TODO Auto-generated method stub
> return true;
> }
>
> @Override
> public void onAnimationUpdate(ValueAnimator animator) {
> Log.d(TAG, "animator playtime = " + animator.getCurrentPlayTime());
>
> }
>
> }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en



-- 
Romain Guy
Android framework engineer
[email protected]

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to