android - animation doesn't work the second time - android

I have a button which need to fade in. But it works only the first time. It doesn't work the second time.
Here is my code.
final TextView doctorInfoView = (TextView) rowView.findViewById(R.id.doctorInfo);
final TextView specialtyView = (TextView) rowView.findViewById(R.id.specialty);
final ImageButton deleteDoctor = (ImageButton)rowView.findViewById(R.id.deleteDoctor);
final Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fade_in_animate);
final ImageButton editDoctor = (ImageButton)rowView.findViewById(R.id.editDoctor);
final RelativeLayout mainRowLayout = (RelativeLayout)rowView.findViewById(R.id.doctorListInfoView);
final LinearLayout rowLayout = (LinearLayout)rowView.findViewById(R.id.doctorInfoLayout);
final LinearLayout editButtonLayout = (LinearLayout)rowView.findViewById(R.id.editButtonLayout);
final LinearLayout deleteButtonLayout = (LinearLayout)rowView.findViewById(R.id.deleteButtonLayout);
rowLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isClicked) {
editDoctor.setAnimation(fadeInAnimation);
editDoctor.setVisibility(View.VISIBLE);
deleteDoctor.setAnimation(fadeInAnimation);
deleteDoctor.setVisibility(View.VISIBLE);
mainRowLayout.setBackgroundColor(Color.parseColor("#ffffff"));
doctorInfoView.setTextColor(Color.parseColor("#eeeeee"));
specialtyView.setTextColor(Color.parseColor("#eeeeee"));
editButtonLayout.setBackgroundColor(Color.parseColor("#16aea3"));
deleteButtonLayout.setBackgroundColor(Color.parseColor("#16aea3"));
isClicked = false;
} else {
editDoctor.setVisibility(View.GONE);
deleteDoctor.setVisibility(View.GONE);
mainRowLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
doctorInfoView.setTextColor(Color.parseColor("#000000"));
specialtyView.setTextColor(Color.parseColor("#0d9e9f"));
editButtonLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
deleteButtonLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
isClicked = true;
}
}
});
Here is fade_in_animate.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="500"/>
</set>
I'd appreciated about any feedback.

One approach to solve this would be to set the animation to null
editDoctor.setVisibility(View.GONE);
editDoctor.setAnimation(null);
EDIT: You forgot to set it to infinite
animation.setRepeatCount(Animation.INFINITE);
Here is the xml
android:repeatCount="-1"
android:repeatMode="repeat"
Here is the full documentation
EDIT 2: I didn't see that you are setting the alpha. My bad. This should work! You don't need to repeat it. This will work with the method of setting the animation to null.
editDoctor.setVisibility(View.GONE);
editDoctor.setAnimation(null);
editDoctor.setAlpha(.0f);

Related

Animation will NOT stop running

i have implemented an animation on one of my image views. My problem is that the animation will NOT stop at all. I call everything clearanimation i set it to null set it to cancel and it still wont stop.
public void tiltani(){
ImageView vault = (ImageView)findViewById(R.id.vault2) ;
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.startAnimation(tilt);
}
public void stopani() {
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
here is xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="6"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatCount="infinite"/>
<rotate
android:fromDegrees="6"
android:toDegrees="-2"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
here is where i start it
Intent intent1 = getIntent();
if (intent1.hasExtra("id1")) {
tiltani();
and i try to stop/cancel everything in an onclick method
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.chestopen));
stopani();
update
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt);
if (intent1.hasExtra("id1")) {
vault.startAnimation(tilt);
vault.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
vault.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,
R.drawable.chestopen));
vault.setAnimation(null);
You can just use vault.startAnimation(tilt); to start your animation, and use vault.setAnimation(null); to stop your animation.
You have initialized Animation a second time in stopani() method. Your view initialization and animation initialization should be global.
Your code should be like this.
ImageView vault = (ImageView)findViewById(R.id.vault2); //Global variable
Animation tilt = AnimationUtils.loadAnimation(this, R.anim.tilt); //Global variable
public void tiltani(){
vault.startAnimation(tilt);
}
public void stopani() {
vault.clearAnimation();
vault.setAnimation(null);
tilt.cancel();
tilt.reset();
}
Hope, It will help you.. :)

How to fade in textview letter by letter in Android

I am trying to do a fade in animation on a textview letter by letter but when i do it with view animation it fade in the entire textview.
I tried to combine view animation with a handler for when letter appear but i don't get the result i wanted. this is the code i tried.
the xml file
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/linear_interpolator"
android:duration="3000"
android:fillBefore="true"
android:zAdjustment="bottom"/>
the java code
public class TextViewAnimator {
private TextView textView;
private CharSequence text;
private long delay;
private int index;
private Handler timerHandler = new Handler();
private Runnable animationTask = new Runnable() {
#Override
public void run() {
textView.setText(text.subSequence(0, index++));
if (index <= text.length()) {
timerHandler.postDelayed(animationTask, delay);
}
}
};
public static TextViewAnimator newInstance(TextView textView,
CharSequence text, long delay) {
TextViewAnimator instance = new TextViewAnimator();
instance.textView = textView;
instance.text = text;
instance.delay = delay;
return instance;
}
public void start() {
textView.setText("");
timerHandler.postDelayed(animationTask, delay);
}
}
is there another way to get fade in letter by letter?
I guess this is too late but still may help someone.
I too needed something like this but found a satisfactory solution nowhere, decided to make a custom View of my own, then transformed it into a library. Try [Fade-In TextView][1]. It sets text letter by letter with a nice fade-in animation.
Usage
In the XML layout
<believe.cht.fadeintextview.TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#android:color/black"
app:letterDuration="250"/>
And in the Activity/Fragment
believe.cht.fadeintextview.TextView textView = (believe.cht.fadeintextview.TextView) findViewById(R.id.textView);
textView.setLetterDuration(250); // sets letter duration programmatically
textView.isAnimating(); // returns current animation state (boolean)
textView.setText(); // sets the text with animation

changing images through ling click in android

i am making a flashlight app in which i am using using two images, one each for flashoff state and flashon state respectively.
i want to have animation such that these two images get toggled(image 1 apperas then image2 then again image 2..infinite times) when there is longclick..
that is iwant to show this in longclicklistner...
here is what i have done..
i am using this animation..
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:duration="1000" android:repeatCount="infinite" android:repeatMode="reverse" android:fromAlpha="0.3" android:toAlpha="1.0" />
and
#Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
tButton.startAnimation(flickon); //tbutton is an image button
return true;
}
});
}
using this animation is happening but images are not switching..please help
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.xxx);
use the above code to set an image to the image view and use a thread that can change contents in the UI bascially some thing like this
public void onLongClick(View v) {
new Thread(new Runnable() {
public void run() {
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.xxx);
}
}).start();
}
You can loop over it for as many times as you want.

Animating a TextView each time it is changed

I have a button which basically changes the TextView every time it is clicked. Is it possible to associate an animation with this? I have looked at things such as a 3D flip but they seem a little too advanced for me. Any simpler suggestions?
final Button button1 = (Button) findViewById(R.id.button1);
final TextView textView = (TextView) findViewById(R.id.text_random_text);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(getNextRandomCuisine());
}
});
I suggest you to take a look at the Android webPage. http://developer.android.com/reference/android/view/animation/AnimationUtils.html
the class AnimationUtils helps you to load and Animation Pattern from your xml folder, you can for example make a "fadein" animation, you should dafür create an xml datei, name it for example "fadein.xml" it should be like this
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" >
</alpha>
with this you are just telling your android device, that this animation should variates the alpha value (also the transparence) of your view, from 0 to 1, making your view visible, the "duration" parameter is just how any milliseconds will take to execute this animation.

How to replay a one shot animation

I want to make a one shot animation but be able to play it as many times as I want. Right now it plays only the first time.
.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/p1_ch1" android:oneshot="true">
<item
android:drawable="#drawable/p1_ch1_5"
android:duration="500"/>
<item
android:drawable="#drawable/p1_ch1_4"
android:duration="1000"/>
<item
android:drawable="#drawable/p1_ch1_5"
android:duration="500"/>
</animation-list>
.java:
public void handler_p1_ch1_5 (View target){
ImageView iv = (ImageView)findViewById(R.id.p1_ch1_5);
AnimationDrawable aw = (AnimationDrawable)iv.getBackground();
aw.start();
}
Just call aw.stop() before aw.start()
Use this:
frameAnimation.setOneShot(true);
Change
android:oneShot="true" to
android:oneShot="false"
If you want to replay a one shot animation after previous animation finish you can do like
final AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable();
if (!animationDrawable.isRunning()) {
int totalFrameDuration = 0;
for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
totalFrameDuration += animationDrawable.getDuration(i);
}
animationDrawable.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animationDrawable.stop();
}
}, duration);
}

Categories

Resources