0
私はボタンbtnSwitch
を持っています。私はボタンを押している間、フラッシュライトを点滅させ、背景色を連続的に変化させたいと思います。私の指を離すと、背景の色は白と黒、フラッシュは正常に機能していますが、背景は一度だけ黒に変わり、決して白に戻りません。スレッド内からrelativeLayoutの背景色を変更する方法
これはコードです:
public class MainActivity extends AppCompatActivity {
public static RelativeLayout relativeLayout;
public Button btnSwitch;
btnSwitch.setOnTouchListener(new View.OnTouchListener() {
private Handler handler;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
if (handler != null) return true;
handler = new Handler();
handler.postDelayed(thread, 14);
break;
case MotionEvent.ACTION_UP:
if (handler == null) return true;
handler.removeCallbacks(thread);
handler = null;
break;
}
return false;
}
Runnable thread= new Runnable() {
@Override
public void run() {
synchronized (this) {
try {
this.wait(7);
relativeLayout.setBackgroundColor(Color.BLACK);
turnonflash();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
this.wait(7);
relativeLayout.setBackgroundColor(Color.WHITE);
turnofflash();
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.postDelayed(this, 14);
}}
};
});
}
と、これはXMLファイルである
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/backv">
<Button
android:id="@+id/btnSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dip"
android:text="switch"
android:contentDescription="@null"
/>
同じprobleme 2番目のrにunonUIThraed背景が黒くなってから最初のrunonUIThreadが決して実行されない –
単なるアイデアですが、テストのために7〜1000を設定してみてください。おそらくあなたのコードは動作しますが、人間の目が色の変化を認識するのに、 – Katharina