ImageView中带有圆angular的位图
我有一个ImageView,我想使它与rounded corners
。
我使用这个:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@null"/> <stroke android:width="1dp" android:color="#ff000000"/> <corners android:radius="62px"/> </shape>
并将此代码设置为我的imageview的背景。 它的工作原理,但是我放在ImageView
上的src图像是走出了边界,并不适应新的形状。
我该如何解决这个问题?
试试这个:
public class CustomImageView extends ImageView { public static float radius = 18.0f; public CustomImageView(Context context) { super(context); } public CustomImageView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { //float radius = 36.0f; Path clipPath = new Path(); RectF rect = new RectF(0, 0, this.getWidth(), this.getHeight()); clipPath.addRoundRect(rect, radius, radius, Path.Direction.CW); canvas.clipPath(clipPath); super.onDraw(canvas); } }
和
<your.pack.name.CustomImageView android:id="@+id/selectIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:scaleType="centerCrop" /> CustomImageView iconImage = (CustomImageView )findViewById(R.id.selectIcon); iconImage.setImageBitmap(bitmap);
要么,
ImageView iv= new CustomImageView(this); iv.setImageResource(R.drawable.pic);
做一个函数,使用canvas四舍五入你的位图。
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundPx = pixels; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
更多信息:> 这里
奇怪的是,这里没有人提到Android支持库v4中的RoundedBitmapDrawable 。 对我来说,这是获得无边界圆angular的最简单方法。 这是用法的例子:
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); final float roundPx = (float) bitmap.getWidth() * 0.06f; roundedBitmapDrawable.setCornerRadius(roundPx);
接受的答案使用path限制,但不支持消除锯齿。 见罗曼·盖伊在他的post上的评论。 “path裁剪不支持抗锯齿,你会看到锯齿边缘。”
http://www.curious-creature.com/2012/12/11/android-recipe-1-image-with-rounded-corners/
有一个很好的库(vinc3m1的RoundedImageView),可以在ImageView上实现圆angular,但是它只支持每个angular上的相同的半径。 所以我做了一个,你可以在每个angular落设置不同的半径。
它不依赖path剪辑,也不重绘。 它只用canvas.drawPath()
方法绘制一次。 所以我最终得到了我想要的结果。
请参阅: https : //github.com/pungrue26/SelectableRoundedImageView
如果你还需要边框,那么你可以使用一个透明的身体和从外侧白色圆形框图像。例如:
并将其与目标图像一起使用,如下所示:
<FrameLayout android:layout_width="100px" android:layout_height="100px" > <ImageView android:id="@+id/targetImage" android:layout_width="100px" android:layout_height="100px" android:src="@drawable/app_icon" android:layout_gravity="center" /> <ImageView android:id="@+id/boxImage" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="@drawable/box" />
谢谢!
如果你需要使用不同angular落半径的位图,我build议遵循下面的代码:
private static Bitmap createRoundedRectBitmap(@NonNull Bitmap bitmap, float topLeftCorner, float topRightCorner, float bottomRightCorner, float bottomLeftCorner) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = Color.WHITE; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); Path path = new Path(); float[] radii = new float[]{ topLeftCorner, bottomLeftCorner, topRightCorner, topRightCorner, bottomRightCorner, bottomRightCorner, bottomLeftCorner, bottomLeftCorner }; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); path.addRoundRect(rectF, radii, Path.Direction.CW); canvas.drawPath(path, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
在android中为imageview制作圆angular的方法不是火箭科学家! 只需使用与背景相同颜色的所需曲线的png,然后将叠加层设置为FITXY。
public class RoundedImageView extends ImageView { public RoundedImageView(Context context) { super(context); } public RoundedImageView(Context context, AttributeSet attrs) { super(context, attrs); } public RoundedImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap rounder = Bitmap.createBitmap(getWidth(),getHeight(),Bitmap.Config.ARGB_8888); Canvas canvasRound = new Canvas(rounder); Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG); xferPaint.setColor(Color.BLACK); final int rx = this.getWidth(); //our x radius final int ry = this.getHeight(); //our y radius canvasRound.drawRoundRect(new RectF(0,0,rx,ry), rx, ry, xferPaint); xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawBitmap(rounder, 0, 0, xferPaint); } }
public void drawRoundImage(boolean isEditPicEnable){ if(originalImageBitmap != null){ setBackgroundResource(R.drawable.ic_account_user_outer_circle_blue); if (isEditPicEnable) { setBackgroundResource(R.drawable.ic_account_user_outer_circle_white); Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.ic_account_white_mask); Bitmap mask1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_account_pencil_bg); originalImageBitmap = Bitmap.createScaledBitmap(originalImageBitmap, mask.getWidth(), mask.getHeight(), true); Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Bitmap.Config.ARGB_8888); Canvas mCanvas = new Canvas(result); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); mCanvas.drawBitmap(originalImageBitmap, 0, 0, null); mCanvas.drawBitmap(mask, 0, 0, paint); mCanvas.drawBitmap(mask1, 0, 0, null); Bitmap mask2 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_account_pencil); mCanvas.drawBitmap(mask2, 0, 0, null); setImageBitmap(result); setScaleType(ScaleType.FIT_XY); } else { Bitmap mask = BitmapFactory.decodeResource(getResources(),R.drawable.ic_account_white_mask); originalImageBitmap = Bitmap.createScaledBitmap(originalImageBitmap, mask.getWidth(),mask.getHeight(), true); Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(),Bitmap.Config.ARGB_8888); Canvas mCanvas = new Canvas(result); Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); mCanvas.drawBitmap(originalImageBitmap, 0, 0, null); mCanvas.drawBitmap(mask, 0, 0, paint); paint.setXfermode(null); setImageBitmap(result); setScaleType(ScaleType.FIT_XY); } }else{ setBackgroundResource(R.drawable.ic_account_user_outer_circle_blue); setImageResource(R.drawable.my_ac_default_profile_pic); } }