You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android Activity自定义颜色及图片取色颜色范围设置咨询

嘿,我来帮你搞定这个问题!针对你遇到的自定义颜色常量、扩展颜色以及颜色范围分类的需求,我给你整理了一套清晰的解决方案:

解决方案:自定义颜色体系与颜色范围分类

一、替代Color.XXX:自己写颜色常量类

Android的Color类是final的,没法直接扩展它的静态常量,不过我们可以自己封装一个工具类来存放自定义颜色,用法和Color.RED完全一样:

public class CustomColors {
    // 自定义基础颜色(直接用16进制色值)
    public static final int PINK = 0xFFE91E63; // 对应#E91E63
    public static final int LIGHT_BLUE = 0xFF87CEFA; // 对应#87CEFA
    public static final int SOFT_YELLOW = 0xFFFFFACD; // 对应#FFFFACD
    
    // 兼容低版本的颜色获取方法(如果用xml定义颜色的话)
    public static int getColor(Context context, int resId) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return context.getColor(resId);
        } else {
            return context.getResources().getColor(resId);
        }
    }
}

之后在代码里就能直接写CustomColors.PINK来使用自定义颜色了。

二、更规范的方式:在colors.xml中定义颜色

推荐把颜色统一放在res/values/colors.xml里管理,方便后续修改和复用:

<resources>
    <!-- 自定义颜色 -->
    <color name="pink">#E91E63</color>
    <color name="light_blue">#87CEFA</color>
    <!-- 颜色范围的基准值 -->
    <color name="red_range_min">#FF0000</color>
    <color name="red_range_max">#FF6347</color>
</resources>

在Activity中调用时:

// API 23及以上直接用
int pinkColor = getColor(R.color.pink);
// 兼容低版本用ContextCompat
int lightBlueColor = ContextCompat.getColor(this, R.color.light_blue);

三、实现颜色范围分类(比如把#FF0000~#FF6347归为红色)

要判断一个颜色是否属于某个区间,核心是解析颜色的RGB分量,然后判断每个分量是否在目标范围内。这里给你写个通用的工具类:

public class ColorRangeClassifier {
    // 判断颜色是否属于自定义红色范围
    public static boolean isRedCategory(int color) {
        // 解析当前颜色的RGB值
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        
        // 对应#FF0000到#FF6347的RGB区间:R接近255,G在0~99,B在0~71
        return r >= 250 
                && g >= 0 && g <= 99 
                && b >= 0 && b <= 71;
    }

    // 通用版:传入最小/最大颜色值,判断目标颜色是否在区间内
    public static boolean isInRange(int targetColor, int minColor, int maxColor) {
        int tR = Color.red(targetColor);
        int tG = Color.green(targetColor);
        int tB = Color.blue(targetColor);
        
        int minR = Color.red(minColor);
        int minG = Color.green(minColor);
        int minB = Color.blue(minColor);
        
        int maxR = Color.red(maxColor);
        int maxG = Color.green(maxColor);
        int maxB = Color.blue(maxColor);
        
        // 处理min和max顺序颠倒的情况
        return tR >= Math.min(minR, maxR) && tR <= Math.max(minR, maxR)
                && tG >= Math.min(minG, maxG) && tG <= Math.max(minG, maxG)
                && tB >= Math.min(minB, maxB) && tB <= Math.max(minB, maxB);
    }
}

使用示例:

// 假设用户点击得到的颜色是clickedColor
int redMin = getColor(R.color.red_range_min);
int redMax = getColor(R.color.red_range_max);

if (ColorRangeClassifier.isInRange(clickedColor, redMin, redMax)) {
    resultTextView.setText("红色");
} else if (clickedColor == CustomColors.PINK) {
    resultTextView.setText("粉色");
}

四、Activity中使用的完整流程

  1. 添加颜色:要么在colors.xml里定义,要么在CustomColors类里写静态常量;
  2. 获取点击颜色:通过ImageView的Bitmap调用getPixel(x,y)获取点击点的颜色值;
  3. 分类判断:用上面的ColorRangeClassifier方法判断颜色所属类别;
  4. 显示结果:把分类结果设置到TextView中。

给你个简单的Activity片段参考:

public class ColorPickerActivity extends AppCompatActivity {
    private ImageView colorImageView;
    private TextView resultText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_picker);
        
        colorImageView = findViewById(R.id.color_image);
        resultText = findViewById(R.id.result_text);
        
        // 监听点击事件获取颜色
        colorImageView.setOnClickListener(v -> {
            MotionEvent event = (MotionEvent) v.getTag();
            Bitmap bitmap = ((BitmapDrawable) colorImageView.getDrawable()).getBitmap();
            int clickedColor = bitmap.getPixel((int) event.getX(), (int) event.getY());
            
            // 判断颜色类别
            String colorName;
            if (ColorRangeClassifier.isRedCategory(clickedColor)) {
                colorName = "红色";
            } else if (clickedColor == CustomColors.LIGHT_BLUE) {
                colorName = "浅蓝色";
            } else if (Color.red(clickedColor) == 0 && Color.green(clickedColor) == 0 && Color.blue(clickedColor) == 0) {
                colorName = "黑色";
            } else {
                colorName = "未知颜色";
            }
            
            resultText.setText("点击的颜色:" + colorName);
        });
        
        // 记录触摸坐标
        colorImageView.setOnTouchListener((v, event) -> {
            v.setTag(event);
            return false;
        });
    }
}

内容的提问来源于stack exchange,提问作者楊政傑

火山引擎 最新活动