android-将带有圆角的图像设置为ImageView
作者:互联网
我有一个带有联系人图像和姓名或电话号码的ListView.现在,我想在此联系人图像的角上画上圆角.
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
Log.d("main", "pos:" + "" + position);
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.Name = (TextView) row.findViewById(R.id.name);
holder.Number = (TextView) row.findViewById(R.id.number);
holder.img=(ImageView) row.findViewById(R.id.image);
Typeface face=Typeface.createFromAsset(context.getAssets(),"helve.ttf");
holder.Name.setTypeface(face);
//holder.Number.setTypeface(face);
//bit = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
//
//holder.img.setImageBitmap(getRoundedCornerBitmap(bit, 40));
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.Name.setText(user.getName());
holder.img.setImageBitmap(user.getbi());
//holder.img.setImageBitmap(roundCornerImage(BitmapFactory.decodeResource(user.getbi(), R.drawable.ic_launcher),60));
holder.Number.setText(user.getNumber());
// Give Different Back Ground To List View---------------------------------------------
if ((position % 2) == 0) {
row.setBackgroundResource(R.drawable.list_dark);
} else {
row.setBackgroundResource(R.drawable.list_light);
}
Log.d("main", "pos:" + "" + position);
return row;
}
static class UserHolder {
TextView Name,Number;
ImageView img;
}
如何在此图像上添加圆角?我有点困惑,我必须做什么样的改变.
解决方法:
嗨,请使用此自定义imageview
public class RoundedImageView extends ImageView {
public RoundedImageView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public RoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@SuppressLint("Instantiatable")
public RoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
int w = getWidth(), h = getHeight();
Bitmap roundBitmap = getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius)
sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
else
sbmp = bmp;
Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xffa19774;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f,
sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
标签:android-bitmap,android,performance,android-imageview 来源: https://codeday.me/bug/20191009/1882846.html