android – 像Google一样可选的圆形图像

如何在用于个人资料照片的当前Google应用程序中创建可选择的圆形 ImageView?

这就是我所说的:

取消选择上面的图像,然后选择下面的图像.

我尝试复制配置文件图片1到1.

到目前为止我的工作:

loadedImage是显示的位图

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.因此,这意味着背景应该与图像重叠.哪个不起作用.

解决方法

非常简单的解决方案,感谢@CommonsWare的提示.

位图大小:imageSizePx – 3DP
ImageView的大小:imageSizePx

mImageView.setBackground(createStateListDrawable(imageSizePx));
mImageView.setimageBitmap(loadedImage);

private StateListDrawable createStateListDrawable(int size) {
    StateListDrawable stateListDrawable = new StateListDrawable();

    ovalShape ovalShape = new ovalShape();
    ovalShape.resize(size,size);
    ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
    shapeDrawable.getPaint().setColor(getResources().getColor(R.color.somecolor));

    stateListDrawable.addState(new int[]{android.R.attr.state_pressed},shapeDrawable);
    stateListDrawable.addState(new int[]{android.R.attr.state_focused},shapeDrawable);
    stateListDrawable.addState(new int[]{},null);

    return stateListDrawable;
}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...