如何在旧 android 设备上应用闪烁动画或效果?
How to apply Blink animation or effect on old android devices?
我使用下面发布的代码来制作闪烁动画,但是这段代码仍然有问题,如果用户在按钮上单击 2 次,闪烁会变得更快。以及停止线程是无效的。
boolean myThreadAlive = false;
private void blink(final View txt){
if(myBestThread != null){
if(!myThreadAlive) {
myThreadAlive = false;
if (myBestThread.isAlive()) {
myBestThread.interrupt();
try {
myBestThread.join();
} catch (InterruptedException e) {
}
}
}
}
myBestThread = new Thread(new Runnable() {
@Override
public void run() {
final int timeToBlink = 1000; //in milissegunds
myThreadAlive= true;
while(myThreadAlive) {
try {Thread.sleep(timeToBlink);}catch(Exception e){}
handler.post(new Runnable() {
@Override
public void run() {
if (txt.getVisibility() == View.VISIBLE) {
txt.setVisibility(View.INVISIBLE);
} else {
txt.setVisibility(View.VISIBLE);
}
}
});
}
handler.post(new Runnable() {
@Override
public void run() {
txt.setVisibility(View.VISIBLE);}});
}
});
myBestThread.start();
}
还有其他解决方案吗?
您可以使用动画 class 来实现此效果,如果没有,只需在您的 res 目录中创建一个 anim 文件夹,然后创建一个 blink.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="100"
android:repeatCount="infinite"
android:interpolator="@android:anim/accelerate_interpolator"
/>
</set>
然后在你的 jave 中,无论你想要什么动画,只要调用这个
Animation anim=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.blink);
textView.startAnimation(anim);
只需在 onCLick 中调用这些行,或者当您希望动画在 blink.xml 中开始时,您可以更改持续时间以使动画变慢或变快
我使用下面发布的代码来制作闪烁动画,但是这段代码仍然有问题,如果用户在按钮上单击 2 次,闪烁会变得更快。以及停止线程是无效的。
boolean myThreadAlive = false;
private void blink(final View txt){
if(myBestThread != null){
if(!myThreadAlive) {
myThreadAlive = false;
if (myBestThread.isAlive()) {
myBestThread.interrupt();
try {
myBestThread.join();
} catch (InterruptedException e) {
}
}
}
}
myBestThread = new Thread(new Runnable() {
@Override
public void run() {
final int timeToBlink = 1000; //in milissegunds
myThreadAlive= true;
while(myThreadAlive) {
try {Thread.sleep(timeToBlink);}catch(Exception e){}
handler.post(new Runnable() {
@Override
public void run() {
if (txt.getVisibility() == View.VISIBLE) {
txt.setVisibility(View.INVISIBLE);
} else {
txt.setVisibility(View.VISIBLE);
}
}
});
}
handler.post(new Runnable() {
@Override
public void run() {
txt.setVisibility(View.VISIBLE);}});
}
});
myBestThread.start();
}
还有其他解决方案吗?
您可以使用动画 class 来实现此效果,如果没有,只需在您的 res 目录中创建一个 anim 文件夹,然后创建一个 blink.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="100"
android:repeatCount="infinite"
android:interpolator="@android:anim/accelerate_interpolator"
/>
</set>
然后在你的 jave 中,无论你想要什么动画,只要调用这个
Animation anim=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.blink);
textView.startAnimation(anim);
只需在 onCLick 中调用这些行,或者当您希望动画在 blink.xml 中开始时,您可以更改持续时间以使动画变慢或变快