I am trying to move 3 buttons relative to their starting points:
The code for starting the animation is:
protected void showMoreBtns() {
Button btn1 = (Button)this.findViewById( R.id.more1btn );
Button btn2 = (Button)this.findViewById( R.id.more2btn );
Button btn3 = (Button)this.findViewById( R.id.more3btn );
Animation showMore = AnimationUtils.loadAnimation( this, R.anim.optionsinup1 );
btn1.startAnimation( showMore );
showMore = AnimationUtils.loadAnimation( this, R.anim.optionsinup2 );
btn2.startAnimation( showMore );
showMore = AnimationUtils.loadAnimation( this, R.anim.optioninup3 );
btn3.startAnimation( showMore );
}
And the animation is defined as:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-60"
android:toYDelta="-30" />
</set>
All three animations follow the same format, with only the android:toDelta's being modified.
The issues is the animation runs, yet the buttons return to their original position at the end of the animation. I would like them to stay at the end point.
showMore.setFillAfter(true);
This will be placed in your java code.
I hope it will be work.If you want to use the button after complete animation you will placed onAnimationEnd(Animation animation) in that place the button using your layout.
You need two sets of buttons, and after the animation is played you have to make one set disappear, probably using View.setVisibility(View.GONE) on its parent or on each Button.
Not sure if this will help you but i was struck with the same problem i was able to do this using these methods,
setTranslationX(float)
setTranslationY(float)
you can use it Like this
Button button = (button) findViewById(your id);
button.setTranslationX(a float value);
here's the android documentation that provide more information
http://developer.android.com/reference/android/view/View.html#attr_android:translationX
also note that the minimum level of android SDK required is 11
Related
In a RelativeLayout I have a TextView and a Button under the TextView and an ImageView covering both of the other objects. As the user opens the activity, the ImageView should cover the other items.
As the user swipes on the image (left or right), it should dismiss so the user can operate the button and read the textfield.
Whith an onClick listener I could dismiss the Image when clicked but I would like an animation, exactly like in a RecicleView when swiping to delete an element.
You could add the desired animation to the onClick event.
1) create(or add if existing) the anim folder inside the res directory and put slide_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
2) Add this code to the onclick action of the image view :
// Refer the ImageView like this
imageView = (ImageView) findViewById(R.id.imageView);
// Load the animation like this
animSlide = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_animation);
// Start the animation like this
imageView.startAnimation(animSlide);
You can modify the animation specs in the xml so you can get the desired effect.
And then just implement AnimatorListener so you can dismiss the image view at animation end.
animSlide.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
//remove image view from the layout
}
});
I want to set a background drawable to 1st element of list view, also to other elements of the list view I want to set the same drawable but it should be little faded. I dont want to use two images for this.
I tried using Alpha Animation for this:
In getView of my List View i used This:
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_anim);
if (position == 0) {
list_row_layout.setBackgroundResource(R.drawable.bg_image);
} else {
list_row_layout.setBackgroundResource(R.drawable.bg_image);
animation.setDuration(0);
animation.setFillAfter( true );
if (list_row_layout != null){
list_row_layout.startAnimation(animation);
}
}
and here is my alpha_anim.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="0.5"
android:duration="1000" />
The problem is it is working fine, but the animation gets applied to 0th element also.
I tried cancelling the animation if(position == 0) but its not working.
I need to know can we apply Alpha animation to each ListRow element separately?
Check out the ListViewAnimation which contains the AlphaAnimation for the list of items in ListView.
User AlphaAnimation class as below to apply alpha effects on your ListView
private void setAlphaAdapter() {
AnimationAdapter animAdapter = new AlphaInAnimationAdapter(mAdapter);
animAdapter.setAbsListView(getListView());
getListView().setAdapter(animAdapter);
}
Why animate it? Have you tried setAlpha ?
Oh, and about the first element, add setAlpha(1f) for pos == 0. I think it might have something to do with reusing the listelements...
I would like to know if there is a simple way to add a view (a button) to a RelativeLayout, with some kind of scale animation.
I extended a class from Button and did something like this:
public class MyButton extends Button {
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
ScaleAnimation anim = new ScaleAnimation(0,1,0,1);
anim.setDuration(1000);
anim.setFillAfter(true);
this.startAnimation(anim);
}
Then tried to add this button to a view and it didn't work. Please help!
In your activity, use instead:
parentview.addView(myButton);
Then animate the button with this:
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.slide_right_in);
animation.setStartOffset(0);
myButton.startAnimation(animation);
This is an example of slide_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>
</set>
In addition,
This is a activity play animation function I wrote:
public Animation PlayAnim( int viewid, Context Con, int animationid, int StartOffset )
{
View v = findViewById(viewid);
if( v != null )
{
Animation animation = AnimationUtils.loadAnimation(Con, animationid );
animation.setStartOffset(StartOffset);
v.startAnimation(animation);
return animation;
}
return null;
}
You can call this like this:
PlayAnim(R.id.bottombar, (Context) this, R.anim.slide_right_in, 0);
Where:
1st parameter is the id of the view you want to apply the animation on.
2nd paramenter isThe context retrieved inside your activity.
3rd parameter is the desired animation that you put inside your anim resource folder or from android predefined animations.
4rd paremeter is the animation startoffset.
I tested your animated button implementation and it works correctly. There must be some other problem. Probably the way you add the button to the layout.
To add your button to the relative layout use code like this.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
MyButton b1 = new MyButton(Main.this);
b1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(b1);
Or you can inflate the button from layout. To do this create layout mybtn.xml containing your button implementation:
<?xml version="1.0" encoding="utf-8"?>
<PACKAGE_OF_MYBUTTON_HERE.MyButton
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
To add it to your layout call:
RelativeLayout rl = (RelativeLayout)findViewById(R.id.rl);
Button b = (Button)getLayoutInflater().inflate(R.layout.mybtn, rl, false);
rl.addView(b);
There might be a problem with proper positioning of your view when you add it to the relative layout. Just add code like this before calling rl.addView(b1) (the code snippet adds new button below someOtherView).
LayoutParams lp = new LayoutParams(b.getLayoutParams());
lp.addRule(RelativeLayout.BELOW, someOtherView.getId());
b.setLayoutParams(lp);
You can try adding this to your code just before adding view.I guess this code would work for any view changes. In my case was switching 2 views with animation.
TransitionManager.beginDelayedTransition(layoutlocation);//layoutlocation is parent layout(In my case relative layout) of the view which you gonna add.
Hope it works.Took 2 days for me to make this work.
It's not always necessary to use animation class to get actual animation. We can provide a delay when adding views to layout using handler as shown.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Animation fadeanimation = new AlphaAnimation(0,1);
fadeanimation.setDuration(position*60+100);
child.setAnimation(fadeanimation);
linearLayout.addView(child);
}
}, position*60);
I have a ListView on screen and a menu at the bottom. Upon a click of menu key, it animates-slides off the screen and the ListView expands.
menuBtmVisable = false;
Animation menu_off = AnimationUtils.loadAnimation(this, R.anim.menu_off);
menubtm.startAnimation(menu_off);
Display display = getWindowManager().getDefaultDisplay();
LayoutParams listlp = new LayoutParams(display.getWidth(), display.getHeight()-87 , 0, 50);
ListViewMain.setLayoutParams(listlp);
menu_off.xml
<set
android:fillEnabled="true"
android:fillAfter="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="120"
android:duration="500"
/>
</set>
Yet when, with the menu down, I click on the ListView item "13" ( see picture ), it results in a menu click, as if it is still in place...
What do you think would be the best way to take care of it?
I've looking for an answer for a problem like that, and finally, after a week, I've managed to solve my problem. Since it looks similar to yours, maybe it will help you.
Set up and AnimationListener() in your animation and, onAnimationEnd, change the layout as you want. In my case, I wanted to slide a layout up, so 2 buttons would appear from below. However, the layout just slided visually; the buttons would still be off screen, interactionally speaking. So I have something like this:
final View screen = findViewById(R.id.welcome_screen);
final Animation a = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_home_up);
a.setFillAfter(true);
a.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
screen.clearAnimation();
screen.setPadding(0, -222, 0, 0);
}
});
screen.startAnimation(a);
I trying to move a image from one place to other using animation,but after moving it coming back to original position, how to stop it to the moved position itself.
to let the image in the last place of the animation , try this :
TranslationAnimation ta = new TranslateAnimation(fromX, toX, 0, 0);
ta.setDuration(1000);
ta.setFillAfter(true); // this will let the image in the last place of the Animation
imageView.startAnimation(ta);
After an Animation is done, use the method setFillAfter(true), to make the last animation state persist:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.
Animation.setFillAfter/Before - Do they work/What are they for?
If you need to do something more specific, you could also set an animation listener and move your object at the end of the animation:
animation1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//change according to your needs
myView.setX(0);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
Finally got a way to work around,the right way to do this is setFillAfter(true),
if you want to define your animation in xml then you should do some thing like this
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="1000"/>
</set>
you can see that i have defined filterAfter="true" in the set tag,if you try to
define it in translate tag it won't work,might be a bug in the framework!!
and then in the Code
Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out);
someView.startAnimation(anim);
See this blog post for a solution:
// first set the view's location to the end position
view.setLayoutParams(...); // set to (x, y)
// then animate the view translating from (0, 0)
TranslationAnimation ta = new TranslateAnimation(-x, -y, 0, 0);
ta.setDuration(1000);
view.startAnimation(ta);
I'm sure you would have found the answer by now (I just did... so I am posting for others). Android seems to have officially shifted to a new animation framework called "Property Animation". This has been available since Honeycomb (3.0). This will essentially solve your problem as it animates the actual property values.
DEV GUIDE:
http://developer.android.com/guide/topics/graphics/prop-animation.html