我创建了一个圆形ImageView
,但我需要在图像外添加一个边框。
以下是代码:
Bitmap circleBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader (bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
paint.setColor(Color.parseColor("#BAB399"));
Canvas c = new Canvas(circleBitmap);
c.drawARGB(0, 0, 0, 0);
c.drawCircle(50, 40,40, paint);
有人可以帮我在圆形图像外面创建边框吗?
答案 0 :(得分:7)
首先创建一个这样的圆形
<?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>
然后添加一个相对布局并向其添加一个imageview。将其排列到相对布局的中心。然后将此圆形设置为Imageview的背景。然后将圆形图像视图置于先前添加的imageview上方。将其也放置到center.By更改圆形图像视图边距将获得所需的边框效果。
最终代码,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView14"
android:background="@drawable/circ"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView15"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/linked"
android:layout_margin="5dp" />
</RelativeLayout>
</LinearLayout>
答案 1 :(得分:0)
嗨,大家好,我遇到了同样的麻烦,但我做了类似的事情。 我分享可能会有所帮助。
public static LayerDrawable borderImageCircleBorder(Drawable draw, Activity activity) {
int xCordinate = returnImageDrawableSize(draw);
ShapeDrawable biggerCircle = new ShapeDrawable(new OvalShape());
biggerCircle.setIntrinsicHeight(xCordinate);
biggerCircle.setIntrinsicWidth(xCordinate);
biggerCircle.setBounds(new Rect(0, 0, xCordinate, xCordinate));
biggerCircle.getPaint().setColor(Color.WHITE);
biggerCircle.setPadding(4, 4, 4, 4);
ShapeDrawable smallerCircle = new ShapeDrawable(new OvalShape());
smallerCircle.setIntrinsicHeight(xCordinate);
smallerCircle.setIntrinsicWidth(xCordinate);
smallerCircle.setBounds(new Rect(0, 0, xCordinate, xCordinate));
smallerCircle.getPaint().setColor(Color.LTGRAY);
smallerCircle.setPadding(2, 2, 2, 2);
Drawable dd = getRoundedCornerBitmap(draw, activity);
Drawable[] d = { smallerCircle, biggerCircle, dd };
LayerDrawable composite = new LayerDrawable(d);
return composite;
}
public static int returnImageDrawableSize(Drawable image) {
Bitmap original = ((BitmapDrawable) image).getBitmap();
Bitmap bitmap = original;
//Log.i("original", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
int cropSizeWrtWidth, croppWrtHeight, xCoordinate, devideBy = 2;
cropSizeWrtWidth = bitmap.getWidth() / devideBy - bitmap.getHeight() / devideBy;
croppWrtHeight = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
xCoordinate = cropSizeWrtWidth > 0 ? cropSizeWrtWidth : croppWrtHeight;
float cordinates = bitmap.getWidth() / 2;
// Find out ratio until we get a square bitmap
while (bitmap.getHeight() > bitmap.getWidth() && xCoordinate + bitmap.getWidth() > bitmap.getWidth()) {
devideBy += 1;
xCoordinate = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
if (xCoordinate + bitmap.getWidth() <= bitmap.getWidth())
break;
}
return xCoordinate;
}
public static Drawable getRoundedCornerBitmap(Drawable image, Activity activity) {
Bitmap original = ((BitmapDrawable) image).getBitmap();
;
Bitmap bitmap = original;
//Log.i("original", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
int cropSizeWrtWidth, croppWrtHeight, xCoordinate, devideBy = 2;
cropSizeWrtWidth = bitmap.getWidth() / devideBy - bitmap.getHeight() / devideBy;
croppWrtHeight = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
xCoordinate = cropSizeWrtWidth > 0 ? cropSizeWrtWidth : croppWrtHeight;
float cordinates = bitmap.getWidth() / 2;
// Find out ratio until we get a square bitmap
while (bitmap.getHeight() > bitmap.getWidth() && xCoordinate + bitmap.getWidth() > bitmap.getWidth()) {
devideBy += 1;
xCoordinate = bitmap.getHeight() / devideBy - bitmap.getWidth() / devideBy;
if (xCoordinate + bitmap.getWidth() <= bitmap.getWidth())
break;
}
if (bitmap.getWidth() >= bitmap.getHeight()) {
cordinates = bitmap.getHeight() / 2;
bitmap = Bitmap.createBitmap(bitmap, xCoordinate, 0, bitmap.getHeight(), bitmap.getHeight());
} else {
bitmap = Bitmap.createBitmap(bitmap, xCoordinate, 0, bitmap.getWidth(), bitmap.getWidth());
}
//Log.i("bitmap after crop", "ht" + bitmap.getHeight() + ",wt=" + bitmap.getWidth());
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
int color = 0xff424242;
Paint paint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);
Canvas canvas = new Canvas(output);
canvas.drawARGB(0, 0, 0, 0);
//Log.i("coordinates", "?????????" + cordinates);
canvas.drawCircle(cordinates, cordinates, cordinates, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
canvas.drawBitmap(bitmap, rect, rect, paint);
// output =
// ShadowView.getCircleWhiteBorderBitmap(original,output,convertDpToPixel(12,activity));
Drawable d = new BitmapDrawable(activity.getResources(), output);
return d;
}
答案 2 :(得分:0)
我找到了一个完全符合你的意愿的图书馆,对我来说很好。看看这个。 https://android-arsenal.com/details/1/932