translation animation issue - android

I want to make a layout animated through translation animation from screen's particular X Y position. and this is happening very correcty. now the problem starts when animations completes the layout again go to screen's top rihgt corner. i want the layout stay at the same position on which the animation ends.
AnimationListener animationListener = new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
drawer_layout.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationStart(Animation animation) {
drawer_layout.setVisibility(View.VISIBLE);
}
};
final Animation animTranslate = AnimationUtils.loadAnimation(this,
R.anim.top_to_bottom_in);
animTranslate.setAnimationListener(animationListener);
drawer_layout = (LinearLayout) findViewById(R.id.drawer_layout);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// bitmapBackground = CaptureBackground();
faded_layout.setBackgroundColor(Color.parseColor("#50ffffff"));
// HandleDropAnimation(drawer_layout);
drawer_layout.startAnimation(animTranslate);
}
});
and this is my xml.
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromYDelta="25%p"
android:toYDelta="50%p" />

Call
animTranslate.setFillAfter(true);
before you start the animation. According to java docs:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.

use before animation starts
animTranslate.setFillAfter(true);

Related

Slide up animation for selected listview items

I have a listview. I want to add one animation that is if I select one list item then it will be removed and rest of the items below that item will be shifted up with slide up animation. I have done this with linear layout by getting its child position but I am not able to understand how to slide up rest elements of listview. Please help
Below is my code for animation in dynamically created linear layout
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation animation1 = AnimationUtils.loadAnimation(getActivity(), R.anim.move);
final Animation animation2 = AnimationUtils.loadAnimation(getActivity(), R.anim.moveup);
this is animation of that item on which we click
lay1.startAnimation(animation1);
this is code for animation rest child to slide up
final int i = lay1.getId() + 1;
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
try {
LinearLayout l = (LinearLayout) layoutall.getChildAt(i);
l.startAnimation(animation2);
layoutall.removeView(lay1);
} catch (Exception e) {
//connectToDatabase();
}
}
}, 600);
Updated Code
Thanks a lot. I was able to implement this functionality but problem is that I have used two animation in one listview but not able to implement first animation.
My code for list item to whom I want to swipe left while on click
viewHolder.accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeListItem(viewHolder.order_card_layout, position);
}
});
And code for animation is
protected void removeListItem(final View rowView, final int positon) {
// TODO Auto-generated method stub
final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move);
rowView.startAnimation(animation);
Animation.AnimationListener al = new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
ViewHolder vh = (ViewHolder) rowView.getTag();
//vh.needInflate = true;
}
#Override public void onAnimationRepeat(Animation animation) {}
#Override public void onAnimationStart(Animation animation) {}
};
collapse(rowView, al);
}
but problem is that My first animation is not working that is slide right
final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move);
rowView.startAnimation(animation);
New Updated Code
I was able to implement this functionality but problem is that I have used two animations and want to do first animation on that item whom I click and another animation on all the list items below that clicked item.But my problem is that the item on which I clicked slide right and also it re appears and slide up with whole list but I want that the item on which i click will be slide right and rest will be slide up.
My code for list item to whom I want to swipe left while on click
viewHolder.accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeListItem(viewHolder.order_card_layout, position);
}
});
And code for animation is
protected void removeListItem(final View rowView, final int positon) {
// TODO Auto-generated method stub
final Animation animation = AnimationUtils.loadAnimation(rowView.getContext(), R.anim.move);
rowView.startAnimation(animation);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
try {
Animation.AnimationListener al = new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
ViewHolder vh = (ViewHolder) rowView.getTag();
//vh.needInflate = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
};
collapse(rowView, al);
} catch (Exception e) {
//connectToDatabase();
}
}
}, 600);
}
private void collapse(final View v, Animation.AnimationListener al) {
final int initialHeight = v.getMeasuredHeight();
Animation anim = new Animation() {
#Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
if (interpolatedTime == 1) {
v.setVisibility(View.GONE);
}
else {
v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
v.requestLayout();
}
}
#Override
public boolean willChangeBounds() {
return true;
}
};
if (al!=null) {
anim.setAnimationListener(al);
}
anim.setDuration(600);
v.startAnimation(anim);
}
Here is code related to your requirement , Try this
https://github.com/paraches/ListViewCellDeleteAnimation
Download code and run it.
if you want to see demo than see this video,
http://www.youtube.com/watch?v=bOl5MIti7n0
Here is Code, where i am implementing both animation.
1) Slide from right while click on List-view Item-click.
2) while clicking on Delete Button, slide-up animation for remaining rows in list-view (which is already done in link i have given).
Copy below files to your anim folder.
1) fade_out.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:duration="700"
android:fromAlpha="1"
android:toAlpha="0" />
</set>
2) fade_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:duration="700"
android:fromAlpha="0"
android:toAlpha="1" />
</set>
3) slide_in_right.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="700"
android:fromXDelta="100%"
android:toXDelta="0%" />
</set>
4) slide_out_right.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:duration="700"
android:fromXDelta="0%"
android:toXDelta="100%" />
</set>
Now add below methods and class to your activity,
//Animation
private void fadeOutView(View view) {
Animation fadeOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_out);
if (fadeOut != null) {
fadeOut.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(fadeOut);
}
}
private void fadeInView(View view) {
Animation fadeIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.fade_in);
if (fadeIn != null) {
fadeIn.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
view.setVisibility(View.VISIBLE);
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
}
});
view.startAnimation(fadeIn);
}
}
private void slideInView(View view) {
Animation slideIn = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_in_right);
if (slideIn != null) {
slideIn.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
view.setVisibility(View.VISIBLE);
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
}
});
view.startAnimation(slideIn);
}
}
private void slideOutView(View view) {
Animation slideOut = AnimationUtils.loadAnimation(view.getContext(), R.anim.slide_out_right);
if (slideOut != null) {
slideOut.setAnimationListener(new ViewAnimationListener(view) {
#Override
protected void onAnimationStart(View view, Animation animation) {
}
#Override
protected void onAnimationEnd(View view, Animation animation) {
view.setVisibility(View.GONE);
}
});
view.startAnimation(slideOut);
}
}
private abstract class ViewAnimationListener implements Animation.AnimationListener {
private final View view;
protected ViewAnimationListener(View view) {
this.view = view;
}
#Override
public void onAnimationStart(Animation animation) {
onAnimationStart(this.view, animation);
}
#Override
public void onAnimationEnd(Animation animation) {
onAnimationEnd(this.view, animation);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
protected abstract void onAnimationStart(View view, Animation animation);
protected abstract void onAnimationEnd(View view, Animation animation);
}
Now, if you want to display animation on List Item Click then, give animation in Adapter class in which you have inflate raw_layout for listView.
5) raw_layout.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/linLayout">
<ImageButton
android:id="#+id/cell_trash_button"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/trash_can"
android:scaleType="fitXY" />
<TextView
android:id="#+id/cell_name_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|left"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:text="cell name" />
</LinearLayout>
6) finally Apply animation to your Parent Layout of raw_layout in getView() method of adapter onClick of Parent Layout.Here My parent layout for raw_layout is LinearLayout which have id named linLayout.
vh.linLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (vh.linLayout.getVisibility() == View.VISIBLE) {
fadeOutView(vh.linLayout);
slideInView(vh.linLayout);
} else {
fadeInView(vh.linLayout);
slideOutView(vh.linLayout);
}
}
});
i have tried this and its working. So try this once!!

Android - show animation atleast a few second

I have Button which function is to animate one of my ImageView for a second and delete specific table, recreate and insert the default value. Everything's working fine but one of my requirements is to show the animation aleast 1 second even recreating table is done. how can i achieve this?
NOTE: I animate my ImageView with Rotate effect so it will looks like refreshing.
refreshDatabase function
public void refreshDatabase(View v){
Button btnRefresh = (Button)findViewById(R.id.btnRef);
ImageView refreshing = (ImageView) findViewById(R.id.rfs);
btnRefresh.setClickable(false);
btnRefresh.setEnabled(false);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//I want to start animation here
refreshing.startAnimation(animation);
....recreat table here
//Stop animation after 1 second
refreshing.clearAnimation();
}
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<rotate
android:duration="300"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="restart"
android:toDegrees="360" />
</set>
I try to add this,
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//recreate table here
refreshing.clearAnimation();
}
}, 1000);
but i got an error Handler is abstract; cannot be instantiated.
Anybody can help me? Thank You!!!
You can use Handler which will wait for a second and then execute clearAnimation method:
public void refreshDatabase(View v){
Button btnRefresh = (Button)findViewById(R.id.btnRef);
ImageView refreshing = (ImageView) findViewById(R.id.rfs);
btnRefresh.setClickable(false);
btnRefresh.setEnabled(false);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
//I want to start animation here
refreshing.startAnimation(animation);
....recreat table here
//Stop animation after 1 second
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
refreshing.clearAnimation();
}
}, 1000);
}
Use a Handler (http://developer.android.com/reference/android/os/Handler.html) to post delay a message or a Runnable in which you call clearAnimation()
You can set the duration of the animation like the following example in your rotate.xml file
<rotate android:interpolator="#android:anim/accelerate_interpolator"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000" //set this property (1000 is 1second)
android:startOffset="1500"/>
Or you can do it programatically too by using setDuration() .
ImageView iv_follow_insta = findViewById(R.id.iv_follow_insta);
Animation zoomInanimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.dialog_zoomin);
Animation zoomOutanimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.dialog_zoomout);
zoomOutanimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
iv_follow_insta.startAnimation(zoomInanimation);
}
});
zoomInanimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation arg0) {
iv_follow_insta.startAnimation(zoomOutanimation);
}
});
iv_follow_insta.startAnimation(zoomInanimation);
iv_follow_insta.startAnimation(zoomOutanimation);
new Handler().postDelayed(() -> {
zoomInanimation.setAnimationListener(null);
zoomOutanimation.setAnimationListener(null);
iv_follow_insta.clearAnimation();
}, 10000);
try this

How to make a View blink after translation using Animation objects?

I want to make 2 animations on one View, with the second one starting after the first one. The first one is translating and I use TranslateAnimation for that purpose and the second one is AlphaAnimation as I want the View to start blinking after translation. The problem is no matter what I'm doing these animations start simultaneously. As far as I'm concerned I should use AnimationSet object for this purpose but how to do this exactly? I even tried to use setStartTime() method to use some crazy value for the AlphaAnimation before putting it into AnimationSet but still the animations start immediately and simultaneously. So what should I do to prevent it from happening?
PS Here is what I tried to do and what didn't work:
TextView cursor = (TextView) findViewById(R.id.cursor);
Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
AnimationSet s = new AnimationSet(false);
s.addAnimation(tAnim);
s.addAnimation(bAnim);
cursor.startAnimation(s);
you can use this
TextView cursor = (TextView) findViewById(R.id.cursor);
Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
tAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) { }
#Override
public void onAnimationRepeat(Animation arg0) { }
#Override
public void onAnimationEnd(Animation arg0) {
cursor.startAnimation(bAnim);
}
});
cursor.post(new Runnable() {
#Override
public void run() {
cursor.startAnimation(tAnim);
}
});
Make sure that cursor and tAnim are instance/global variable in your class, and also start the animation in the post method of the TextView to run it in synchronize with the textView
Use animation listener. hope this works
Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
AnimationListener AnimationListener = new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
// TODO Animation start and end
}
#Override
public void onAnimationEnd(Animation animation) {
//start the second animation here
yourview.startAnimation(bAnim);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}};
tAnim.setAnimationListener(AnimationListener);
yourview.startAnimation(tAnim);
tAnim.hasEnded();

Doing animation on "hidden" Activity

I have been working on trying to get an Activity to not show until data has been selected and is ready for display: Prevent screen display until after fetching data
Finally got it to work with the aid of themes.
Now I have another problem.
I need to animate the transition from one Activity to the next.
I know how to use overridePendingTransition but this won't work here since I am already in the target Activity when I want to do the animation.
The only reason I can see the other one is because the current one is transparent.
I have no problem sliding in the new one:
View view = getLayoutInflater().inflate(R.layout.content_screen, null);
view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
setContentView(view);
However, I can't think of any way to get the old one to slide out.
Any ideas anyone?
Create a "slide out left" animation. Then adjust your animation for the new view, so when the animation starts, you apply and start the other animation to the other view. E.g.,
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.slide_in_right);
animation.setDuration(TIME);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO - Set and start other animation here
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(animation);
[Edit]
// First, define and infalte the view that will be incoming
View upcomingView = inflater.inflate(...);
// Next, we'll set the animation up
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.slide_in_right);
animation.setDuration(TIME);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Animation outAnimation = AnimationUtils.loadAnimation(context,
R.anim.slide_out_left);
upcomingView.startAnimation(outAnimation);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
currentView.startAnimation(animation); // This will start the animation defined above, which will also set and start the animation for the incoming object

android temporarily use setEnabled(false)

I am animating an ImageView so that when you click the image the animation occurs (then later it resets) but my problem is that if you click again where the image sat originally - the animation starts from the beginning right away with out finishing (it just resets and starts again.
so I tried using
setEnabled(false)
which works great, the animation continues on its path up perturbed by any random clicking, now the only problem is getting the ImageView enabled again - at about the same time as the animation stops
here's what i have
stopImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mpButtonClick.start();
stopImage.setEnabled(false);
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
stopImage.startAnimation(anim);
now is there an easy way i can call setEnabled(true) after a some time has passed?
You could try using an AnimationListener and then calling setEnabled(true) from onAnimationEnd().
Something like this:
stopImage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
mpButtonClick.start();
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
anim.setAnimationListener(new Animation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
stopImage.setEnabled(false);
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
stopImage.setEnabled(true);
}
});
stopImage.startAnimation(anim);
}
}
Here's the documentation for AnimationListeners.

Categories

Resources