android.view.animation.TranslateAnimation Usage, Examples, Demos and Tutorials

..Code Examples of android.view.animation.An animation that controls the position of an object. See the{@link android.view.animation full package} description for details and sample code.

// the TranslateAnimation system doesn’t support device independent pixels.

// So for the Wanda ending and Kabocha endings, in which the game over text

// scrolls in horizontally, compute the size based on the actual density of

// the display and just generate the anim in code.

The Rokudou animation

// can be safely loaded from a file.

Animation gameOverAnim = new TranslateAnimation(startX, 0, 0, 0);

gameOverAnim.setDuration(6000);

gameOverAnim.setFillAfter(true);

gameOverAnim.setFillEnabled(true);

gameOverAnim.setStartOffset(8000);

View background = findViewById(R.id.animation_background);

View foreground = findViewById(R.id.animation_foreground);

View gameOver = findViewById(R.id.game_over);

Animation foregroundAnim = AnimationUtils.loadAnimation(this, R.anim.horizontal_layer2_slide);

Animation backgroundAnim = AnimationUtils.loadAnimation(this, R.anim.horizontal_layer1_slide);

background.startAnimation(backgroundAnim);

foreground.startAnimation(foregroundAnim);

gameOver.startAnimation(gameOverAnim);

mAnimationEndTime = gameOverAnim.getDuration() + System.currentTimeMillis();

} else if (mAnimationType == ROKUDOU_ENDING) {

setContentView(R.layout.rokudou_ending_animation);

View background = findViewById(R.id.animation_background);

View sphere = findViewById(R.id.animation_sphere);

View cliffs = findViewById(R.id.animation_cliffs);

View rokudou = findViewById(R.id.animation_rokudou);

View gameOver = findViewById(R.id.game_over);

Animation backgroundAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_bg);

Animation sphereAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_sphere);

Animation cliffsAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_cliffs);

Animation rokudouAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_slide_rokudou);

Animation gameOverAnim = AnimationUtils.loadAnimation(this, R.anim.rokudou_game_over);

background.startAnimation(backgroundAnim);

sphere.startAnimation(sphereAnim);

cliffs.startAnimation(cliffsAnim);

rokudou.startAnimation(rokudouAnim);

gameOver.startAnimation(gameOverAnim);

mAnimationEndTime = gameOverAnim.getDuration() + System.currentTimeMillis();

} else {

assert false;

}

}

public void onItemSelected(AdapterView parent, View v, int position, long id) {

final View target = findViewById(R.id.target);

final View targetParent = (View) target.getParent();

Animation a = new TranslateAnimation(0.0f,

targetParent.getWidth() – target.getWidth() – targetParent.getPaddingLeft() –

targetParent.getPaddingRight(), 0.0f, 0.0f);

a.setDuration(1000);

a.setStartOffset(300);

a.setRepeatMode(Animation.RESTART);

a.setRepeatCount(Animation.INFINITE);

switch (position) {

case 0:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.accelerate_interpolator));

break;

case 1:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.decelerate_interpolator));

break;

case 2:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.accelerate_decelerate_interpolator));

break;

case 3:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.anticipate_interpolator));

break;

case 4:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.overshoot_interpolator));

break;

case 5:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.anticipate_overshoot_interpolator));

break;

case 6:

a.setInterpolator(AnimationUtils.loadInterpolator(this,

android.R.anim.bounce_interpolator));

break;

}

target.startAnimation(a);

float FontWidth = Common.GetTextWidth(Sentence2, txtLRC2.getTextSize());

layWM.width = dm.widthPixels;

layText2.x = (int) (dm.widthPixels – 75 – FontWidth);

if (FontWidth > dm.widthPixels – 80)

anim = new TranslateAnimation(FontWidth – (dm.widthPixels – 80), 0, 0, 0);

AbsoluteLayout.LayoutParams layText1 = (AbsoluteLayout.LayoutParams) txtLRC1.getLayoutParams();

layText1.x = 5;

layText1.width = (int) Common.GetTextWidth(Sentence1, txtLRC1.getTextSize());

txtLRC1.setLayoutParams(layText1);

layText2.width = (int) FontWidth;

txtLRC2.setLayoutParams(layText2);

if (anim != null && TimeGap != null)

{

anim.setStartOffset((long) (TimeGap * 0.15));

anim.setDuration((long) (TimeGap * 0.75));

txtLRC2.startAnimation(anim);

}

}

else

{

Animation anim = null;

AbsoluteLayout.LayoutParams layText1 = (AbsoluteLayout.LayoutParams) txtLRC1.getLayoutParams();

AbsoluteLayout.LayoutParams layText2 = (AbsoluteLayout.LayoutParams) txtLRC2.getLayoutParams();

float FontWidth1 = Common.GetTextWidth(Sentence1, txtLRC1.getTextSize());

float FontWidth2 = Common.GetTextWidth(Sentence2, txtLRC2.getTextSize());

layWM.width = dm.widthPixels;

if (FontWidth2 > dm.widthPixels – 80)

layText2.x = 5;

else

layText2.x = (int) (dm.widthPixels – 75 – FontWidth2);

if (FontWidth1 > dm.widthPixels – 80)

{

layText1.x = (int) (dm.widthPixels – 75 – FontWidth1);

anim = new TranslateAnimation(FontWidth1 – (dm.widthPixels – 80), 0, 0, 0);

}

else

layText1.x = 5;

txtLRC1.setLayoutParams(layText1);

{

LinearLayout.LayoutParams laySongTitle = (LinearLayout.LayoutParams) txtSongTitle.getLayoutParams();

laySongTitle.width = (int) CurrWidth;

txtSongTitle.setLayoutParams(laySongTitle);

Animation anim = new TranslateAnimation(0, -(CurrWidth – main.getDm().widthPixels + 165), 0, 0);

anim.setDuration((long) (CurrWidth * 15));

anim.setStartOffset(2500);

anim.setRepeatCount(100);

anim.setInterpolator(new LinearInterpolator());

anim.setRepeatMode(Animation.REVERSE);

txtSongTitle.startAnimation(anim);

}

// ????????????????????

CurrWidth = Common.GetTextWidth(txtSongInfo.getText().toString(), txtSongInfo.getTextSize());

if (CurrWidth > main.getDm().widthPixels – 165)

{

LinearLayout.LayoutParams laySongInfo = (LinearLayout.LayoutParams) txtSongInfo.getLayoutParams();

laySongInfo.width = (int) CurrWidth;

txtSongInfo.setLayoutParams(laySongInfo);

Animation anim = new TranslateAnimation(0, -(CurrWidth – main.getDm().widthPixels + 165), 0, 0);

anim.setDuration((long) (CurrWidth * 15));

anim.setStartOffset(2500);

anim.setRepeatCount(100);

anim.setInterpolator(new LinearInterpolator());

anim.setRepeatMode(Animation.REVERSE);

txtSongInfo.startAnimation(anim);

}

// ??????????

btnListPlay.setOnClickListener(new OnClickListener()

final AdServerView adserverView1 = new AdServerView(this,8061,20249);

adserverView1.setId(1);

adserverView1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ApiDemos.BANNER_HEIGHT));

Animation animation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,

Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, -1.0f

);

animation.setDuration(Integer.MAX_VALUE);

animation.setFillAfter(true);

adserverView1.startAnimation(animation);

adserverView1.setOnAdDownload(new OnAdDownload() {

@Override

public void error(String arg0) {

}

@Override

public void end() {

Animation animation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,

Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f

);

animation.setDuration(2000);

adserverView1.startAnimation(animation);

}

@Override

public void begin() {

private Animation createHideAnimation() {

Animation animation = null;

switch (position) {

case TOP:

animation = new TranslateAnimation(0, 0, 0, -contentPlaceHolder

.getHeight());

break;

case RIGHT:

animation = new TranslateAnimation(0, contentPlaceHolder

.getWidth(), 0, 0);

break;

case BOTTOM:

animation = new TranslateAnimation(0, 0, 0, contentPlaceHolder

.getHeight());

break;

case LEFT:

animation = new TranslateAnimation(0, -contentPlaceHolder

.getWidth(), 0, 0);

break;

}

animation.setDuration(animationDuration);

getChildAt(1).setOnKeyListener(l);

}

@Override

protected void onSizeChanged(int w, int h, int oldW, int oldH) {

inAnimUp = new TranslateAnimation(0, 0, h, 0);

inAnimUp.setDuration(ANIM_DURATION);

outAnimUp = new TranslateAnimation(0, 0, 0, -h);

outAnimUp.setDuration(ANIM_DURATION);

inAnimDown = new TranslateAnimation(0, 0, -h, 0);

inAnimDown.setDuration(ANIM_DURATION);

outAnimDown = new TranslateAnimation(0, 0, 0, h);

outAnimDown.setDuration(ANIM_DURATION);

}

void insert(String delta) {

EditText editor = (EditText) getCurrentView();

final AdServerView adserverView1 = (AdServerView)findViewById(R.id.adServerView);

final Animation animation =

AnimationUtils.loadAnimation(getApplicationContext(),

R.animator.ad_in_out);

Animation animation2 = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,

Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, -1.0f

);

animation2.setDuration(Integer.MAX_VALUE);

animation2.setFillAfter(true);

adserverView1.startAnimation(animation2);

adserverView1.setOnAdDownload(new OnAdDownload() {

@Override

public void error(String arg0) {

return fadding;

}

public static Animation translation(float fromX, float toX, float fromY, float toY, long duration, int repeatCount, int repeatMode)

{

TranslateAnimation translation = new TranslateAnimation(fromX,toX,fromY,toY);

translation.setDuration(duration);

if (repeatCount != 0)

translation.setRepeatCount(repeatCount);

if (repeatMode != 0)

translation.setRepeatMode(repeatMode);

return translation;

}

public static AnimationSet elementAlongEdge(int height, int width, int imageSizeX, int imageSizeY, long duration)

{

AnimationSet starAlongEdge = new AnimationSet(true);

final int toX = pos[0];

final int toY = pos[1];

mDraggableImage.setPadding(0, 0, 0, 0);

final TranslateAnimation anim = new TranslateAnimation(Animation.ABSOLUTE, paddingLeft, Animation.ABSOLUTE, toX , Animation.ABSOLUTE, paddingTop, Animation.ABSOLUTE, toY);

anim.setZAdjustment(Animation.ZORDER_TOP);

anim.setDuration(500);

anim.setAnimationListener(aniListener);

mDraggableImage.setPadding(0, 0, 0, 0);

mDraggableImage.startAnimation(anim);

}

final int toX = pos[0];

final int toY = pos[1];

mDraggableImage.setPadding(0, 0, 0, 0);

final TranslateAnimation anim = new TranslateAnimation(Animation.ABSOLUTE, paddingLeft, Animation.ABSOLUTE, toX , Animation.ABSOLUTE, paddingTop, Animation.ABSOLUTE, toY);

anim.setZAdjustment(Animation.ZORDER_TOP);

anim.setDuration(500);

anim.setAnimationListener(aniListener);

mDraggableImage.setPadding(0, 0, 0, 0);

mDraggableImage.startAnimation(anim);

}

}

@Override

public void onSizeChanged(int w, int h, int oldW, int oldH) {

mWidth = w;

inLeft

= new TranslateAnimation(mWidth, 0, 0, 0);

outLeft

= new TranslateAnimation(0, -mWidth, 0, 0);

inRight

= new TranslateAnimation(-mWidth, 0, 0, 0);

outRight = new TranslateAnimation(0, mWidth, 0, 0);

inLeft.setDuration(ANIM_DURATION);

outLeft.setDuration(ANIM_DURATION);

inRight.setDuration(ANIM_DURATION);

outRight.setDuration(ANIM_DURATION);

}

@Override

public void onSizeChanged(int w, int h, int oldW, int oldH) {

mWidth = w;

inLeft

= new TranslateAnimation(mWidth, 0, 0, 0);

outLeft

= new TranslateAnimation(0, -mWidth, 0, 0);

inRight

= new TranslateAnimation(-mWidth, 0, 0, 0);

outRight = new TranslateAnimation(0, mWidth, 0, 0);

inLeft.setDuration(ANIM_DURATION);

outLeft.setDuration(ANIM_DURATION);

inRight.setDuration(ANIM_DURATION);

outRight.setDuration(ANIM_DURATION);

}

@Override

public void onSizeChanged(int w, int h, int oldW, int oldH) {

mWidth = w;

inLeft

= new TranslateAnimation(mWidth, 0, 0, 0);

outLeft

= new TranslateAnimation(0, -mWidth, 0, 0);

inRight

= new TranslateAnimation(-mWidth, 0, 0, 0);

outRight = new TranslateAnimation(0, mWidth, 0, 0);

inLeft.setDuration(ANIM_DURATION);

outLeft.setDuration(ANIM_DURATION);

inRight.setDuration(ANIM_DURATION);

outRight.setDuration(ANIM_DURATION);

}

private void slideDotHome() {

int distanceFromStart = dot.getLeft() – getLeft();

dot.offsetLeftAndRight(-distanceFromStart);

Animation slideBack = new TranslateAnimation(distanceFromStart, 0, 0, 0);

slideBack.setDuration(SLIDE_MILLIS);

slideBack.setInterpolator(new DecelerateInterpolator(SLIDE_ACCEL));

dot.startAnimation(slideBack);

}

private boolean isComplete() {

double dotCenterY = dot.getLeft() + dot.getMeasuredWidth()/2.0;

Animation animation = new AlphaAnimation(0.0f, 1.0f);

animation.setDuration(50);

set.addAnimation(animation);

animation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,

Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f

);

animation.setDuration(100);

set.addAnimation(animation);

Animation animation = new AlphaAnimation(0.0f, 1.0f);

animation.setDuration(50);

set.addAnimation(animation);

animation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,

Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f

);

animation.setDuration(100);

set.addAnimation(animation);

Animation animation = new AlphaAnimation(0.0f, 1.0f);

animation.setDuration(50);

set.addAnimation(animation);

animation = new TranslateAnimation(

Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,

Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f

);

animation.setDuration(100);

set.addAnimation(animation);

Related Class of android.view.animation.TranslateAnimation

Copyright © 2011 www.androidadb.com. All rights reserved. All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc. Contact . See also:

|

|

|

Read More Post