build SweepGradient - Android android.graphics

Android examples for android.graphics:SweepGradient

Description

build SweepGradient

Demo Code


import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.SweepGradient;
import android.preference.PreferenceManager;
import android.util.Log;
import java.util.Calendar;

public class Main{
    public static SweepGradient buildGradient(int width, int height,
            Context mContext) {/*from   w  w  w. java  2  s . c  om*/
        String TimeColor = getCurrentTimeColor();
        int StartColor = getStartColor(mContext);
        int EndColor = Color.parseColor(getEndColor(TimeColor));
        int[] Colors = { StartColor, EndColor };
        float[] Poses = { 0, 1 };
        SweepGradient SG = new SweepGradient(width / 2, height / 2, Colors,
                Poses);
        return SG;
    }
    static String getCurrentTimeColor() {
        Calendar c = Calendar.getInstance();
        String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
        String min = String.valueOf(c.get(Calendar.MINUTE));
        String sec = String.valueOf(c.get(Calendar.SECOND));

        //Check if length is 1
        if (hour.length() < 2) {
            hour = "0" + hour;
        }
        if (min.length() < 2) {
            min = "0" + min;
        }
        if (sec.length() < 2) {
            sec = "0" + sec;
        }
        String time = hour + min + sec;
        Log.d("TIMECOLOR", time);
        return time;
    }
    public static int getStartColor(Context mContext) {
        SharedPreferences mPref = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        Boolean isNoti = mPref.getBoolean("noti", false);
        Log.d("isNoti", String.valueOf(isNoti));
        if (isNoti) {
            int color = mPref.getInt("appcolor", Color.BLACK);
            mPref.edit().putBoolean("noti", false).apply();
            return color;
        } else {
            return Color
                    .parseColor(mPref.getString("color_end", "#000000"));
        }
    }
    public static String getEndColor(String mColor) {
        String time = "#ff" + mColor;
        return time;
    }
}

Related Tutorials