gpt4 book ai didi

Android颜色渐变根据值变化

转载 作者:行者123 更新时间:2023-11-30 00:59:00 25 4
gpt4 key购买 nike

我正在开发一个安卓天气应用程序,我需要一张卡片的背景具有适合温度的颜色(蓝色代表寒冷,红色代表炎热),但我逐渐需要它,比如 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);
}
}

最佳答案

我设法找到了这个解决方案,可能不是最好的。

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

关于Android颜色渐变根据值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39724441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com