Permalink
Browse files

Added support for animating the translationX and translationY values …

…of a View.
  • Loading branch information...
1 parent 9c9e925 commit dc05cf6c9010b3cf50ff0225723b7e47285dfba2 Phil Goodwin committed Oct 12, 2012
@@ -1,19 +1,59 @@
package com.xtremelabs.robolectric;
import android.accounts.AccountManager;
-import android.app.*;
+import android.animation.ObjectAnimator;
+import android.animation.ValueAnimator;
+import android.app.Activity;
+import android.app.ActivityGroup;
+import android.app.ActivityManager;
+import android.app.AlarmManager;
+import android.app.AlertDialog;
+import android.app.Application;
+import android.app.Dialog;
+import android.app.KeyguardManager;
+import android.app.ListActivity;
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.app.ProgressDialog;
+import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
-import android.content.*;
+import android.content.ContentProviderOperation;
+import android.content.ContentProviderResult;
+import android.content.ContentResolver;
+import android.content.ContentValues;
+import android.content.Context;
+import android.content.ContextWrapper;
+import android.content.Intent;
+import android.content.UriMatcher;
import android.content.pm.ResolveInfo;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.database.CursorWrapper;
-import android.database.sqlite.*;
-import android.graphics.*;
-import android.graphics.drawable.*;
+import android.database.sqlite.SQLiteCursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.database.sqlite.SQLiteOpenHelper;
+import android.database.sqlite.SQLiteProgram;
+import android.database.sqlite.SQLiteQueryBuilder;
+import android.database.sqlite.SQLiteStatement;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.ColorMatrix;
+import android.graphics.Matrix;
+import android.graphics.Paint;
+import android.graphics.Path;
+import android.graphics.Rect;
+import android.graphics.drawable.AnimationDrawable;
+import android.graphics.drawable.BitmapDrawable;
+import android.graphics.drawable.ColorDrawable;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
+import android.graphics.drawable.ShapeDrawable;
+import android.graphics.drawable.StateListDrawable;
import android.hardware.Camera;
import android.hardware.SensorManager;
import android.location.Address;
@@ -28,8 +68,25 @@
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
-import android.os.*;
-import android.preference.*;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.os.CountDownTimer;
+import android.os.Handler;
+import android.os.HandlerThread;
+import android.os.Looper;
+import android.os.Message;
+import android.os.Parcel;
+import android.os.PowerManager;
+import android.os.ResultReceiver;
+import android.os.Vibrator;
+import android.preference.DialogPreference;
+import android.preference.EditTextPreference;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceCategory;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceScreen;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
@@ -47,11 +104,57 @@
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.SparseIntArray;
-import android.view.*;
-import android.view.animation.*;
+import android.view.Display;
+import android.view.GestureDetector;
+import android.view.LayoutInflater;
+import android.view.MenuInflater;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewConfiguration;
+import android.view.ViewGroup;
+import android.view.ViewTreeObserver;
+import android.view.animation.AlphaAnimation;
+import android.view.animation.Animation;
+import android.view.animation.AnimationSet;
+import android.view.animation.AnimationUtils;
+import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
-import android.webkit.*;
-import android.widget.*;
+import android.webkit.CookieManager;
+import android.webkit.CookieSyncManager;
+import android.webkit.JsPromptResult;
+import android.webkit.JsResult;
+import android.webkit.MimeTypeMap;
+import android.webkit.SslErrorHandler;
+import android.webkit.WebView;
+import android.widget.AbsListView;
+import android.widget.AbsSeekBar;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.BaseAdapter;
+import android.widget.CheckedTextView;
+import android.widget.CursorAdapter;
+import android.widget.ExpandableListView;
+import android.widget.Filter;
+import android.widget.FrameLayout;
+import android.widget.Gallery;
+import android.widget.GridView;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.PopupWindow;
+import android.widget.ProgressBar;
+import android.widget.RatingBar;
+import android.widget.RemoteViews;
+import android.widget.ResourceCursorAdapter;
+import android.widget.ScrollView;
+import android.widget.SeekBar;
+import android.widget.SimpleCursorAdapter;
+import android.widget.TabHost;
+import android.widget.TextView;
+import android.widget.Toast;
+import android.widget.VideoView;
+import android.widget.ViewAnimator;
+import android.widget.ViewFlipper;
+import android.widget.ZoomButtonsController;
import com.xtremelabs.robolectric.bytecode.DirectCallPolicy.FullStackDirectCallPolicy;
import com.xtremelabs.robolectric.bytecode.RobolectricInternals;
import com.xtremelabs.robolectric.bytecode.ShadowWrangler;
@@ -254,6 +357,7 @@ public static void logMissingInvokedShadowMethods() {
ShadowNotificationManager.class,
ShadowNetworkInfo.class,
ShadowOverlayItem.class,
+ ShadowObjectAnimator.class,
ShadowPagerAdapter.class,
ShadowPaint.class,
ShadowPair.class,
@@ -331,6 +435,7 @@ public static void logMissingInvokedShadowMethods() {
ShadowTypedValue.class,
ShadowUriMatcher.class,
ShadowURLSpan.class,
+ ShadowValueAnimator.class,
ShadowVibrator.class,
ShadowVideoView.class,
ShadowView.class,
@@ -753,6 +858,10 @@ public static ShadowNetworkInfo shadowOf(NetworkInfo instance) {
return (ShadowNetworkInfo) shadowOf_(instance);
}
+ public static ShadowObjectAnimator shadowOf(ObjectAnimator instance) {
+ return (ShadowObjectAnimator) shadowOf_(instance);
+ }
+
public static ShadowNotification shadowOf(Notification other) {
return (ShadowNotification) Robolectric.shadowOf_(other);
}
@@ -962,6 +1071,10 @@ public static ShadowUriMatcher shadowOf(UriMatcher instance) {
return (ShadowUriMatcher) shadowOf_(instance);
}
+ public static ShadowValueAnimator shadowOf(ValueAnimator instance) {
+ return (ShadowValueAnimator) shadowOf_(instance);
+ }
+
public static ShadowView shadowOf(View instance) {
return (ShadowView) shadowOf_(instance);
}
@@ -0,0 +1,97 @@
+package com.xtremelabs.robolectric.shadows;
+
+import android.animation.ObjectAnimator;
+import android.os.Handler;
+import android.os.Looper;
+import com.xtremelabs.robolectric.internal.Implementation;
+import com.xtremelabs.robolectric.internal.Implements;
+import com.xtremelabs.robolectric.internal.RealObject;
+
+import java.lang.reflect.Method;
+
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+
+
+@SuppressWarnings({"UnusedDeclaration"})
+@Implements(ObjectAnimator.class)
+public class ShadowObjectAnimator extends ShadowValueAnimator {
+ @RealObject
+ private ObjectAnimator realObject;
+ private Object target;
+ private String propertyName;
+ private float[] floatValues;
+ private Class<?> animationType;
+
+ @Implementation
+ public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) {
+ ObjectAnimator result = new ObjectAnimator();
+
+ result.setTarget(target);
+ result.setPropertyName(propertyName);
+ result.setFloatValues(values);
+ shadowOf(result).setAnimationType(float.class);
+
+ return result;
+ }
+
+ private void setAnimationType(Class<?> type) {
+ animationType = type;
+ }
+
+ @Implementation
+ public void setTarget(Object target) {
+ this.target = target;
+ }
+
+ @Implementation
+ public Object getTarget() {
+ return target;
+ }
+
+ @Implementation
+ public void setPropertyName(String propertyName) {
+ this.propertyName = propertyName;
+ }
+
+ @Implementation
+ public String getPropertyName() {
+ return propertyName;
+ }
+
+ @Implementation
+ public void setFloatValues(float... values) {
+ this.floatValues = values;
+ }
+
+ @Implementation
+ public ObjectAnimator setDuration(long duration) {
+ this.duration = duration;
+ return realObject;
+ }
+
+ @Implementation
+ public void start() {
+ String methodName = "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
+ final Method setter;
+ try {
+ setter = target.getClass().getMethod(methodName, animationType);
+ if (animationType == float.class) {
+ setter.invoke(target, floatValues[0]);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ if (animationType == float.class) {
+ setter.invoke(target, floatValues[floatValues.length - 1]);
+ }
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+ }, duration);
+ }
+}
@@ -0,0 +1,16 @@
+package com.xtremelabs.robolectric.shadows;
+
+import android.animation.ValueAnimator;
+import com.xtremelabs.robolectric.internal.Implementation;
+import com.xtremelabs.robolectric.internal.Implements;
+
+@Implements(ValueAnimator.class)
+public class ShadowValueAnimator {
+ protected long duration;
+
+ // Tested via ObjectAnimatorTest for now
+ @Implementation
+ public long getDuration() {
+ return duration;
+ }
+}
@@ -7,8 +7,13 @@
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
-import android.view.*;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
+import android.view.View;
import android.view.View.MeasureSpec;
+import android.view.ViewGroup;
+import android.view.ViewParent;
+import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.internal.Implementation;
@@ -78,6 +83,8 @@
private CharSequence contentDescription = null;
private int measuredWidth = 0;
private int measuredHeight = 0;
+ private float translationX = 0.0f;
+ private float translationY = 0.0f;
public void __constructor__(Context context) {
__constructor__(context, null);
@@ -949,6 +956,26 @@ public ViewTreeObserver getViewTreeObserver() {
public void onAnimationEnd() {
}
+ @Implementation
+ public void setTranslationX(float translationX) {
+ this.translationX = translationX;
+ }
+
+ @Implementation
+ public float getTranslationX() {
+ return translationX;
+ }
+
+ @Implementation
+ public void setTranslationY(float translationY) {
+ this.translationY = translationY;
+ }
+
+ @Implementation
+ public float getTranslationY() {
+ return translationY;
+ }
+
/*
* Non-Android accessor.
*/
Oops, something went wrong.

0 comments on commit dc05cf6

Please sign in to comment.