Layout position change with animation in android - android

In my application I have made chathead
I can change position of this chathead using
windowManager.updateViewLayout(chatHead, params);
But it does not change position with animation.
How can I get animation on move
I have tried following but it does not work.
TranslateAnimation anim = new TranslateAnimation( 0, params.x, 0, params.y);
anim.setDuration(1000);
chatHead.startAnimation(anim);

Have you checked the params.x and params.y value is correct or not?

TranslateAnimation anim = new new TranslateAnimation( 0, params.x, 0, params.y);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)chatHead.getLayoutParams();
**//set margin according to your calculation**
params.topMargin = mTopMargin;
params.leftMargin = 0;
chatHead.setLayoutParams(params);
});
chatHead.startAnimation(anim);
}
or
use anim.setFillAfter(true);

Related

is there a TranslateAnimation onComplete?

I am animating a view to move off the screen, on complete i would like to call another function but I cant seem to find a onComplete method.
int originalPos[] = new int[2];
icons.getLocationOnScreen( originalPos );
layoutMoved = originalPos[0]+icons.getWidth();
TranslateAnimation anim = new TranslateAnimation( 0, -layoutMoved , 0, 0);
anim.setDuration(500);
anim.setFillAfter( true );
icons.startAnimation(anim);
icons.setVisibility(View.GONE);
Set an AnimationListener, you'll have access to OnAnimationEnd.
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// do your stuff
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Android: How to keep Animation after Animation

I'm implementing animation when slide between fragments in ViewPager.
when slide to new fragment I want to scale the view and the make start a set of animation includes alpha and translate animation but when start the set problem is scale animation's result was not keeping (the view return to the original size).
I had tried to with
scaleAnimation.setFillAfter(true);
scaleAnimation.setFillEnabled(true);
But it just work when I do not start set of animation above, When I start the set, the view is return to original size.
Any body can give me a solution?
EDIT:
First thing I want to scale an imageView by animation:
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 1.3f, 1f, 1.3f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 1);
scaleAnimation.setDuration(1000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setFillEnabled(true);
Then I want after scale, ImageView still keep new scale size and start another Animation as:
private void startSetAnimation() {
AnimationSet set = new AnimationSet(true);
set.setDuration(3000);
set.setFillAfter(true);
float alphaS = 1f;
AlphaAnimation alpha = new AlphaAnimation(currentAlpha, alphaS);
alpha.setDuration(3000);
alpha.setFillAfter(true);
float scale = 0.1f;
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, currentTrans,
Animation.RELATIVE_TO_SELF, scale,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF , 0f);
translate.setFillAfter(true);
translate.setDuration(3000);
set.addAnimation(translate);
set.addAnimation(alpha);
ivHorizontal.startAnimation(set);
}
So I add below code to scale animation to make it but not success.
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
startSetAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
ivHorizontal.startAnimation(scaleAnimation);
There is an onAnimationEnd listener you can utilize kick off certain animations when other ones are done. I'll post code when I get to my computer
Edit:
The reason why your imageView doesn't retain its new size after scaling is because Animations aren't permanent, what you need to do is this
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// change the actual width and height so it doesn't go back to what it was
LayoutParams layoutParams = (LayoutParmas) ivHorizontal.getLayoutParams();
layoutParams.width = ivHorizental.getWidth();
layoutParams.height= ivHorizental.getHeight();
ivHorizontal.setLayoutParams(layoutParams);
ivHorizontal.requestLayout();
startSetAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
ivHorizontal.startAnimation(scaleAnimation);

Is it possible to change the destination coordinates of the imageview while animating

In my application i am able to animate the image from source location to destination location and repeating the animation 5 times using animation.setRepeatCount(5);
But i want to change the destination location(yDelta) after each repetition.
For example :
destination Ydelta is 2, after first repetition need to change the ydelta to 3 and next repetition ydelta to 4 so on... up to 5 repetitions....
Below is my animation code:
int sourceCoords[] = new int[2];
int targetCoords[] = new int[2];
int xDelta;
int yDelta;
sourceImage.getLocationOnScreen(sourceCoords);
targetImage.getLocationOnScreen(targetCoords);
xDelta = targetCoords[0] - sourceCoords[0];
yDelta = targetCoords[1] - sourceCoords[1];
sourceImage.bringToFront();
TranslateAnimation animation = new TranslateAnimation(0, xDelta, 0, yDelta);
animation.setDuration(400);
animation.setFillAfter(false);
animation.setRepeatCount(5);
sourceImage.startAnimation(animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(final Animation animation) {
sourceImage.bringToFront();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});

Make an ImageView Visible with timer

I know that this question has been answered, but CountdownTimer doesn't work into my method.
So basically I have an animation for a button and a textview, after a button is clicked. But I want also to have an imageview, with a timer. Because imageview is a part of the textview that I want to show it to the user.
Any help?
This is my code:
onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView2 = (ImageView)findViewById(R.id.imageView2);
edittext = (EditText)findViewById(R.id.edittext);
leatseatbutton = (Button)findViewById(R.id.letseatbutton);
register=(Button)findViewById(R.id.register);
signin1button=(Button)findViewById(R.id.signin1button);
signinbutton_fb=(Button)findViewById(R.id.signinbutton_fb);
cancel=(Button)findViewById(R.id.cancel);
imageView1 = (ImageView)findViewById(R.id.imageView1);
First onClickListener
leatseatbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 175;
params.height = 40;
params.setMargins(75, 240, 0, 0);
leatseatbutton.setLayoutParams(params);
edittext.setVisibility(View.VISIBLE);
TranslateAnimation slide = new TranslateAnimation(0, 0, 100,0 );
slide.setDuration(1000);
slide.setFillAfter(true);
slide.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageView2.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
edittext.startAnimation(slide);
}
});
Second onClickListener
signinbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.width = 175;
params.height = 40;
params.setMargins(75, 240, 0, 0);
signinbutton.setLayoutParams(params);
edittext.setVisibility(View.VISIBLE);
TranslateAnimation slide = new TranslateAnimation(0, 0, 100,0 );
slide.setDuration(1000);
slide.setFillAfter(true);
edittext.startAnimation(slide);
}
});
}
Maybe you need use AnimationListener
....
TranslateAnimation slide = new TranslateAnimation(0, 0, 100,0 );
slide.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
imageview.setVisible(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
slide.setDuration(1000);
slide.setFillAfter(true);
edittext.startAnimation(slide);
....

How to move and fade out any View with Animation

Ok, so I have a View where its being animated with the following code:
RelativeLayout rl = (RelativeLayout) findViewById(R.id.productPage_parentLayout_RL);
ImageView iv = new ImageView( ProductPage.this );
imageLoader.DisplayImage(FULL_PRODUCT_INFO.getFirstImage(), iv);
Animation animation = new TranslateAnimation(0, helper.getDeviceWidth() - 100,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
rl.addView(iv);
With this code, the view starts from the left side of the screen and moves to almost the end of the screen, but I also want to fade-out the view and hide/destroy it in the end. I tried to search anything related to this, but couldn't find any.
Any help is appreciated.
Try this
ObjectAnimator move=ObjectAnimator.ofFloat(iv, "translationY",100f);
move.setDuration(3000);
ObjectAnimator alpha1=ObjectAnimator.ofFloat(iv, "alpha",0.5f);
alpha1.setDuration(1000);
ObjectAnimator alpha2=ObjectAnimator.ofFloat(iv, "alpha",0);
alpha2.setDuration(2000);
AnimatorSet animset=new AnimatorSet();
animset.play(alpha2).before(alpha1).with(move);
animset.start();
Animation a = new AlphaAnimation(1.0f, 0.0f);
a.setDuration(1000);
a.setFillAfter(true);
iv.startAnimation(a);
AnimationSet set = new AnimationSet(true);
set.addAnimation(animation)
set.addAnimation(a)
set.setFillAfter(true);
iv.startAnimation(set);
set.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
iv.setVisibility(View.INVISIBLE);
}
});

Categories

Resources