Android颜色渐变基于值更改

时间:2016-09-27 12:16:47

标签: android colors

我正在开发一款Android天气应用程序,我需要一张卡片的背景才能获得适合温度的颜色(蓝色表示冷,红色表示热)但我需要逐渐增加,如40度是鲜红色,但是20度像黄色一样。

你能帮我一个颜色选择方法,帮助我做到这一点吗?

代码:

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

}

2 个答案:

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

答案 1 :(得分:0)

在不同透明度的帮助下,您可以通过

进行管理
rotate