Android 围绕自定义图像视图绘制边框
我想在自定义ImageView周围添加一个黑色边框。目前,我正在使用此类在ImageView中实现圆角:Android 围绕自定义图像视图绘制边框,android,android-imageview,Android,Android Imageview,我想在自定义ImageView周围添加一个黑色边框。目前,我正在使用此类在ImageView中实现圆角: public class RoundedBitmapDisplayer implements BitmapDisplayer { private Context ctx; public RoundedBitmapDisplayer(Context context) { this.ctx = context; } @Override p
public class RoundedBitmapDisplayer implements BitmapDisplayer {
private Context ctx;
public RoundedBitmapDisplayer(Context context) {
this.ctx = context;
}
@Override
public Bitmap display(Bitmap bitmap, ImageView imageView) {
Bitmap roundBitmap;
try {
roundBitmap = getRoundedCornerBitmap(ctx, bitmap, 10, bitmap.getWidth(), bitmap.getHeight(),
false, false, true, true);
} catch (OutOfMemoryError e) {
Log.e(ImageLoader.TAG, "Can't create bitmap with rounded corners. Not enough memory.", e);
roundBitmap = bitmap;
}
imageView.setImageBitmap(roundBitmap);
return roundBitmap;
}
/*
private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
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(0xFFFFFFFF);
canvas.drawRoundRect(rectF, roundPixels, roundPixels, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}*/
public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels , int w , int h , boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR ) {
Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final float densityMultiplier = context.getResources().getDisplayMetrics().density;
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
final RectF rectF = new RectF(rect);
//make sure that our rounded corner is scaled appropriately
final float roundPx = pixels*densityMultiplier;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
//draw rectangles over the corners we want to be square
if (squareTL ){
canvas.drawRect(0, 0, w/2, h/2, paint);
}
if (squareTR ){
canvas.drawRect(w/2, 0, w, h/2, paint);
}
if (squareBL ){
canvas.drawRect(0, h/2, w/2, h, paint);
}
if (squareBR ){
canvas.drawRect(w/2, h/2, w, h, paint);
}
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(input, 0,0, paint);
return output;
}
}
您可以设置包含
的背景xml可绘制。您可以设置包含
的背景xml可绘制。您可以在画布中绘制线条。确保通过正确的坐标。
在onDraw()中
你可以在画布上画线。确保通过正确的坐标。 在onDraw()中
在drawable中创建image_border.xml文件
并在imageview xml条目中使用此xml
<ImageView android:id="@+id/image"
android:background="@drawable/image_border" <!--add xml like this-->
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/image" />
android:layout\u height=“包装内容”
android:layout\u width=“包装内容”
android:src=“@drawable/image”/
在drawable中创建image\u border.xml文件
并在imageview xml条目中使用此xml
<ImageView android:id="@+id/image"
android:background="@drawable/image_border" <!--add xml like this-->
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/image" />
android:layout\u height=“包装内容”
android:layout\u width=“包装内容”
android:src=“@drawable/image”/
<ImageView android:id="@+id/image"
android:background="@drawable/image_border" <!--add xml like this-->
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/image" />