【问题标题】:How to get a Bitmap from VectorDrawable如何从 VectorDrawable 获取位图
【发布时间】:2016-04-09 07:12:40
【问题描述】:

我仍在尝试解决几天前遇到的问题,但仍未找到解决方案。但是,我正在一步一步地到达那里。现在我遇到了另一个障碍。

我试图让Bitmap.getpixel(int x, int y) 返回用户使用OnTouchListener 触摸的Color。馅饼是VectorDrawable 资源,vectordrawable.xml 我不需要对像素数据做任何事情,我只需要测试它。所以我做了一个TextView,它会吐出Color被触动。

public class MainActivity extends AppCompatActivity {
    ImageView imageView;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);
        textView = (TextView) findViewById(R.id.textView);

        imageView.setOnTouchListener(imageViewOnTouchListener);
    }

    View.OnTouchListener imageViewOnTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {

            Drawable drawable = ((ImageView)view).getDrawable();
            //Bitmap bitmap = BitmapFactory.decodeResource(imageView.getResources(),R.drawable.vectordrawable);
            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

            int x = (int)event.getX();
            int y = (int)event.getY();

            int pixel = bitmap.getPixel(x,y);

            textView.setText("touched color: " + "#" + Integer.toHexString(pixel));

            return true;
        }
    };
}

但是,当我触摸ImageView 时,我的应用程序遇到了一个致命错误,给出了“不幸的是,...”消息并退出。在堆栈跟踪中,我找到了这个。

java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
    at com.skwear.colorthesaurus.MainActivity$1.onTouch(MainActivity.java:38)

第 38 行就是这一行,

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();

我有点关注this。我究竟做错了什么?是因为它是VectorDrawable。我该怎么做才能获得Color?你可以看到我也尝试过BitmapFactory 来转换DrawableVectorDrawable 的 API 级别是否也有问题,因为它是像 API 21 一样添加的?

【问题讨论】:

    标签: android bitmap ontouchlistener getpixel android-vectordrawable


    【解决方案1】:

    首先,您不能将VectorDrawable 转换为BitmapDrawable。他们没有亲子关系。它们都是Drawable 类的直接子类。

    现在,要从 drawable 中获取位图,您需要从 drawable 元数据中创建一个Bitmap

    在单独的方法中可能是这样的,

    try {
        Bitmap bitmap;
    
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    } catch (OutOfMemoryError e) {
        // Handle the error
        return null;
    }
    

    我希望这会有所帮助。

    【讨论】:

    • 非常感谢,真的很有帮助,但是drawable 变量呢?输入了,用其他方法赋值了,没有输出,只有bitmap有输出。我认为它必须在为它分配任何方法之后应用某些 Android 方法,或者不是?
    • 什么是BITMAP_CONFIG
    • 您需要将 BITMAP_CONFIG 替换为可用的位图配置之一。例如:Bitmap.Config.RGB_565 更多详细信息请参见:stackoverflow.com/questions/45511017/…
    【解决方案2】:

    使用 AndroidX 支持库中的 Drawable.toBitmap()VectorDrawableDrawable 的孩子。

    【讨论】:

    • 如果 VectorDrawable 的纵横比与您请求的位图不同会怎样?
    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多