How to I round the corners of ImageView for only on side fit to the parent layout in Android?
Date : March 29 2020, 07:55 AM
Hope this helps I think that you should apply a transformation when loading the images. For example if you use picasso for loading imageas you could apply this transformation: Base class: public class RoundedCornersBitmap implements Transformation {
private static final float DEFAULT_RADIUS = 5.f;
private static final int DEFAULT_BORDER_COLOR = Color.WHITE;
private static final int DEFAULT_STROKE_WIDTH = 3;
protected float mCornerRadius;
protected int mBorderColor;
protected int mStrokeWidth;
@Override
public String key() {
return "roundedCorners()";
}
public RoundedCornersBitmap() {
mCornerRadius = DEFAULT_RADIUS;
mBorderColor = DEFAULT_BORDER_COLOR;
mStrokeWidth = DEFAULT_STROKE_WIDTH;
}
public RoundedCornersBitmap(float cornderRadius, int borderColor, int strokeWidth) {
mCornerRadius = cornderRadius;
mBorderColor = borderColor;
mStrokeWidth = strokeWidth;
}
@Override
public Bitmap transform(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(mStrokeWidth, mStrokeWidth, (bitmap.getWidth() - mStrokeWidth), bitmap.getHeight()
- mStrokeWidth);
final RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
paint.setColor(mBorderColor);
paint.setStrokeWidth(3);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
//canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
/**
* @return the mCornerRadius
*/
public float getCornerRadius() {
return mCornerRadius;
}
/**
* @param mCornerRadius
* the mCornerRadius to set
*/
public void setCornerRadius(float mCornerRadius) {
this.mCornerRadius = mCornerRadius;
}
/**
* @return the mBorderColor
*/
public int getBorderColor() {
return mBorderColor;
}
/**
* @param mBorderColor
* the mBorderColor to set
*/
public void setBorderColor(int mBorderColor) {
this.mBorderColor = mBorderColor;
}
/**
* @return the mStrokeWidth
*/
public int getStrokeWidth() {
return mStrokeWidth;
}
/**
* @param mStrokeWidth
* the mStrokeWidth to set
*/
public void setStrokeWidth(int mStrokeWidth) {
this.mStrokeWidth = mStrokeWidth;
}
}
public class LeftRoundedCornersBitmap extends RoundedCornersBitmap {
public LeftRoundedCornersBitmap() {
super();
}
public LeftRoundedCornersBitmap(float cornderRadius, int borderColor, int strokeWidth) {
super(cornderRadius, borderColor, strokeWidth);
}
@Override
public Bitmap transform(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(mStrokeWidth, mStrokeWidth, (bitmap.getWidth() - mStrokeWidth), bitmap.getHeight()
- mStrokeWidth);
final RectF rectF = new RectF(rect);
final Rect topRightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2);
final Rect bottomRect = new Rect(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
paint.setColor(mBorderColor);
paint.setStrokeWidth(3);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
canvas.drawRect(topRightRect, paint);
canvas.drawRect(bottomRect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
}
compile 'com.squareup.picasso:picasso:2.5.2'
Picasso.with(context).load(imageUrl).transform(new LeftRoundedCornersBitmap()).into(youImageView);
|
Border radius 50% on parent doesn't round corners
Date : March 29 2020, 07:55 AM
Any of those help So I have found an answer to this that is quite dirty, but is working for what I need it for... The original issue seemed to only affect mobile devices, specifically Android Chrome. For some reason, the image would hide itself in the browser if the image was smaller than 60 pixels, not sure how or why this happens but my work around is below: .profile-img__wrapper {
border: 2px solid #d0d0d0;
width: 30px;
height: 30px;
display: block;
border-radius: 50%;
overflow: hidden;
-moz-box-sizing: content-box;
-webkit-box-sizing: content-box;
box-sizing: content-box;
}
.profile-img {
width: 30px;
height: 30px;
background-size: cover;
display: block;
background-position: 50% 0;
}
<span class="profile-img__wrapper">
<span class="profile-img" style="background-image: url('imageSource')"></span>
</span>
|
Imageview round corners and mask shape via xml do not work in AS 3.0
Date : March 29 2020, 07:55 AM
Hope this helps Topics checked before creating topic , Final solution via java only (original idea proposed by @pskink) ImageView img = (ImageView) findViewById(R.id.img);
Bitmap src1 = BitmapFactory.decodeResource(getResources(), R.drawable.img_port);
RoundedBitmapDrawable dr1 = RoundedBitmapDrawableFactory.create(getResources(), src1);
dr1.setCornerRadius(Math.max(src1.getWidth(), src1.getHeight()) / 10f);
img.setImageDrawable(dr1);
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="15dp" />
<solid/>
</shape>
|
Why is making two corners round slower than make all corners round?
Tag : ios , By : Julian Ivanov
Date : March 29 2020, 07:55 AM
|
android unable to round the corners of imageview?
Date : March 29 2020, 07:55 AM
|