Jak przekonwertować bitmapę na Drawable w Androidzie?

Jak przekonwertować obraz bitmapowy na Drawable ?

Dzięki, Farha

Author: Farha Ansari, 2010-03-10

8 answers

Brzmi jakbyś chciał użyć BitmapDrawable

Z dokumentacji:

A Drawable który otacza bitmapę i może być wyłożone kafelkami, rozciągnięte lub wyrównane. Ty można utworzyć BitmapDrawable z ścieżka do pliku, strumień wejściowy, przez Inflacja XML, lub z Bitmap obiekt.

 245
Author: Graeme Duncan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-18 12:19:16

Try this to converts a Bitmap type image to Drawable

Drawable d = new BitmapDrawable(getResources(), bitmap);
 770
Author: Manoj,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-15 13:41:19

Po zaobserwowaniu dużej liczby problemów z nieprawidłowym skalowaniem bitmap po przekonwertowaniu do BitmapDrawable, ogólnym sposobem konwersji powinien być:

Drawable d = new BitmapDrawable(getResources(), bitmap);

Bez Resources reference, bitmap może nie renderować poprawnie, nawet jeśli jest poprawnie skalowany. Istnieje tu wiele pytań, które można by rozwiązać po prostu za pomocą tej metody, a nie prostego wywołania tylko z argumentem bitmap.

 136
Author: Zulaxia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-15 13:42:10

Oficjalna Bitmapdrawable Dokumentacja

Oto przykład jak przekonwertować bitmapę na drawable

Bitmap bitmap;  
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
 32
Author: Cristiana Chavez,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-19 08:41:57

Jeśli masz obraz bitmapowy i chcesz go użyć w drawable, jak

Bitmap contact_pic;    //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic); 
 20
Author: Pir Fahim Shah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-19 08:41:31

I used with context

//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
 20
Author: Samuel Ivan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-13 00:06:25

Po prostu zrób to:

private void setImg(ImageView mImageView, Bitmap bitmap) {

    Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
    mImageView.setDrawable(mDrawable);
}
 10
Author: Joolah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-23 16:55:43

Oto kolejny:

Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
 0
Author: SolidSnake,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-09 07:09:36