伙计们,我有一个文本视图,我需要它闪烁,请帮助我。
<TextView
android:id="@+id/usage"
android:layout_marginTop="220dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="15dip"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google "
android:textColor="#030900"/>
我希望Google文字闪烁
答案 0 :(得分:169)
您可以使用:
TextView myText = (TextView) findViewById(R.id.myText );
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the blinking time with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
myText.startAnimation(anim);
这与我在这篇文章Blinking Text in android view
中给出的答案相同希望这有帮助!
答案 1 :(得分:24)
为此目的使用XML动画:
<强> R.anim.blink 强>
<?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>
眨眼活动:像这样使用它: -
public class BlinkActivity extends Activity implements AnimationListener {
TextView txtMessage;
Button btnStart;
// Animation
Animation animBlink;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blink);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animBlink = AnimationUtils.loadAnimation(this,
R.anim.blink);
// set animation listener
animBlink.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animBlink);
}
});
}
@Override
public void onAnimationEnd(Animation animation) {
// Take any action after completing the animation
// check for blink animation
if (animation == animBlink) {
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
如果您有任何疑问,请告诉我。
答案 2 :(得分:23)
在3.0
版本之前,这是Android的弃用答案,请使用SolArabehety的答案或查看this主题。
package teste.blink;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
public class TesteBlinkActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
blink();
}
private void blink(){
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
int timeToBlink = 1000; //in milissegunds
try{Thread.sleep(timeToBlink);}catch (Exception e) {}
handler.post(new Runnable() {
@Override
public void run() {
TextView txt = (TextView) findViewById(R.id.usage);
if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
}
<TextView
android:id="@+id/usage"
android:layout_marginTop="220dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="15dip"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google "
android:textColor="#030900"/>
答案 3 :(得分:9)
创建AlphaAnimation并将其应用于您设置textview的活动中的textview。闪烁将通过重复从1.0 alpha到0.0 alpha到1.0 alpha的动画来完成。
修改 :Google provideth。
答案 4 :(得分:3)
不需要为此做好准备。只是alpha:
动画/ flash_leave_now.xml
h.map { |k,l| l.map { |v| [ k,v ] } }.reduce(&:zip).map(&:to_h)
在代码中:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="900"
android:fromAlpha="1.0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="0.2"/>
答案 5 :(得分:2)
只需使用<blink/>
标签即可。
<blink
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/usage"
android:layout_marginTop="220dip"
android:layout_marginLeft="45dip"
android:layout_marginRight="15dip"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Google "
android:textColor="#030900"/>
</blink>
答案 6 :(得分:0)
你可以制作一个动画,或者为什么不用定时器使它成为View.VISIBLE和View.INVISIBLE?我认为更好的方法是使用alpha的动画确实:)
答案 7 :(得分:0)
礼貌地回答,这就是我所做的:
textBlink = new TimerTask() {
int countdown = 10;
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (countdown <= 0) {
timer.cancel();
textview.setVisibility(View.GONE);
} else {
textview.setVisibility(textview.getVisibility() == View.VISIBLE?View.GONE:View.VISIBLE);
countdown--;
}
}
});
}
};
然后在代码的某处:
timer = new Timer();
timer.scheduleAtFixedRate(textBlink,0,500);
这将持续5秒闪烁效果。如果你不想要fadeIn-fadeOut效果,建议使用。
答案 8 :(得分:0)
private fun blink() {
val handler = Handler()
Thread(Runnable {
val timeToBlink = 500 //in milissegunds
try {
Thread.sleep(timeToBlink.toLong())
} catch (e: Exception) {
}
handler.post(Runnable {
if (usage.visibility == View.VISIBLE) {
usage.visibility = View.INVISIBLE
} else {
usage.visibility = View.VISIBLE
}
blink()
})
}).start()
}
答案 9 :(得分:0)
public final class BlinkEffectUtils {
private static BlinkEffectUtils blinkEffect;
public enum PROPERTY_TYPE {
BACKGROUND_COLOR,
TEXT_COLOR
}
private BlinkEffectUtils() {
}
public static BlinkEffectUtils getInstance(Context context) {
if (blinkEffect == null) {
blinkEffect = new BlinkEffectUtils();
}
return blinkEffect;
}
public void setBlinkEffect(Object targetView, PROPERTY_TYPE property_type, int duration, int defaultColor, int effectColor) {
String propertyName = "";
switch (property_type) {
case TEXT_COLOR:
propertyName = "textColor";
break;
case BACKGROUND_COLOR:
propertyName = "backgroundColor";
break;
}
@SuppressLint("ObjectAnimatorBinding")
ObjectAnimator anim = ObjectAnimator.ofInt(targetView, propertyName,
effectColor,
defaultColor);
anim.setDuration(duration);
anim.setEvaluator(new ArgbEvaluator());
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.start();
}
}
答案 10 :(得分:-1)
这是我使用alpha动画的帮助器实现:
public void blinkText(final TextView text_to_animate, int durationMillis) {
final AlphaAnimation fade_out = new AlphaAnimation(1.0f, 0.0f);
//ScaleAnimation scale_it = new ScaleAnimation(1.0f, 1.25f, 1.0f, 1.25f);
fade_out.setDuration(durationMillis);
final AlphaAnimation fade_in = new AlphaAnimation(0.0f, 1.0f);
//ScaleAnimation scale_it = new ScaleAnimation(1.0f, 1.25f, 1.0f, 1.25f);
fade_in.setDuration(durationMillis);
fade_out.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
if (recording == true)
text_to_animate.startAnimation(fade_in);
}
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
fade_in.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation arg0) {
// TODO Auto-generated method stub
if (recording == true)
text_to_animate.startAnimation(fade_out);
}
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
text_to_animate.startAnimation(fade_out);
}