2016-06-26 12 views
0

Ich habe eine Taste btnSwitch und ich möchte das Blitzlicht blinken lassen und die Hintergrundfarbe kontinuierlich ändern, solange ich die Taste halte und damit aufhören, wenn ich Lassen Sie meinen Finger los, die Farben für den Hintergrund sind weiß und schwarz, der Blitz funktioniert richtig, aber der Hintergrund ändert nur einmal das Schwarz und kehrt nie wieder zu Weiß zurück.Wie ändere ich relativeLayout Hintergrundfarbe von innerhalb eines Threads

Dies ist der Code:

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); 
        }} 
      }; 
     }); 
    } 

und dies ist die XML-Datei

<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" 
    /> 

Antwort

3

Sie müssen relativeLayout.setBackgroundColor(color); auf der UiThread nennen:

activity.runOnUiThread(new Runnable() { 
     @Override 
      public void run() { 
       relativeLayout.setBackgroundColor(color); 
    } 
}); 
+0

Samen probleme am zweiten r unonUIThraed der Hintergrund wird schwarz dann wird der erste runonUIThread nie wieder ausgeführt –

+0

Es ist nur eine Idee - aber versuche 7 bis 1000 zu testen - vielleicht funktioniert dein Code aber 7ms ist zu kurz für das menschliche Auge, um die Farbänderungen zu erkennen? – Katharina

Verwandte Themen