import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.widget.ImageView; /** * Created by lenovo on 2017/10/25. */ public class CircleImageView extends android.support.v7.widget.AppCompatImageView { public CircleImageView(Context context) { super(context); } public CircleImageView(Context context, AttributeSet attrs) { super(context, attrs); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } private int bWidth = 10;// 边框宽度 private int bColor = Color.WHITE;// 边框颜色 /** * 设置圆形图片的边框颜色 * @param color */ public void setBorderColor(int color) { bColor = color; } /** * 设置圆形图片的边框宽度 * @param */ public void setBorderWidth(int width) { bWidth = width; } /** * 设置圆形图片的边框宽度和颜色 * @param */ public void setBorderWidth(int width, int color) { bWidth = width; bColor = color; } @Override public void setImageBitmap(Bitmap bm) { int d = Math.min(bm.getWidth(), bm.getHeight());// 或得圆直径 Bitmap dest = Bitmap.createBitmap(d, d, bm.getConfig());// 创建一个副本 // 画边框 Canvas c = new Canvas(dest); Paint paint = new Paint(); paint.setColor(bColor); // 边框颜色 paint.setAntiAlias(true);// 设置抗锯齿 c.drawCircle(d / 2, d / 2, d / 2, paint); // 画圆 Path path = new Path(); path.addCircle(d / 2, d / 2, d / 2 - bWidth, Path.Direction.CW); c.clipPath(path); // 裁剪区域 Matrix matrix = new Matrix();// 不缩放 c.drawBitmap(bm, matrix, paint);// 把图画上去 super.setImageBitmap(dest);} }
public class MainActivity extends Activity { private CircleImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (CircleImageView) findViewById(R.id.iv); iv.setBorderWidth(15, Color.BLUE);// 设置边框 ImageLoader.getInstance().displayImage(bigBean.getData().get(position).getImg(),iv, MyApplication.getOptions()); } }
自定义控件图片圆形和边框
最新推荐文章于 2023-07-20 15:29:46 发布