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

标签 android android-support-library android-vectordrawable

我将 VectorDrawableCompat 设置为 View 的背景。

仅当我单击可绘制的非透明区域时,我才需要在此 View 上处理单击

对于 BitmapDrawable 来说非常简单 - 我可以只检查 getPixel(x, y),我可以使用 VectorDrawableCompat 做什么?

最佳答案

您可以尝试对访问字段使用反射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;
    }

关于android - 如何通过指定的x和y坐标获取VectorDrawableCompat的像素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37405257/

相关文章:

android - 如何使用设计支持库 23.2 实现动画矢量绘图?

android - 使用 postDelayed 在特定时间运行任务

Android,不使用支持库时,DrawerLayout 的等效类是什么?

java - 使用 Retrofit Rest Adapter 时出错 : NoClassDefFoundError: Retrofit. Restadapter$Builder

Android Gradle 构建失败 : Could not find com. google.android:support-v4:r18

android - 将支持库添加到 Android Studio 项目

android - ImageButton 上的 VectorDrawable 很难看

android - 如何从 ImageSpan 中的 xml 向量设置 Drawable

java - 整个屏幕的OnTouchListener

java - Service 和PeriodicWorkRequest() 之间的区别