为图像添加边框,例如安卓中的谷歌照片扫描应用程序



是否可以在不向用户显示图像的情况下为图像添加边框。 喜欢谷歌照片扫描应用程序。在这里,带有照片扫描徽标的图像中添加了一个边框

你可以使用这个库

implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'

在 XML 文件中

<com.github.siyamed.shapeimageview.BubbleImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/neo"
app:siArrowPosition="right"
app:siSquare="true"/>
  • 设计了具有图像视图的自定义布局
  • 将其膨胀为视图对象
  • 在布局中将"我的图像"设置为该图像视图
  • 将视图绘制为位图并保存

充气和绘图代码

public Bitmap drawToBitmap(Context context, final int layoutResId,
final int width, final int height, String imageDescription) {
final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bmp);
final LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(layoutResId, null);
ImageView img = layout.findViewById(R.id.imgAnalysedBitmap);
img.setImageBitmap(croppedBitmap);
layout.setDrawingCacheEnabled(true);
layout.measure(
View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY));
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight());
canvas.drawBitmap(layout.getDrawingCache(), 0, 0, new Paint());
return bmp;
}

用法:

final DisplayMetrics metrics = getResources().getDisplayMetrics();
Bitmap bmap = drawToBitmap(this, R.layout.layout_watermark, metrics.widthPixels, metrics.heightPixels, imageDescription);

最新更新