gpt4 book ai didi

java - Android Activity 方法使显示屏闪烁白色以发出警告通知

转载 作者:行者123 更新时间:2023-12-02 11:32:33 26 4
gpt4 key购买 nike

我正在尝试让 Android Activity 闪烁,以向用户发出警告(全屏)。为了使其闪烁,我在 Activity 中插入了一个 ImageView,它的大小与显示器的大小相同,并且是白色的,这会将其可见性从可见旋转到消失。我曾研究过创建外部线程,但后来发现外部线程无法更改 UI 组件。我需要在 flash 更改之间暂停线程,这意味着我无法将其放在主 UI 线程上。我该如何实现这样的事情?

我在下面包含了一些代码:

XML

<ImageView
android:id="@+id/alertScreen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:srcCompat="@android:color/white" />

Main.java

    public static boolean flashOn = false;
public static boolean endFlash = false;
public static Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aircraft_main);
AircraftMain.context = getApplicationContext();

flash = (Button) findViewById(R.id.flashTest);
flash.setOnClickListener(flashBtnOnClickListener);
}

View.OnClickListener flashBtnOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View arg0) {

AsyncTask.execute(new Runnable() {
public void run(){
AlertFlash alert = new AlertFlash(3);
alert.run();

ImageView v = findViewById(R.id.alertScreen);

while (endFlash != true) {
if(flashOn = true) {
v.setVisibility(View.VISIBLE);
} else {
v.setVisibility(View.GONE);
}
}
}
});

}
};

AlertFlash.java

public class AlertFlash implements Runnable {

public static volatile int flashes;
boolean suspendFlag, end;

public AlertFlash (int flashNo) {
flashes = flashNo * 2;
suspendFlag = false;
end = false;
}

public void run() {
for (int i = 0; i<=flashes; i++) {

if (suspendFlag = false) {
AircraftMain.flashOn = true;
suspendFlag = true;
}
else {
AircraftMain.flashOn = false;
suspendFlag = false;
}

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
end = true;
}
}

最佳答案

使用Activity.runOnUiThread() ,您可以从后台线程修改 UI 元素。

以下是针对您的情况我会采取的方法。在 Main.java 中,我首先创建一个简单的方法来修改传入 View 的可见性,特别是在 UI 线程上:

private void setViewVisibility(final View view, final int visibility) {
runOnUiThread(new Runnable() {
@Override
public void run() {
view.setVisibility(visibility);
}
});
}

然后我会更改 while循环到以下内容:

while (endFlash != true) {
if(flashOn = true) {
setViewVisibility(v, View.VISIBLE);
} else {
setViewVisibility(v, View.GONE);
}
}

编辑:为了防止 while 循环出现问题,我们可以通过传入 activity 来完全绕过它。和view创建 AlertFlash 时目的。见下文。

public class AlertFlash implements Runnable {

public static volatile int flashes;
boolean suspendFlag, end;

private Activity activity;
private View view;

public AlertFlash (int flashNo, Activity activity, View view) {
flashes = flashNo * 2;
suspendFlag = false;
end = false;

this.activity = activity;
this.view = view;
}

public void run() {
for (int i = 0; i<=flashes; i++) {

if (suspendFlag = false) {
AircraftMain.flashOn = true;
suspendFlag = true;
}
else {
AircraftMain.flashOn = false;
suspendFlag = false;
}

final int visibility = AircraftMain.flashOn ? View.VISIBLE : View.GONE;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
view.setVisibility(visibility);
}
});

try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}

}
end = true;
}
}

关于java - Android Activity 方法使显示屏闪烁白色以发出警告通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49195689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com