Java 安卓:我可以在我的图像上有圆形边框吗
我成功地将图像裁剪成圆形。但现在我想以编程方式在其周围添加圆形红色边框。 我尝试了很多东西来使用我的代码,但它不起作用。 下面是进行裁剪的类的代码Java 安卓:我可以在我的图像上有圆形边框吗,java,android,image,border,crop,Java,Android,Image,Border,Crop,我成功地将图像裁剪成圆形。但现在我想以编程方式在其周围添加圆形红色边框。 我尝试了很多东西来使用我的代码,但它不起作用。 下面是进行裁剪的类的代码 enter code here import android.R.color; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import a
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;
}
}
请尝试下面的代码以获取图像周围的圆角边框,此函数将向imageview显示圆角图像和边框
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;
}
是的,我试过这个密码。这个问题是我不能改变输出图像的大小,主要的是我仍然没有得到图像周围的边界