【问题标题】:Bitmap inside white circle白色圆圈内的位图
【发布时间】:2017-01-19 19:13:21
【问题描述】:

我正在尝试在白色圆圈内填充Bitmap,更具体地说:

我有一个Bitmap,例如:

我想要这个:

我已将背景设为灰色以便理解图像。

我使用这种方法,但它并没有达到我想要的效果..

public static Bitmap getRoundedBitmap(Bitmap bitmap)
{
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.WHITE;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    bitmap.recycle();

    return output;
}

【问题讨论】:

    标签: java android bitmap android-bitmap


    【解决方案1】:

    我猜你用来绘制椭圆的颜色的 ALPHA = 0。尝试用 Color(1.0f, 1.0f, 1.0f, 1.0f) 替换 Color.WHITE。告诉我这是否解决了你的问题,然后看看: What does PorterDuff.Mode mean in android graphics.What does it do?

    在这种情况下,对于 SRC_IN:[Sa * Da, Sc * Da](取自 android 参考,PorterDuff.Mode)

    【讨论】:

      【解决方案2】:

      这是一个使用 XML 制作的形状的示例。

      1) 右键单击​​您的可绘制资源文件夹并创建一个新的可绘制资源文件。

      2) 粘贴以下代码。这将创建一个可绘制资源,该资源的外观与您想要的背景非常相似。

      <?xml version="1.0" encoding="utf-8"?>
      <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
          <item>
              <shape android:shape="rectangle">
                  <size android:width="50dp" android:height="50dp"/>
                  <solid android:color="@android:color/darker_gray"/>
              </shape>
          </item>
          <item>
              <shape android:shape="oval">
                  <size android:width="50dp" android:height="50dp"/>
                  <solid android:color="@android:color/white"/>
              </shape>
          </item>
      </layer-list>
      

      3) 在布局中使用资源。用您的“tick”资源文件替换我的占位符 Android 复选框。

      <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@drawable/highlight_circle">
      
          <ImageView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerInParent="true"
              android:src="@android:drawable/checkbox_on_background"/>
      </RelativeLayout>
      

      【讨论】:

      • 你能说明你想要什么吗?您是否希望将位图放入圆形/灰色背景中以便在视图中使用?
      • 不,我有一个位图(第一个图像),我想将它包裹成白色圆圈(像第二个图像,但另一个位图,不需要资源文件)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 2020-02-22
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 2012-08-01
      相关资源
      最近更新 更多