I have an ImageView in my layout.
ImageButton ib = (ImageButton) findViewById(R.id.btn1);
ib.setImageResource(R.drawable.blue_3);
I want to have an animation only for R.drawable.blue_3 icon from left to right.
its is not possible to only add animation to resource image not to view.
Rather you can do it by:
1.Taking any Layout in place of ImageButton.
2.Add ImageButton in Layout and then,
3.Apply animation to ImageButton.
I things your problem is to manage background of ImageButton so as par above idea you can do it the same.
try this its work for me....!
make a folder in resource and name it anim and place this xml file
(note: this is a sample xml file if you want single move you want to edit it)
move.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator" >
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-75%p"
android:toXDelta="75%p"
android:duration="2000" >
</translate>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:startOffset="800"
android:fromXDelta="75%p"
android:toXDelta="-75%p"
android:duration="2000" >
</translate>
</set>
Finally add this in your code
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
Image.startAnimation(animation);
Related
I am new to android development. I want to create a splash screen with two text views. In this splash screen I want two transitions
1) Text View 1 transition from top to center
2) text View 2 transition from bottom to center
Both transitions should be performed at the same time
how to achieve this ?
Thanks,
Creat an xml file in your anim folder name bottom_to_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fillAfter="true"
android:fromYDelta="100%p"
android:toYDelta="0%p" />
</set>
and your oncreat you add this
TextView textview= (TextView) findViewById(R.id.textview);
Animation bottomToTop = AnimationUtils.loadAnimation(this, R.anim.bottom_to_top);
textview.startAnimation(bottomToTop);
and from top to bottom animation
create an xml file by name top_bottom.xml in your anim folder
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fillAfter="true"
android:fromYDelta="-100%p"
android:toYDelta="0%p" />
</set>
and place in java
TextView textview2= (TextView) findViewById(R.id.textview2);
Animation topTobottom = AnimationUtils.loadAnimation(this, R.anim.top_bottom);
textview2.startAnimation(topTobottom );
Hope this helps you
I'm trying to develop a splash screen with a large image scrolling (Animating) from top to bottom.
I dont think using ScrollView would be good since I dont want the user to scroll the image.
This is what I would like to achieve:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromYDelta="-100%p"
android:toYDelta="100%p"
>
</translate>
</set>
create this animation as slidedown.xml in an anim folder in the project and use the code below in the java file that has the view.You may change the values of "android:fromYdelta" according to your need.
myImageView.startAnimation(slidedown);
For example if your imageview is 250 px use :
ObjectAnimator.ofFloat(myImageView, "translationY", 250);
And in xml set on your imageview :
android:translationY="-250"
I want to make a Fade Out and In Animation every time I click a button.
I started to check only the fade out and on the first click it seems the Fade Out works just fine. But when I click again the animation fade from top to bottom, making it look bad and cut.
The Animation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="1000" />
</set>
The Code:
Animation fadeOut = AnimationUtils.loadAnimation(myActivity.this, R.anim.fade_out);
LinearLayout myBackground=(LinearLayout)findViewById(R.id.myBackground);
myBackground.setAnimation(fadeOut);
What am I missing? Thanks!
Make anim in following folder ris->anim->fade_anim.xml
Add below xml
<alpha
android:duration="2000"
android:fromAlpha="1"
android:toAlpha="0.0"
android:repeatCount="infinite"/>
Got to java class
//make animation object
final Animation myAnim = AnimationUtils.loadAnimation(getContext(), R.anim.bounce_anim);
myAnim.setRepeatMode(Animation.INFINITE);
//get View on which on you want to apply animation I am applying on imageView
mLayout.findViewById(R.id.imageView).startAnimation(myAnim);
I've got a layout much like the one below. Currently when the back button is pressed the red linear layout's visibility is set to gone. However, I'd like it to "slide" up off of the page instead. How would I do this?
You need to use animations. here is the top in/out animations:
In Top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%" android:toYDelta="0%" android:duration="300"/>
</set>
Out Top
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="600"/>
</set>
Then in your activity get the view and apply an animation to it like this:
This are type Animation.
mSlideInTop = AnimationUtils.loadAnimation(this, R.anim.slide_in_top);
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
and call them with this code:
header.startAnimation(mSlideOutTop);
header.setVisibility(View.INVISIBLE);
Here header is a LinearLayout wrapping my views. same thing if you want to make it slide in. just add the slide in animation and make the view visible.
I want to have a View that initially is invisible and when I press a button, it becomes visible with a fade in animation. I'm using the AlphaAnimation for the fading effect. The problem is that if I make the view invisible the animation can't be seen.
Suppose you have an ImageView named imageView and an animation file your_fade_in_anim.xml inside your res\anim\ folder:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
// Now Set your animation
imageView.startAnimation(fadeInAnimation);
Your XML
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="[duration (in milliseconds)]"
android:repeatCount="infinite" />
</set>
Replace the brackets with your actual duration.
Provide an AnimationListener to the Animation and make the View visible as soon as the Animation starts.
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
Instead of the infinite repeat count and hiding/viewing your View, I suggest to just not repeat the animation and initially start with the alpha channel set to maximum. Then you can use:
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="[duration (in milliseconds)]"
android:repeatCount="0" />
</set>
And you're done. No need for a Listener, hiding or showing. Just as simple.