2016-03-30 26 views
-2

我要特別強調每5秒改變自己的背景顏色自定義的線性佈局,並用梯度動畫做(平滑地從頂部向下) 我寫這篇文章的代碼,但不工作做代碼每隔X秒定製的LinearLayout

MainActivity:

package com.idk.book; 

import android.app.Activity; 
import android.os.Bundle; 

import com.idk.pro.ProLinearLayout; 

/** 
* Created by mohamad on 3/24/2016. 
*/ 
public class Main extends Activity { 

    ProLinearLayout mProLinearLayout; 
    String backgroundColors[] = { 
      "#e57fe5", 
      "#FF6D46", 
      "#85EF8C", 
      "#FFF78E" 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_activity); 
     mProLinearLayout = (ProLinearLayout) findViewById(R.id.lay_mainactivity_background); 
     mProLinearLayout.setBackgroundColors(backgroundColors); 
     mProLinearLayout.startAnimatingBackgroundColor(); 
    } 
} 

ProLinearLayout類:

package com.idk.pro; 

import android.content.Context; 
import android.graphics.Color; 
import android.graphics.drawable.GradientDrawable; 
import android.util.AttributeSet; 
import android.widget.LinearLayout; 

import java.io.InterruptedIOException; 
import java.lang.reflect.Array; 
import java.util.Arrays; 
import java.util.Random; 

/** 
* Created by mohamad on 3/29/2016. 
*/ 
public class ProLinearLayout extends LinearLayout { 

    private boolean BackgroundAnimationState = false; 
    private int backgroundColors[]; 
    private int currentColor; 

    public ProLinearLayout(Context context) { 
     super(context); 
     init(context); 
    } 

    public ProLinearLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    private void init(Context context) { 
     //do stuff that was in your original constructor... 
    } 

    public void setBackgroundColors(String[] colors) { 
     int colorsTemp[] = new int [colors.length]; 
     for (int i = 0; i < colors.length-1; i++) { 
      colorsTemp[i] = Color.parseColor(colors[i]); 
     } 
     backgroundColors = Arrays.copyOf(colorsTemp, colorsTemp.length); 
    } 

    public void startAnimatingBackgroundColor() { 
     BackgroundAnimationState = true; 
     animateBackgroundColor(); 
    } 

    public void stopAnimatingBackgroundColor() { 
     BackgroundAnimationState = false; 
    } 

    private void animateBackgroundColor() { 
     currentColor = backgroundColors[0]; 
     while (BackgroundAnimationState) { 
      Random ran = new Random(); 
      final int firstColor = currentColor; 
      final int secondColor = backgroundColors[ran.nextInt(backgroundColors.length)]; 
      Thread mThread = new Thread() { 
       public void run() { 
        try { 
         GradientDrawable mGradientDrawable; 
         int c1, c2; 
         int cs[] = new int[2]; 
         int r1, g1, b1, r2, g2, b2, r, g, b; 
         r1 = Color.red(firstColor); 
         g1 = Color.green(firstColor); 
         b1 = Color.blue(firstColor); 
         r2 = Color.red(secondColor); 
         g2 = Color.green(secondColor); 
         b2 = Color.blue(secondColor); 
         c1 = firstColor; 
         for (int j = 0; j < 25; j++) { 
          r = (r1 * (24 - j) + r2 * j)/24; 
          g = (g1 * (24 - j) + g2 * j)/24; 
          b = (b1 * (24 - j) + b2 * j)/24; 
          c2 = Color.rgb(r, g, b); 
          cs[0] = c1; 
          cs[1] = c2; 
          mGradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, cs); 
          setBackgroundDrawable(mGradientDrawable); 
          sleep(200); 
          c1 = c2; 
         } 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } finally { 
         currentColor = secondColor; 
        } 
       } 
      }; 
      mThread.start(); 
     } 
    } 
} 
+0

'this code but not work':你需要更具體。什麼不起作用? – AADProgramming

+0

不會崩潰。只是一個黑色的屏幕,幾秒鐘後關閉 –

+0

,因爲你正在運行它的while循環執行FOREVER – k0sh

回答

-1

試試這個:

private void animateBackgroundColor() { 
    currentColor = backgroundColors [ 0 ]; 
    if (BackgroundAnimationState) { 
     Random ran = new Random(); 
     final int firstColor = currentColor ; 
     final int secondColor = backgroundColors [ ran . nextInt (backgroundColors . length)]; 
     Thread mThread = new Thread() { 
      public void run() { 
       try { 
        GradientDrawable mGradientDrawable ; 
        int c1 , c2 ; 
        int cs [] = new int [ 2 ]; 
        int r1 , g1 , b1 , r2 , g2 , b2 , r , g , b ; 
        r1 = Color . red (firstColor); 
        g1 = Color . green (firstColor); 
        b1 = Color . blue (firstColor); 
        r2 = Color . red (secondColor); 
        g2 = Color . green (secondColor); 
        b2 = Color . blue (secondColor); 
        c1 = firstColor ; 
        for (int j = 0 ; j < 25 ; j ++) { 
         r = (r1 * (24 - j) + r2 * j)/24 ; 
         g = (g1 * (24 - j) + g2 * j)/24 ; 
         b = (b1 * (24 - j) + b2 * j)/24 ; 
         c2 = Color . rgb (r , g , b); 
         cs [ 0 ] = c1 ; 
         cs [ 1 ] = c2 ; 
         mGradientDrawable = new GradientDrawable (GradientDrawable . Orientation . TOP_BOTTOM , cs); 
         setBackgroundDrawable (mGradientDrawable); 
         c1 = c2 ; 
        } 
       } catch (InterruptedException e) { 
        e . printStackTrace(); 
       } finally { 
        currentColor = secondColor ; 
       } 
       sleep (5000); 
       animateBackgroundColor(); 
      } 
     }; 
     mThread . start(); 
    } 
} 
0

可以使用Handler.postDelayed(long milliseconds)例如;

private Handler handler = new Handler(); 

private Runnable runnable = new Runnable() { 
    @Override public void run() { 
     //do your animation and stuff 
     runEveryFiveSeconds();//call this method to execute every 5 seconds. 
    } 
}; 

private void runEveryFiveSeconds() { 
    handler.postDelayed(runnable, 50000); 
} 

@Override protected void onDetachedFromWindow() { 
    // good way to remove the runnable and stop the handle from executing when the view is Detached from the activity 
    handler.removeCallbacks(runnable); 
    handler = null; 
    super.onDetachedFromWindow(); 
} 

public void startAnimating() { //when you wanna start ur animation call this method so the handler start executing and repeats every 5 seconds. 
    handler.postDelayed(runnable, 0); 
}