How to convert the LayerDrawable to Drawable in Android?

后端 未结 1 482
暖寄归人
暖寄归人 2021-01-06 01:09

In my app i use the LayerDrawable to display overlayer image.and i do this is successfully use of layerDrawable.

after i get layerDrawable set as a

相关标签:
1条回答
  • 2021-01-06 01:40

    Just a test, I haven't tried this

    public Drawable geSingleDrawable(LayerDrawable layerDrawable){
    
              int resourceBitmapHeight = 136, resourceBitmapWidth = 153;
    
              float widthInInches = 0.9f;
    
              int widthInPixels = (int)(widthInInches * getResources().getDisplayMetrics().densityDpi);
              int heightInPixels = (int)(widthInPixels * resourceBitmapHeight / resourceBitmapWidth);
    
              int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;
    
              layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);     
    
              Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);
    
              Canvas canvas = new Canvas(bitmap);
              layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
              layerDrawable.draw(canvas);
    
              BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap);
              bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
    
              return bitmapDrawable;
    }
    
    0 讨论(0)
提交回复
热议问题