Imagebutton does not become invisible after translate animation in Android - 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

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.

How to set marquee behaviour in android to alternate programatically

This is the code I am using but the behaviour of the marquee is that is scrolls. How to make it to alternate i.e. it moves from right to left and then left to right.
public void setMarquee(TextView textView) {
textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
textView.setMarqueeRepeatLimit(-1);
textView.setHorizontallyScrolling(true);
textView.setSelected(true);
textView.requestFocus();
}
Step1
create folder called anim in res directory
step 2
create slide_left.xml
slide_left.xml
<translate
android:duration="3000"
android:fromXDelta="100%"
android:toXDelta="0" />
</set>
step 3
in main_activity.class
LinearLayout mlayout =
(LinearLayout)findViewById(R.id.mlayout); // the view u want to animate
Animation slide_left_anim;
slide_left_anim=
AnimationUtils.loadAnimation(getActivity().getApplicationContext(),
R.anim.slide_left);
slide_left_anim.setRepeatCount(Animation.INFINITE);
mlayout.startAnimation(slide_left_anim);

Hiding image view before animating it

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

How to create an animation in front of a 3rd party linear layout

I have 5 linear layouts wrapped up in one linear layout, inside a relative layout. I am using a translation animation to move an image button within one linear layout. If I try and move it from from outside the linear layout (in this case a point in another linear layout) into the linear layout it resides in it seems to be behind the other linear layout, ie not visible, and only appears when it crosses into the linear layout it resides within. Can I have it somehow over the other linear layouts? Here is my code.
<translate
android:duration="2000"
android:fromXDelta="-40%p"
android:fromYDelta="100%p"
android:toXDelta="0%p"
android:toYDelta="10%p"
android:zAdjustment="top" />
</set>
Would a frame layout solve this issue? If so, how do I ensure the image in the frame layout appears in front of the linear layouts?
Animate that View on the DecorView. First add it to the decorView and then call translate animation. Remove that view from decorview when animation ends.
findViewById(R.id.main).addView(viewToAnimate);
viewToAnimate.setAnimationListener(new AnimationListener() {
onAnimationEnd(..) {
findViewById(R.id.main).removeView(viewToAnimate);
}
}
viewToAnimate.startAnimation();
Code is not exactly what it will be (typed from memory), but should be something like this..

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