5

I have a question similar to this but I am wanting to make only the text on the button flash. I don't want the button background to also flash.

This is my R.anim.blink.xml file:

<?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"
           android:startOffset="20"
           android:repeatMode="reverse"
           android:repeatCount="infinite"/>
</set>

But this code...

Animation blinkingAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
myButton.setAnimation(blinkingAnimation);

...makes the whole button blink. So how to make just the text blink (so the button background is shown all the time)?

ban-geoengineering
  • 18,324
  • 27
  • 171
  • 253

5 Answers5

5

Simple way:

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button btn = (Button) findViewById(R.id.btn);
    final ObjectAnimator colorAnim = ObjectAnimator.ofInt(btn, "textColor", Color.BLACK, Color.TRANSPARENT); //you can change colors
    colorAnim.setDuration(500); //duration of flash
    colorAnim.setEvaluator(new ArgbEvaluator());
    colorAnim.setRepeatCount(ValueAnimator.INFINITE);
    colorAnim.setRepeatMode(ValueAnimator.REVERSE);
    colorAnim.start();


    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            colorAnim.end();
            colorAnim.cancel();
        }
    });
}

It will finish flashing after pressing.

EDIT:

You can define your animation in xml (use objectAnimator):

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="textColor"
    android:duration="500"
    android:valueFrom="#000000"
    android:valueTo="@android:color/transparent"
    android:repeatMode="reverse"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/accelerate_interpolator" />

and use it in your code:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(this, R.animator.blink);
    final Button btn = (Button) findViewById(R.id.btn);
    animator.setTarget(btn);
    animator.start();

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            animator.end();
            animator.cancel();
        }
    });
}

XML must be in the 'animator' folder.

antonid
  • 178
  • 1
  • 9
  • Thanks for this. Is it possible to use my **R.anim.blink.xml** with `ObjectAnimator`? – ban-geoengineering Aug 22 '17 at 13:09
  • @ban-geoengineering, yes, of course. check edited answer. – antonid Aug 22 '17 at 14:34
  • Thank, but your new answer makes the entire button blink. I need just the text to blink... – ban-geoengineering Aug 22 '17 at 16:24
  • @ban-geoengineering in my case only the text blinks. Are you sure that your xml and a code coincide with mine? What api-level are you using? – antonid Aug 23 '17 at 09:45
  • My code was the same as yours. Can you also post your **R.animator.blink.xml** file so I can make sure that is the same? – ban-geoengineering Aug 23 '17 at 18:56
  • @ban-geoengineering XML in edited answer is R.animator.blink.xml. You don't need use R.anim. Use R.animator instead – antonid Aug 23 '17 at 19:05
  • Ahhh, thanks. Problem was that I had `android:propertyName="alpha"` in my **R.animator.blink.xml** file instead of `android:propertyName="textColor"` . :-/ – ban-geoengineering Aug 23 '17 at 19:56
  • Well, it's working great on one device - API 22. But when I try it on an API 19 device, instead of the text changing between two colours, it rapidly goes through range of multi-colours. I tried adding `android:valueType="colorType"` to **R.animator.blink.xml**, but it didn't help. Any ideas? – ban-geoengineering Aug 23 '17 at 23:26
2

Try this code in oncreate method of Activity

    final Animation animation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible
    animation.setDuration(500); // duration - half a second
    animation.setInterpolator(new LinearInterpolator()); // do not alter animation rate
    animation.setRepeatCount(Animation.INFINITE); // Repeat animation infinitely
    animation.setRepeatMode(Animation.REVERSE); // Reverse animation at the end so the button will fade back in
    final Button btn = (Button) findViewById(R.id.your_btn);
    btn.startAnimation(animation);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View view) {
            view.clearAnimation();
            //also your extra work here
        }
    });
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
Bapusaheb Shinde
  • 839
  • 2
  • 13
  • 16
0

I suggest you use FrameLayout instead.

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:background="any color" >

    <TextView
        android:id="@+id/bn1"
        android:layout_width="wrap_content"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:layout_height="wrap_content"
        android:text="some_txt"/>

</FrameLayout>

Now Apply Blink Animation that TextView

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
0

in kotlin we can do that. blink the element res\animator\blink.xml

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="alpha"
    android:duration="1000"
    android:valueFrom="1.0"
    android:valueTo="0.1"
    android:repeatMode="reverse"
    android:repeatCount="infinite"
    android:interpolator="@android:anim/accelerate_interpolator" /> 

in the activity.kt

  lateinit var animator : ObjectAnimator

onCreate

animator = AnimatorInflater.loadAnimator(this, R.animator.blink) as ObjectAnimator
animator.target = targetElement
animator.start()

to stop and cancel

animator.end()
animator.cancel()
targetElement?.alpha = 1.0f

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

blink only text

val valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f)
valueAnimator.duration = 1000
valueAnimator.repeatCount = ValueAnimator.INFINITE
valueAnimator.repeatMode = ValueAnimator.REVERSE
    valueAnimator.addUpdateListener { it ->
    val fractionAnim = it.animatedValue as Float
    targetElement?.setTextColor(
        ColorUtils.blendARGB(Color.parseColor("#00cc00"),
        resources.getColor(R.color.transparent), fractionAnim))
    }
valueAnimator.start()

targetElement?.setOnClickListener{
    valueAnimator.cancel()
    targetElement?.setTextColor(Color.parseColor("#00cc00"))
}
AllanRibas
  • 678
  • 5
  • 14
-1

blink_effect.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="600"
        android:repeatMode="reverse"
        android:repeatCount="infinite"/>
</set>

MainActivity.java

 Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(),
                        R.anim.blink_effect);
 yourWidget.startAnimation(animation1);
EtherPaul
  • 424
  • 10
  • 19