How to set marquee behaviour in android to alternate programatically - android

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);

Related

Android View animation to top parent

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.

Make a layout appear with animation after button click in Android

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/

Best way to implement custom view with animation at the center of the screen?

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 :)

How to avoid blink of View when using animation?

I use translate and rotate animations to position View inside FrameLayout in onCreate event. I want animations to perform instantly so I set duration for both of them to 0. But when application starts there is a short blink of my View in top left corner of screen and then it becomes positioned according to animation parameters. How can I avoid this blink?
I spent whole day with that problem. fillAfter and fillBefore has nothing to do with that. Try this before animation start:
view.setVisibility(View.GONE); // view is our View we trying to animate
Then set animation listener for your animation:
animation.setAnimationListener(new AnimationListener(){
public void onAnimationEnd(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
v.setVisibility(View.VISIBLE);
}
use animation.setFillAfter(true) or animation.setFillBefore(true), depending on your needs. This should resolve the blink
Having a tag inside the parent tag of your animation file will cause this, try it with only one tag and you will see the difference. I am working with this right now
Odaym's answer is the solution for the issue actually. If you have something like that:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<set
android:duration="1000">
<translate
android:fromXDelta="100%"
android:toXDelta="0"
/>
</set>
</set>
Change it into this:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true" android:duration="1000">
<translate
android:fromXDelta="100%"
android:toXDelta="0"
/>
</set>
Set the visibility after the animation execution code but not in "onAnimationEnd", also try to set the duration
view.setVisibility(View.VISIBLE); // view is our View we trying to animate

Animate View in a layout (pre-Honeycomb)

I tried to animate View's position in a RelativeLayout. In every frame I change the topMargin of the View's LayoutParams and then call View#setLayoutParams(newParams).
The animation is unfortunately too slow because the setLayoutParams() call triggers remeasuring of the whole View tree.
How can I solve this without using API 11+?
You can use View Animations Just make yourself an xml file with a translate animation in it that defines how you'd like your View to move. Then inflate a reference to your Animation and call
view.startAnimation(anim);
Here is an example of a translate xml file. You'd save a file like this in res/anim/
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/overshoot_interpolator">
<translate android:fromXDelta="100%p" android:toXDelta="0%p" android:duration="700"/>
</set>
Here is what you'd do in java to use it.
Animation slideLeftIn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
//mView can be any View object.
mView.startAnimation(slideLeftIn);
}

Categories

Resources