我有类似于this的问题,但我想只让按钮上的文字闪烁。我不希望按钮背景也闪烁。
这是我的 R.anim.blink.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"
android:startOffset="20"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
但是这段代码......
Animation blinkingAnimation = AnimationUtils.loadAnimation(this, R.anim.blink);
myButton.setAnimation(blinkingAnimation);
...使整个按钮闪烁。那么如何让文本闪烁(所以按钮背景一直显示)?
答案 0 :(得分:2)
简单方法:
@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();
}
});
}
按下后将完成闪烁。
修改强>
您可以在xml中定义动画(使用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" />
并在您的代码中使用它:
@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必须位于&#39;动画师&#39;文件夹中。
答案 1 :(得分:0)
在Activity的oncreate方法中尝试此代码
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
}
});
答案 2 :(得分:0)
我建议你改用FrameLayout
。
<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>
现在应用TextView
答案 3 :(得分:0)
在 kotlin 中,我们可以做到这一点。 闪烁元素 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" />
在activity.kt中
lateinit var animator : ObjectAnimator
onCreate
animator = AnimatorInflater.loadAnimator(this, R.animator.blink) as ObjectAnimator
animator.target = targetElement
animator.start()
停止和取消
animator.end()
animator.cancel()
targetElement?.alpha = 1.0f
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ ↓↓↓↓↓↓↓
仅闪烁文本
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"))
}
答案 4 :(得分:-2)
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);