在Custom LinearLayout中每X秒执行一次代码

时间:2016-03-30 08:56:33

标签: android android-custom-view

我想制作一个自定义的线性布局,每5秒改变一次自己的背景颜色并使用渐变动画(从上到下顺利) 我写这段代码但不起作用

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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:-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 (); 
    } 
}