Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a Bitmap from VectorDrawable

Pie

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.

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

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)

and line 38 is this one,

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

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?

like image 573
skwear Avatar asked Apr 09 '16 07:04

skwear


People also ask

How do I make a drawable bitmap?

This example demonstrates how do I convert Drawable to a Bitmap in Android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I make a bitmap object in Kotlin?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory.

What is a bitmap in Android?

A bitmap is simply a rectangle of pixels. Each pixel can be set to a given color but exactly what color depends on the type of the pixel. The first two parameters give the width and the height in pixels. The third parameter specifies the type of pixel you want to use.

What is a vector in XML?

A VectorDrawable is a vector graphic defined in an XML file as a set of points, lines, and curves along with its associated color information. The major advantage of using a vector drawable is image scalability.


2 Answers

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.

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

I hope this helps.

like image 160
Jigar Avatar answered Oct 23 '22 04:10

Jigar


Use Drawable.toBitmap() from AndroidX support library. VectorDrawable is child of Drawable.

like image 37
Malwinder Singh Avatar answered Oct 23 '22 06:10

Malwinder Singh