Gop*_*opi 23
试试这个...
public static Bitmap getCircularBitmapWithWhiteBorder(Bitmap bitmap,
int borderWidth) {
if (bitmap == null || bitmap.isRecycled()) {
return null;
}
final int width = bitmap.getWidth() + borderWidth;
final int height = bitmap.getHeight() + borderWidth;
Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas canvas = new Canvas(canvasBitmap);
float radius = width > height ? ((float) height) / 2f : ((float) width) / 2f;
canvas.drawCircle(width / 2, height / 2, radius, paint);
paint.setShader(null);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
paint.setStrokeWidth(borderWidth);
canvas.drawCircle(width / 2, height / 2, radius - borderWidth / 2, paint);
return canvasBitmap;
}
Run Code Online (Sandbox Code Playgroud)
首先用您的代码获取Circulat图片。然后应用此xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<gradient android:startColor="#333440" android:endColor="#333440"
android:angle="270"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
然后添加一个相对布局并向其中添加一个imageview,将其排列在相对布局的中心,并将该圆形设置为Imageview的背景,然后将圆形imageview放置在先前添加的imageview上方,并将其也居中放置,通过更改圆形imageview页边距,您将获得所需的边框效果。希望能帮到你..
首先将以下行添加到build.gradle文件
implementation 'de.hdodenhof:circleimageview:2.2.0'
Run Code Online (Sandbox Code Playgroud)
然后将以下XML代码添加到xml文件:
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"/>
Run Code Online (Sandbox Code Playgroud)
不,您不必使用两个图像视图,一个用于图像,另一个用于白色边框。您可以创建一个新的 XML 文件,如下所示
边框.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<stroke android:width="5dp" android:color="#000000" />
<padding android:left="5dp" android:top="5dp" android:right="5dp"
android:bottom="5dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后将其设置为您的图像视图。就像将下面的行添加到您的图像视图中一样。
android:background="@drawable/border"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31952 次 |
| 最近记录: |