如何通过指定的x和y坐标获取VectorDrawableCompat的像素颜色

时间:2016-05-24 05:42:47

标签: android android-support-library android-vectordrawable

我将VectorDrawableCompat设置为我视图的背景。

只有点击了非透明的drawable区域,我才需要在此视图上处理点击

对于BitmapDrawable非常简单 - 我可以检查一下getPixel(x,y),我可以用VectorDrawableCompat做些什么呢?

1 个答案:

答案 0 :(得分:2)

您可以尝试对访问字段VectorDrawableCompat.mVectorState使用反射,并从属性mCachedBitmap获取缓存位图

类似的东西:

     public static int getColorAt(VectorDrawableCompat drawable, float x, float y) {
        try {
            final Field field = drawable.getClass().getDeclaredField("mVectorState");
            field.setAccessible(true);
            final Object state = field.get(drawable);
            final Field bitmapField = state.getClass().getDeclaredField("mCachedBitmap");
            bitmapField.setAccessible(true);
            final Bitmap bitmap = (Bitmap) bitmapField.get(state);
            return bitmap.getPixel((int) x, (int) y);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return -1;
    }