package com.gh.common.util; import android.content.Context; import android.graphics.Color; import android.graphics.drawable.GradientDrawable; import android.text.TextUtils; import android.util.TypedValue; import android.view.View; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; import androidx.core.content.ContextCompat; import com.gh.common.view.DrawableView; import com.gh.gamecenter.R; import com.gh.gamecenter.entity.TagStyleEntity; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Locale; import java.util.TimeZone; /** * @author 温冠超 * @email 294299195@qq.com * @date 2015-8-14 * @update 2015-8-14 * @des 设置列表中game item视图上的工具类 */ public class GameViewUtils { public static void setLabelList(Context context, LinearLayout labelLayout, List tagStyle) { setLabelList(context, labelLayout, tagStyle, 8); } public static void setLabelList(Context context, LinearLayout labelLayout, List tagStyle, int margin) { labelLayout.removeAllViews(); if (tagStyle == null || tagStyle.isEmpty()) { // 没有数据的话默认不显示 // TagStyleEntity tagEntity = new TagStyleEntity(); // tagEntity.setName("官方版"); // labelLayout.addView(getNewGameTagView(context, tagEntity, 0)); } else { for (int i = 0, size = tagStyle.size(); i < size; i++) { View view = getNewGameTagView(context, tagStyle.get(i), i == size - 1 ? 0 : DisplayUtils.dip2px(context, margin)); labelLayout.addView(view); if (labelLayout.getChildCount() == 3) { break; } } } } public static void setLabelList(Context context, LinearLayout labelLayout, String tagType, List tagStyle) { labelLayout.removeAllViews(); if (tagStyle == null || tagStyle.isEmpty()) { labelLayout.addView(getGameTagView(context, "官方版", 0, tagType, null)); } else { for (int i = 0; i < tagStyle.size() - 1; i++) { View view; if (i == tagStyle.size() - 1) { view = getGameTagView(context, tagStyle.get(i).getName(), 0, tagType, tagStyle.size() > i ? tagStyle.get(i) : null); } else { view = getGameTagView(context, tagStyle.get(i).getName(), DisplayUtils.dip2px(context, 8), tagType, tagStyle.size() > i ? tagStyle.get(i) : null); } if (view != null) { labelLayout.addView(view); } if (labelLayout.getChildCount() == 3) { break; } } } } // 新的游戏标签样式 version>=4.0.0 private static TextView getNewGameTagView(Context context, TagStyleEntity tagEntity, int rightMargin) { // 参数不全,用旧样式实现 if (TextUtils.isEmpty(tagEntity.getBackground())) { return getGameTagView(context, tagEntity.getName(), rightMargin, "type", tagEntity); } LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lparams.rightMargin = rightMargin; TextView tag = new TextView(context); tag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10); tag.setSingleLine(true); tag.setText(tagEntity.getName()); tag.setLayoutParams(lparams); tag.setPadding( DisplayUtils.dip2px(context, 4), 0, DisplayUtils.dip2px(context, 4), DisplayUtils.dip2px(context, 1)); tag.setTextColor(Color.parseColor("#" + tagEntity.getColor())); tag.setBackground(DrawableView.getServerDrawable(Color.parseColor("#" + tagEntity.getBackground()))); return tag; } private static TextView getGameTagView(Context context, String tagStr, int rightMargin, String tagType, TagStyleEntity tagEntity) { LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); lparams.rightMargin = rightMargin; TextView tag = new TextView(context); tag.setTextSize(TypedValue.COMPLEX_UNIT_SP, 9); tag.setSingleLine(true); tag.setText(tagStr); if ("官方版".equals(tagStr) || "已关注".equals(tagStr)) { tag.setBackgroundResource(R.drawable.border_green_bg); tag.setTextColor(ContextCompat.getColor(context, R.color.tag_green)); } else { String colorStr; if (!TextUtils.isEmpty(tagType) && "type".equals(tagType) && tagEntity != null) { // 游戏标签 colorStr = "#" + tagEntity.getColor(); GradientDrawable gradientDrawable = new GradientDrawable(); if ("border".equals(tagEntity.getStyle())) { gradientDrawable.setColor(Color.TRANSPARENT); gradientDrawable.setStroke(DisplayUtils.dip2px(context, 1f), Color.parseColor(colorStr)); tag.setTextColor(Color.parseColor(colorStr)); } else { gradientDrawable.setColor(Color.parseColor(colorStr)); gradientDrawable.setShape(GradientDrawable.RECTANGLE); tag.setTextColor(Color.WHITE); } gradientDrawable.setCornerRadius(DisplayUtils.dip2px(3F)); tag.setBackgroundDrawable(gradientDrawable); } else { colorStr = TagUtils.getInstance(context).getColor(tagStr); if (colorStr == null) { return null; } int color = Color.parseColor(colorStr); GradientDrawable gradientDrawable = new GradientDrawable(); gradientDrawable.setColor(Color.TRANSPARENT); gradientDrawable.setStroke(DisplayUtils.dip2px(context, 1f), color); gradientDrawable.setCornerRadius(DisplayUtils.dip2px(3F)); tag.setBackgroundDrawable(gradientDrawable); tag.setTextColor(color); } } tag.setLayoutParams(lparams); tag.setPadding(DisplayUtils.dip2px(context, 4), 0, DisplayUtils.dip2px(context, 4), DisplayUtils.dip2px(context, 1)); return tag; } public static String getGameTestDate(long testTime) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA); format.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); String testDate; try { long today = format.parse(format.format(new Date())).getTime(); long day = Long.parseLong(testTime + "000"); SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm", Locale.CHINA); String time = timeFormat.format(day); if (day >= today && day < today + 86400 * 1000) { testDate = "今天 " + time; } else if (day >= today + 86400 * 1000 && day < today + 86400 * 1000 * 2) { testDate = "明天 " + time; } else if (day >= today + 86400 * 1000 * 2 && day < today + 86400 * 1000 * 3) { testDate = "后天 " + time; } else if (day >= today - 86400 * 1000 && day < today) { testDate = "昨天 " + time; } else { testDate = new SimpleDateFormat("MM-dd HH:mm", Locale.CHINA).format(day); } return testDate; } catch (ParseException e) { e.printStackTrace(); long day = Long.parseLong(testTime + "000"); Calendar calendar = Calendar.getInstance(TimeZone .getTimeZone("Asia/Shanghai")); calendar.setTimeInMillis(day); int hour = calendar.get(Calendar.HOUR_OF_DAY); format = new SimpleDateFormat("MM-dd", Locale.CHINA); testDate = format.format(day) + " " + hour + "点"; return testDate; } } }