Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does TranslateAnimation work on Android?

Tags:

I went through

TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) 

but am still confused about how Translate animation works.

Can someone please explain how it works? I read the docs which say

fromXDelta  Change in X coordinate to apply at the start of the animation toXDelta    Change in X coordinate to apply at the end of the animation fromYDelta  Change in Y coordinate to apply at the start of the animation toYDelta    Change in Y coordinate to apply at the end of the animation  

but it is still not clear to me how it works.

EDIT: I have a Button and a LinearLayout without any children. When I am clicking on the Button I want to dynamically generate a TextView and animate that TextView to appear in the LinearLayout. The number of TextViews will depend upon the number of clicks on the Button.

like image 692
Yasir Khan Avatar asked Jul 16 '12 12:07

Yasir Khan


People also ask

What is Tweened animation in Android?

Tween Animation takes some parameters such as start value , end value, size , time duration , rotation angle e.t.c and perform the required animation on that object. It can be applied to any type of object. So in order to use this , android has provided us a class called Animation.

What are two different types of view animations?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .


2 Answers

AFAIK,there would be relative connection between this.

That is,if you want to translate a hidden textview from right of screen to left of screen,on click of a button,you actually need to translate it from 100% of X-direction(right side of screen) to 0% of X-direction(left side of screen).

At this point,you don't need to change Y-direction at all.so that would be 0% for both the options.So finally,you will have:

fromXDelta 100%

toXDelta 0%

fromYDelta 0%

toYDelta 0%

you can restrict view of the component by setting this percentages between 0 to 100,as per your requirement.

Similarly,if you need to translate your component on Y-direction as well,then you need to change 0% to some other value.

Hope,its clear to you now.

EDIT :

for your requirement,you need to override onclick of button-1,and there you can control button-2's visibility as well as translation.

create animation file in anim folder in your res.

translate_button.xml:

<?xml version="1.0" encoding="utf-8"?> <!-- translating button from right to left --> <set xmlns:android="http://schemas.android.com/apk/res/android"     android:shareInterpolator="false">     <translate         android:fromXDelta="100%" android:toXDelta="0%"         android:fromYDelta="0%" android:toYDelta="0%"         android:duration="900"     /> </set> 

now,in your activity file,

...  // ll  is linear layout containing button_2 //counter is used to manage visibility of button_2 on click of button_1,i.e.1st click-button_2 would be visible,on 2nd click on button_1,it would be invisible.  //you can change behavior as per your need  button_2.setVisibility(View.GONE); button_1.setOnClickListener(new OnClickListener() {      @Override     public void onClick(View arg0) {          if(counter<1)         {             counter++;                               button_2.setVisibility(View.VISIBLE);             Animation anim=AnimationUtils.loadAnimation(context, R.anim.translate_button);             button_2.startAnimation(anim);         }         else         {             counter=0;             button_2.setVisibility(View.GONE);         }     } }); ll.setOnClickListener(new OnClickListener() {      @Override     public void onClick(View arg0) {          if(counter==1)         {             counter=0;             button_2.setVisibility(View.GONE);         }     } });  ... 
like image 106
Hiral Vadodaria Avatar answered Oct 05 '22 20:10

Hiral Vadodaria


With TranslateAnimation you can create an animation to control an object.

With TranslateAnimation you're able to control the position of an object. You pass this 4 parameters, which stand for the X and Y coordinates.

By Example you want to move an object to the right, you would do something like: TranslateAnimation(0.0f, 1.0f, 0.0f, 0.0f)

(or use Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF)

We only use the X coordinate now, because we are doing now an easy "LeftToRight" animation-move.

Change in X coordinate to apply at the start of the animation toXDelta (0.0f)      Change in X coordinate to apply at the end of the animation (1.0f) 

= 1 to right

Maybe take a look at http://en.wikipedia.org/wiki/Coordinate_system

like image 20
eMi Avatar answered Oct 05 '22 21:10

eMi