0
버튼이 btnSwitch
이고 버튼을 누른 상태에서 플래시를 깜박 거리고 배경색을 계속 변경하고 싶을 때 내 손가락을 뗍니다. 배경색은 흰색과 검정색으로 플래시가 작동하지만 배경은 한 번만 검정색으로 바뀌고 결코 흰색으로 되돌아 가지 않습니다. thread 내에서 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"
/>
당신은 UiThread에
relativeLayout.setBackgroundColor(color);
를 호출 할 필요가
같은 probleme 두 번째 r에 unonUIThraed 배경이 검은 색이되면 첫 번째 runonUIThread가 다시 실행되지 않습니다 –
테스트 용으로 7 ~ 1000을 설정하십시오. 코드가 작동하지만 인간의 눈이 색상 변경을 인식하기에는 너무 짧습니다. – Katharina