Jungs, ich habe eine Textansicht, die ich brauche, um zu blinken, bitte hilf mir dabei.
<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"/>
Ich möchte, dass der Google-Text blinkt
Es ist eine veraltete Antwort auf Android vor der 3.0
-Version. Bitte verwenden Sie die Antwort von SolArabehety oder schauen Sie sich den this - Thread an.
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"/>
Sie können dies verwenden:
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);
Es ist die gleiche Antwort, die ich in diesem Beitrag gegeben habe Blinkender Text in der Android-Ansicht
Hoffe das hilft!
Verwenden Sie dazu XML-Animationen:
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>
Blinzeln Aktivität: so verwenden: -
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) {
}
}
Lassen Sie mich wissen, wenn Sie Fragen haben.
Erstellen Sie eine AlphaAnimation und wenden Sie sie auf die Textansicht in der Aktivität an, in der Sie die Textansicht einrichten. Das Blinken würde durch Wiederholen einer Animation von 1,0 Alpha bis 0,0 Alpha bis 1,0 Alpha erreicht.
Bearbeiten: Google provideth .
Dafür brauche ich keine. Nur Alpha:
anim/flash_leave_now.xml
<?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"/>
Und im Code:
mTextView.setAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.flash_leave_now));
Höflichkeit zur ersten Antwort, das habe ich getan:
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--;
}
}
});
}
};
dann irgendwo auf dem Code:
timer = new Timer();
timer.scheduleAtFixedRate(textBlink,0,500);
Dies bewirkt einen Blinkeffekt für 5 Sekunden. Empfohlen für, wenn Sie den Effekt fadeIn-fadeOut nicht möchten.
Sie können eine Animation erstellen oder vielleicht machen Sie es nicht mit einem Timer View.VISIBLE und View.INVISIBLE? Ich denke, der bessere Weg ist die Animation mit Alpha in der Tat :)
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()
}
Benutze einfach das <blink/>
Etikett.
<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>
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();
}
}