Using a rounded corners drawable

前端 未结 3 1550
遇见更好的自我
遇见更好的自我 2020-12-23 10:59

There is a nice post made by the popular Google developer Romain Guy that shows how to use a rounded corners drawable (called "StreamDrawable" in his code ) on a v

相关标签:
3条回答
  • 2020-12-23 11:13

    I had some size issues with this code, and I solved it.

    Maybe this will help you, too:

    1) in the constructor store the bitmap in a local variable (e.g. private Bitmap bmp;)

    2) override two more methods:

    @Override
        public int getIntrinsicWidth() {
        return bmp.getWidth();
    }
    
    @Override
        public int getIntrinsicHeight() {
        return bmp.getHeight();
    }
    

    Best regards, DaRolla

    0 讨论(0)
  • 2020-12-23 11:14

    I think that the solution that is presented on this website works well.

    unlike other solutions, it doesn't cause memory leaks, even though it is based on Romain Guy's solution.

    EDIT: now on the support library, you can also use RoundedBitmapDrawable (using RoundedBitmapDrawableFactory ) .

    0 讨论(0)
  • 2020-12-23 11:23

    There underlying problem is that the BitmapShader's TileMode doesn't have a scaling option. You'll note in the source that it's been set to Shader.TileMode.CLAMP, and the docs describe that as:

    replicate the edge color if the shader draws outside of its original bounds

    To work around this, there are three solutions:

    1. Constrain the size of the view in which the drawable is used to the size of the bitmap.
    2. Constrain the drawing region; for instance, change:

      int width = bounds.width() - mMargin;
      int height = bounds.height() - mMargin;
      mRect.set(mMargin, mMargin, width, height);
      

      To:

      int width = Math.min(mBitmap.getWidth(), bounds.width()) - mMargin;
      int height = Math.min(mBitmap.getHeight(), bounds.height()) - mMargin;
      mRect.set(mMargin, mMargin, width, height);
      
    3. Scale the bitmap to the size of the drawable. I've moved creating the shader into onBoundsChange() and have opted to create a new bitmap from here:

      bitmap = Bitmap.createScaledBitmap(mBitmap, width, height, true);
      mBitmapShader = new BitmapShader(bitmap,
              Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
      

      Note that this a potentially slow operation and will be running on the main thread. You might want to carefully consider how you want to implement it before you go for this last solution.

    0 讨论(0)
提交回复
热议问题