Selectable circular imageview like Google+

前端 未结 5 1815
北恋
北恋 2021-02-09 21:19

How can I create a selectable circular ImageView like in the current Google+ app used for the profile pictures?

This is what I refer to:

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-09 21:49

    Really simple solution, thanks to @CommonsWare for the tips.

    Size of Bitmap: imageSizePx - 3DP
    Size of 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;
    }
    

提交回复
热议问题