Android:我的图像周围可以有圆形边框吗[重复]
Posted
技术标签:
【中文标题】Android:我的图像周围可以有圆形边框吗[重复]【英文标题】:Android: can i have round border around on my image [duplicate] 【发布时间】:2014-08-08 05:44:52 【问题描述】:我成功地裁剪了圆形图像。但现在我想以编程方式在它周围添加圆形红色边框。 我尝试了很多东西来使用我拥有的代码,但它不起作用。 这是进行裁剪的类的代码
enter code here
import android.R.color;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
/**
* GraphicsUtil an utility class which convert the image in circular shape
*/
public class GraphicsUtil
/*
* Draw image in circular shape Note: change the pixel size if you want
* image small or large
*/
public Bitmap getRoundedShape(Bitmap scaleBitmapImage)
// TODO Auto-generated method stub
int targetWidth = 400;
int targetHeight = 400;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth) / 2, ((float) targetHeight) / 2,
(Math.min(((float) targetWidth), ((float) targetHeight)) / 2),
Path.Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.GRAY);
// paint.setStyle(Paint.Style.STROKE);
Paint p = new Paint();
p.setColor(color.white);
p.setStyle(Paint.Style.STROKE);
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawOval(new RectF(0, 0, targetWidth, targetHeight), paint);
// paint.setColor(Color.TRANSPARENT);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()), new RectF(0, 0, targetWidth,
targetHeight), paint);
return targetBitmap;
【问题讨论】:
【参考方案1】:请尝试以下代码为图像周围的圆角边框,此函数显示圆角图像以及图像视图的边框
public Bitmap getCircleBitmap(Bitmap bitmap, int pixels)
Bitmap output = Bitmap.createBitmap(100, 100,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffffffff;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, 100, 100);
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
paint.setShadowLayer(4.0f, 0.0f, 2.0f, Color.GREEN);
canvas.drawOval(rectF, paint);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth((float) 4);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
【讨论】:
是的,我试过这段代码。问题是我无法更改输出图像大小,主要是我仍然没有得到图像周围的边框以上是关于Android:我的图像周围可以有圆形边框吗[重复]的主要内容,如果未能解决你的问题,请参考以下文章