Android Development--drawable and bitmap knowledge

Source: Internet
Author: User

Get drawable from your resources

Drawable drawable = getResources().getDrawable(R.drawable.xxx);

drawable Conversion bitmapdrawble

BitmapDrawable bitmapdrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.xxx);

Drawble conversion to bitmap
    • The first way

      In fact, it is simpler to convert drawable to bitmapdrawable and then bitmapdrawable to bitmap.

      Bitmap bitmap = bitmapdrawable.getBitmap();
    • The second way

      Need to re-draw, more troublesome, recommend the use of the first

        public static Bitmap drawableToBitmap(Drawable drawable) {         // 取 drawable 的长宽         int w = drawable.getIntrinsicWidth();         int h = drawable.getIntrinsicHeight();         // 取 drawable 的颜色格式         Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888                 : Bitmap.Config.RGB_565;         // 建立对应 bitmap         Bitmap bitmap = Bitmap.createBitmap(w, h, config);         // 建立对应 bitmap 的画布         Canvas canvas = new Canvas(bitmap);         drawable.setBounds(0, 0, w, h);         // 把 drawable 内容画到画布中         drawable.draw(canvas);         return bitmap;  }   
Bitmap conversion to Drawable
    • The first way

        
    • The second way

        

      Ps:bitmapdrawable is a subclass of drawable and can be used directly in the Drawable method

Android Development--drawable and bitmap knowledge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.