【发布时间】:2013-06-04 16:16:04
【问题描述】:
如何创建一个可选择的圆形ImageView,就像当前用于个人资料图片的 Google+ 应用程序一样?
这就是我所指的:
上图未选中,下图已选中。
我尝试一一复制个人资料图片。
我目前的工作:
loadedImage 是显示的Bitmap
mImageView.setBackground(createStateListDrawable());
mImageView.setImageBitmap(createRoundImage(loadedImage));
使用的方法:
private Bitmap createRoundImage(Bitmap loadedImage) {
Bitmap circleBitmap = Bitmap.createBitmap(loadedImage.getWidth(), loadedImage.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(loadedImage, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(loadedImage.getWidth() / 2, loadedImage.getHeight() / 2, loadedImage.getWidth() / 2, paint);
return circleBitmap;
}
private StateListDrawable createStateListDrawable() {
StateListDrawable stateListDrawable = new StateListDrawable();
OvalShape ovalShape = new OvalShape();
ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
stateListDrawable.addState(new int[] { android.R.attr.state_pressed }, shapeDrawable);
stateListDrawable.addState(StateSet.WILD_CARD, shapeDrawable);
return stateListDrawable;
}
ImageView 的大小为imageSizePx,图像的大小为imageSizePx - 3。所以,这意味着背景应该与图像重叠。哪个不行。
【问题讨论】:
-
有你所指的截图吗?
-
也许这会有所帮助。 stackoverflow.com/questions/13108803/…
-
@CommonsWare 等一下。
-
@CommonsWare 更新了我的问题。
标签: android graphics imageview