2016-09-27 104 views
1

我正在開發一款android天氣應用程序,我需要一張卡片的背景以使其具有足夠的顏色以適應溫度(藍色表示冷,紅色表示熱度),但我需要逐漸使用它40度爲鮮紅色,20度爲淡黃色。基於值的Android顏色漸變變化

你能幫我選擇一種顏色選擇方法嗎?

代碼:

public class WeatherContentAdapter extends RecyclerView.Adapter<WeatherContentAdapter.WeatherContentVH> { 
private List<WeatherInfo> mWeatherInfoList; 

public WeatherContentAdapter() { 
    this.mWeatherInfoList = new ArrayList<>(); 
    mWeatherInfoList.add(new DailyWeatherInfo("Monday", 10, WeatherConditions.RAINY)); 
    mWeatherInfoList.add(new DailyWeatherInfo("Tuesday", 15, WeatherConditions.RAINY)); 
    mWeatherInfoList.add(new DailyWeatherInfo("Wednesday", 20, WeatherConditions.FOGGY)); 
    mWeatherInfoList.add(new DailyWeatherInfo("Thursday", 25, WeatherConditions.CLOUDY)); 
    mWeatherInfoList.add(new DailyWeatherInfo("Friday", 30, WeatherConditions.CLOUDY)); 
    mWeatherInfoList.add(new DailyWeatherInfo("Saturday", 35, WeatherConditions.CLEAR)); 
    mWeatherInfoList.add(new DailyWeatherInfo("Sunday", 40, WeatherConditions.CLEAR)); 
} 

@Override 
public WeatherContentVH onCreateViewHolder(ViewGroup parent, int viewType) { 
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext()); 
    View inflatedView = layoutInflater.inflate(R.layout.item_content, parent, false); 
    return new WeatherContentVH(inflatedView); 
} 

@Override 
public void onBindViewHolder(WeatherContentVH holder, int position) { 
    holder.mWeatherTempTV.setText(mWeatherInfoList.get(position).mTemperature + "Celsius degrees"); 
    holder.mWeatherCard.setCardBackgroundColor(???); 
} 

@Override 
public int getItemCount() { 
    return mWeatherInfoList.size(); 
} 

class WeatherContentVH extends RecyclerView.ViewHolder { 
    TextView mWeatherTempTV; 
    CardView mWeatherCard; 

    public WeatherContentVH(View itemView) { 
     super(itemView); 

     mWeatherTempTV = (TextView) itemView.findViewById(R.id.ic_tv_weather_temp); 
     mWeatherCard = (CardView) itemView.findViewById(R.id.ic_cv_weather); 
    } 
} 

}

+0

向我們展示您的工作...任何代碼? – FiN

+0

這更像是一個測試代碼示例 –

回答

1

我設法找到這一解決方案進行管理,可能不是最好的。

public static int getTemperatureColor(int mTemp) { 
    if (mTemp <= 10) { 
     return getColdTemperatureColor(mTemp); 
    } else if (mTemp > 10 && mTemp <= 15) { 
     return getMediumTemperatureColor(mTemp); 
    } else if (mTemp > 15 && mTemp <= 20) { 
     return getWarmTemperatureColor(mTemp); 
    } else { 
     return getHotTemperatureColor(mTemp); 
    } 
} 

/** 
* Get a color with blue 255, red 0 and green value variable(10temp=250) 
* 10 temp values included above 0 
* 
* @return Color value 
*/ 
private static int getColdTemperatureColor(int mTemp) { 
    if (mTemp < 0) 
     return Color.rgb(0, 0, 255); 
    else { 
     int greenValue = 25 * mTemp; 
     return Color.rgb(0, greenValue, 255); 
    } 
} 

/** 
* Get a color with green 255, red 0 and blue value variable(15temp=0) 
* 5 temp values included 
* 
* @return Color value 
*/ 
public static int getMediumTemperatureColor(int mTemp) { 
    int blueValue = 255 - (mTemp - 10) * 51; 
    return Color.rgb(0, 255, blueValue); 
} 

/** 
* Get a color with red 255, blue 0 and green value variable(40temp=255) 
* 20 temp values included 
* 
* @return Color value 
*/ 
public static int getWarmTemperatureColor(int mTemp) { 
    int redValue = (mTemp - 15) * 51; 
    return Color.rgb(redValue, 255, 0); 
} 

public static int getHotTemperatureColor(int mTemp) { 
    if(mTemp >40){ 
     return Color.rgb(255, 0, 0); 
    } else { 
     int greenValue = 255 - (mTemp - 20) * 12; 
     return Color.rgb(255, greenValue, 0); 
    } 
} 
0

在不同透明度的幫助下,你可以通過

100% — FF 
95% — F2 
90% — E6 
85% — D9 
80% — CC 
75% — BF 
70% — B3 
65% — A6 
60% — 99 
55% — 8C 
50% — 80 
45% — 73 
40% — 66 
35% — 59 
30% — 4D 
25% — 40 
20% — 33 
15% — 26 
10% — 1A 
5% — 0D 
0% — 00 



#ff0074 for Pink 
#33ff0074 for Light pink