如何从 VectorDrawable 获取位图 [英] How to get a Bitmap from VectorDrawable

查看:38
本文介绍了如何从 VectorDrawable 获取位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在努力解决我几天前遇到的问题,但我仍然没有找到解决方案.但是,我正在一步一步地到达那里.现在我遇到了另一个障碍.

I'm still trying to solve the problem I've had since a couple of days ago and I still have not found a solution. However, I am getting there step by step. Now I have run into another roadblock.

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

I am trying to get Bitmap.getpixel(int x, int y) to return the Color of what the user has touched using OnTouchListener. The pie is a VectorDrawable resource, vectordrawable.xml I don't need to do anything with the pixel data yet, I just need to test it. So I made a TextView that will spit out the Color touched.

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 后立即遇到致命错误,并给出不幸的是,..."消息并退出.在堆栈跟踪中,我发现了这一点.

But my app encounters a fatal error as soon as I touch the ImageView, gives the "Unfortunately,..." message and quits. In the stack trace, I found this.

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 行就是这个,

and line 38 is this one,

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

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

I kind of followed this. What am I doing wrong? Is it because it's a VectorDrawable. What can I do to get the Color? You can see that I have also tried BitmapFactory to cast the Drawable. Could it also be a problem with the API level of VectorDrawable since it was added like API 21?

推荐答案

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

First of all, you cannot cast VectorDrawable to BitmapDrawable. They don't have a parent-child relationship. They both are direct subclasses of Drawable class.

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

Now, to get a bitmap from drawable, you will need to create a Bitmap from the drawable metadata.

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

Probably something like this in a separate method,

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;
}

我希望这会有所帮助.

这篇关于如何从 VectorDrawable 获取位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆