英文:
Adding border to image like google photo scan app in android
问题
在安卓中是否可能在不向用户显示图像的情况下为图像添加边框,类似于Google Photo Scan应用中的做法... 这里添加了带有PhotoScan徽标的图像边框。
英文:
Is it possible to add border to a image without showing the image to the user in android.
Like google photo scan app..Here a border is added to image with photoscan logo
答案1
得分: 1
你可以使用这个库:
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"/>
英文:
You can use this this library
implementation 'com.github.siyamed:android-shape-imageview:0.9.+@aar'
in Xml File
<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"/>
答案2
得分: 0
- 设计了一个自定义布局,其中包含一个图片视图。
 - 将其实例化为一个视图对象。
 - 在布局中将我的图片设置到该图片视图中。
 - 将视图绘制到位图并保存。
 
用于实例化和绘制的代码
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);
英文:
- Designed a custom layout with a image view
 - Inflated it to a view object
 - Set the my image to that image view in layout
 - Drawn the view to a bitmap and saved it
 
Code for inflating and drawing
    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;
}
Usage:
final DisplayMetrics metrics = getResources().getDisplayMetrics();
        Bitmap bmap = drawToBitmap(this, R.layout.layout_watermark, metrics.widthPixels, metrics.heightPixels, imageDescription);
专注分享java语言的经验与见解,让所有开发者获益!

评论