Program Translation View Pre-Honeycomb

How can I programmatically translate an arbitrary view without using animation ( android.view.animation.* )?

API 11 introduced setTranslationX and setTranslationY , setX and setY , and getMatrix - all of which can be used to accomplish what I'm looking for. However, I cannot find the equivalent at previous API levels.

android.view.animation.TranslateAnimation uses getMatrix in its implementation (back in API 4), but during that time it was a private API.

Is there a way to do this without resorting to thought?

+49
android android-layout
Jan 04 2018-12-12T00:
source share
7 answers

In favorites, because I would like to be proved wrong on this, but I came across a similar wall when I jumped back several times.

I'm sure you are stuck with offsetTopAndBottom() and offsetLeftAndRight() to move the View around without animation in the early APIs. The big drawback is that these methods actually change the top / bottom / left / right values, so you need to do some pretty heavy tracking if you want to get back to where you started.

Another option would be to simply use TranslateAnimation with a VERY short duration and set to fillAfter ...

Other parameters require subclassification and are still not very good, for example, when translating Canvas View . The problem here is that you also have to create a parent who does not loop it over children so that the view can draw beyond its own boundaries.

+14
Jan 04 2018-12-21T00:
source share

Yes, you can use TranslateAnimation. With fillAfter set to true, your view will remain translated even after the animation finishes. Here is an example:

 TranslateAnimation anim=new TranslateAnimation(0, 0, 20, 20); anim.setFillAfter(true); anim.setDuration(0); yourView.startAnimation(anim); 
+11
Jan 04 2018-12-21T00:
source share

Grishkaโ€™s answer helped me a lot, it works great. To animate a translation using the Honeycomb API using nine-species, I set the FloatEvaluator to the translation value, and then used either setTranslationX on Honeycomb + or the TranslateAnimation method. Here is my code:

 public class TranslationXEvaluator extends FloatEvaluator { private View view; public TranslationXEvaluator(View view) { this.view = view; } @Override public Float evaluate(float fraction, Number startValue, Number endValue) { float translationX = super.evaluate(fraction, startValue, endValue); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { view.setTranslationX(translationX); } else { TranslateAnimation anim = new TranslateAnimation(translationX, translationX, 0, 0); anim.setFillAfter(true); anim.setDuration(0); view.startAnimation(anim); } return translationX; } } 
+6
Jul 31 2018-12-12T00:
source share

I had the same problem. I managed to get the translation by manipulating the layouts, in particular the fields, the views that I wanted to translate. You can use negative values โ€‹โ€‹for btw fields.

+1
Apr 19 2018-12-12T00:
source share

One alternative to TranslateAnimation is to subclass View and translate Canvas to dispatchDraw. Something like:

  public class TranslateView extends View { private float mTranslationX = 0; private float mTranslationY = 0; private boolean mWillUseSDKTranslation = true; @Override protected void dispatchDraw(final Canvas canvas) { if (mWillUseSDKTranslation == false) { // keep track of the current state canvas.save(Canvas.MATRIX_SAVE_FLAG); // translate canvas.translate(mTranslationX, mTranslationY); // draw it super.dispatchDraw(canvas); // restore to the previous state canvas.restore(); } else { super.dispatchDraw(canvas); } } public void setTranslationValue(final float aTranslationX, final float aTranslationY) { if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { mWillUseSDKTranslation = false; mTranslationX = aTranslationX; mTranslationY = aTranslationY; invalidate(); } else { setTranslationX(aTranslationX); setTranslationY(aTranslationY); } } } 
+1
Mar 11 '14 at 10:59
source share
0
Jan 26 '14 at 22:14
source share

The workaround I found was to set the negative fields using:

 layoutParams.setMargins(left, top, right, bottom); yourView.setLayoutParams(layoutParams); 
0
Apr 17 '15 at 21:49
source share



All Articles