I'm trying to implement a simple animation effect for login screen.
Here's the scenario,
1: Some text and a login button will be displayed by default.
2: After clicking the login button a new frame layout will appear from bottom to top. This layout will prompt user to enter their username and password.
I can make an animation which will overlay from one parent to another. In this scenario, I'm looking for an animation that will appear without leaving the activity.
First set invisible to your layout.
Set animation to slideUp and slideDown.
final LinearLayout layout = (LinearLayout)findViewById(R.id.yourlayout);
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Animation slideUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up);
Animation slideDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down);
if(layout.getVisibility()==View.INVISIBLE){
layout.startAnimation(slideUp);
layout.setVisibility(View.VISIBLE);
}
});
slide_up.xml (create in res/anim directory)
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:fromYDelta="500" android:duration="500"/>
</set>
slide_down.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:fromYDelta="0" android:toYDelta="500" android:duration="500"/>
</set>
Note:
You should edit values in slide_down.xml and slide_up.xml until get favorable result.for example:change android:fromYDelta="500" to android:fromYDelta="700"
Check out this tutorial, it explains some basic animations which you can customize for your need.
http://www.androidhive.info/2013/06/android-working-with-xml-animations/
Related
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.
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);
My target is to animate current view from it's current position(center), to top border of it's parent. Is that possible? I have already tried next code, but the problem is that my view disappears after animation:
Animation bottomUp = AnimationUtils.loadAnimation(context, R.anim.bottom_up);
popup.startAnimation(bottomUp);
and
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="100%"
android:toYDelta="0%"
android:fillAfter="true"
android:duration="500"/>
</set>
That can easily be done in Java with ViewPropertyAnimator
popup.animate().translationY(0).setDuration(500); // 0 is top of Y axis
The animation always starts at the current position.
Check the ViewPropertyAnimator docs for future animations, it's very easy to use and can save you a lot of code.
My GOAL is to implement custom toast in my application as follows:
MyCustomToastClass.makeText(context,View,anyDurationInMS);
I want to set the "Toast" gravity, Layout, duration(not just Length.SHORT/LONG)
Things i've tried so far:
Class with windowManager object
problem was it must implement android system animation in:
params.windowAnimations
and when I tried to implement my custom animation as followed:
windowManager.addView(mLayout);
Animation AlphaAnimation = new ...
it didn't implement my animation.
Class with the rootView element to add the Toast's layout to it:
In this way I succeeded to implement everything only when the root view was FrameLayout type (With other layout I couldn't set the "Toast" gravity to center).
I would appreciate if someone has implemented this feature, Or to guide me if i'm missing something in one of my ways.
thanks
Sorry for misunderstand your question
if you want to create an pop up like toast and the duration is up to you maybe you can try to create a custom view that will contain the content you want to be toasted after that you can place your main layout in the frame layout then everytime the user trigger your custom toast you can add the custom view to your frame layout so it'll be positioned in front of your main layout and for fade in fade out animation you can use this
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
or if you want to use XML
FadeIn
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha= 1.0"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
FadeOut
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
/>
and for the time you can use
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// FADE OUT THE POP UP/TOAST HERE
}
}, /*SET THE TIME HERE*/);
I hope this answer is clear enough for you
and if you still have some question about my answer don't hesitate to ask in the comment :)
I'm trying to move an image from left to right on layout, but not by dragging. I want it to go automatically when something happened. So, i found an animation like this:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_interpolator" >
<translate
android:duration="1000"
android:fromXDelta="0"
android:toXDelta="200"
android:fillEnabled="true"
android:fillAfter="true" />
</set>
But the problem is i couldn't find how to make it stop there! It's just went back directly. Any suggestion please?
Here is my Java code:
public void onClick(View v) {
runOnRight(this, image);
}
static Animation runOnRight(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,
R.anim.slide_left_to_right);
target.startAnimation(animation);
return animation;
}
set FillAfter to true in your animation set xml
When the View.onAnimationEnd() callback is fired, you can set the new View postion with View.layout(int, int, int, int). The View.layout call assign size and position to your view. Refer to the documentation for more information
use the field FillAfter="true" which will stops at the end.
just like following.