Hiding image view before animating it - android

I have an Image view, i am applying animation to it, it has to move up and down on the screen when the button is pressed. But before the button is pressed i want the image hidden.
I am using the following code:
final Animation animation = AnimationUtils.loadAnimation(this,
R.anim.aim);
animation.reset();
maxName.startAnimation(animation);
anim.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fromYDelta="15%p"
android:repeatCount="2"
android:repeatMode="reverse"
android:toYDelta="60%p" />
What changes should i make in order to hide the image before the button is pressed.
Thank-you in advance.

Try to add
android:visibility="invisible"
to your element in its xml file? You can also use gone instead of invisible if you want the element to occupy no space in your layout.
Then when the button is pressed, you can change the visibility of the element at runtime:
final Animation animation = AnimationUtils.loadAnimation(this,
R.anim.aim);
animation.reset();
maxName.setVisibility(View.VISIBLE);
maxName.startAnimation(animation);

Add AnimationListener to your animation .. and write down
yourImageview.setvisibility(view.gone) in onAnimationStart
yourImageview.setvisibility(view.visible) in onAnimationEnd

Related

How to make setOnClickListener on a dynamic imageview

Working in Android Studio, I need to add in my app a click listener on my imageview which has a certain animation. This is the code I have in MainActivity:
myImage.startAnimation(myAnimation);
myImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//DO SOMETHING
}
});
myAnimation comes from an XML in anim folder which does a translational animation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:interpolator="#android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:fromYDelta="0%p"
android:toYDelta="40%p"
android:duration="1000"/>
</set>
This works fine, although this click listener is set on the space that has the image on the layout (activity_main.xml) and does not follow the animation inserted in the image. If I click on the space which belongs to the image in the layout, the click listener starts even if the image is not there due to the animation.
Is there a way in which the click listener is attach to the imageview in motion?
Thank you
This happens because the xml Translate animation you are using, does not really animate the View's property, it just moves the pixels on the screen and so it just looks like it has changed position, that is why the OnClickListener is still active on the View's original position, because Android thinks the View never really moved.
Simple solution use ObjectAnimator or even better ViewPropertyAnimator. Both will animate the View's property and so the OnClicklistener will also change it's position with it.

Imagebutton does not become invisible after translate animation in Android

I am moving an image button from left to right at the bottom of screen using xml using this:
<translate android:fromXDelta="-500%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="00%"
android:duration="800"
android:zAdjustment="bottom"/>
At the same time I have a linear layout invisible which by clicking the button becomes visible but my button is still visible.
case R.id.fab_image_button:
txtTitle.setVisibility(View.INVISIBLE);
listView.setVisibility(View.INVISIBLE);
fabImageButton.setVisibility(View.INVISIBLE);
lOUT.setVisibility(View.VISIBLE);
break;
any suggestions?
We need to put the imagebutton in a separate layout and make the whole layout invisible after clicking

Animation on changing ImageBitmap of ImageButton

Let's say I have an ImageButton btn. I have two different Bitmap, bmp1 and bmp2. By default, btn displays bmp1, like this:
btn.setImageBitmap(bmp1);
Can I somehow make an animation, which crossfades bmp1 and bmp2, when calling the following:
btn.setImageBitmap(bmp2);
So the question is, is it possible - if yes, how - to animate the changing of bitmaps on ImageButtons?
The way I see it, there are two main ways to implement this functionality. Note that there may be other methods that will make this happen exactly as you are describing, but these would be my approaches.
First Approach: Leveraging the onAnimationEnd() Callback
Here, you would want to essentially fade out the first ImageButton, change the resource in the onAnimationEnd() callback, and then fade it back in. To do this, you would have to implement the below code.
Create an Animation resource in XML that would be used to fade the View in:
<!-- Filename: fadein.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Note that you might want to change the duration here-->
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="250"/>
</set>
Create an Animation resource in XML that would be used to fade the View out:
<!-- Filename: fadeout.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Note that you might want to change the duration here-->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="250"/>
</set>
Load the fade out and fade in Animations in your code:
// Obtain a reference to the Activity Context
Context context = getContext();
// Create the Animation objects.
Animation outAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeout);
Animation inAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
Assign a new AnimationListener to this Animation, and build it out as follows:
outAnimation.setAnimationListener(new AnimationListener(){
// Other callback methods omitted for clarity.
public void onAnimationEnd(Animation animation){
// Modify the resource of the ImageButton
yourImageButton.setImageResource(R.drawable.[your_second_image]);
// Create the new Animation to apply to the ImageButton.
yourImageButton.startAnimation(inAnimation);
}
}
Assign the Animation to the ImageButton, and start the first Animation:
yourImageButton.startAnimation(outAnimation);
Then, if you wish to animate back to the previous image, you would simply do the same but in the reverse order.
Second Approach: Overlay a Second ImageButton
For this approach, you would simply assign two ImageButtons to the same coordinates on the screen, and with the same width. Then, you would fade out the first ImageButton and fade in the second one after the first Animation ended (much like the example above).
I hope that this answer aligns with your expectations. My apologies if there are any code errors, as I am doing this without an editor at the moment. Please let me know if you would like any additional clarification!
It is not possible to animate image button source changes. The workaround would be to stack two ImageButtons and animate its alfa channel separately to get expected behavior.

Android-Move content downwards

I'm very new regarding developing apps for android. Now what I want to do is to implement four buttons and as soon as a user clicks, let's say, on the topmost button, another two sub-buttons should appear underneath the clicked button and the other three remaining buttons should automatically move downwards.
I think my explanation is not hundred per cent clear, so I try to illustrate the problem with some images.
Now here are the four buttons:
http://advancedata.ad.funpic.de/First-AGApp.png
And as soon as the user pushes button one, two extra buttons should appear and the other three buttons should move downwards:
http://advancedata.ad.funpic.de/Second-AgApp.png
I would be very thankful for any advice how to implement this.
Thanks,
enne
Draw all your buttons in a LinearLayout with vertical orientation. Add the attribute
android:visibility="gone"
to the buttons that should appear when clicking the main buttons. Then you can show those buttons in the OnClickListener of the main buttons with the line:
button.setVisibility(View.VISIBLE);
where button is the reference to your layout in the code.
Button button = (Button) findViewById (R.id.your_button_id);
EDIT:
To add an animation to the process, you have to slide up/down the new buttons appearing and the buttons below. (Group the views into Layouts so it's easier to apply the animations).
Here you have the two XML files to create in your res/anim folder:
slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="-50" android:toYDelta="0"
android:duration="300" />
slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="0" android:toYDelta="-50"
android:duration="300" />
Create the animations in your code with:
Animation slideDown = AnimationUtils.loadAnimation(this, R.anim.slide_down);
and apply it to the buttons with:
secondaryButton.startAnimation(slideDown);
When sliding up, you need to set the visibility to "gone" after the animation is finished, not before. In order to do that, you need to set the animation listener and hide the button in onAnimationEnd:
slideUp.setAnimationListener(new AnimationListener () {
#Override
public void onAnimationEnd(Animation animation) {
secondaryButton.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) {}
});

Android animation for dynamically loaded image

I want to create an animation for an image that is loaded into a vertical linear layout dynamically. When triggered I would like the image to slide down with its alpha set to 0 (this will slide all content below it down I hope) then fade the image in. Being new to Android animations I am having some trouble. Right now I'm trying to get just the fade in to work, here is what I have:
fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="https://chemas.android.com/ap/res/android"
android:interpolator="#android:anim/anticipate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="5000" >
</alpha>
In the Activity:
Animation animation = AnimationUtils.loadAnimation(context, R.anim.fade_in);
ImageView image = new ImageView(context);
image.setImageResource(R.drawable.my_image);
image.setLayoutParams(layoutParams);
image.setAlpha(0);
((ViewGroup) view).addView(image, 0);
image.startAnimation(animation);
The image is being loaded as all the context below it is shifted down, however if never fades in. I would like to get this working as well as having the image slide down so the dynamica add does not look so choppy.
Thanks in advance.
You have a typo in the xmlns:android tag.
It should be
xmlns:android = "http://schemas.android.com/apk/res/android"
With that being corrected, just remove the image.setAlpha(0); and it should animate. I am afraid I cannot give you a reason why it doesn't work with the alpha attribute set to 0.
Regarding the slide down animation, you will have to subclass Animation. Have a look at this answer that I think can help you get what you want: https://stackoverflow.com/a/5122460/871102

Categories

Resources