0

I have a circular image, this circular image is inside linearlayout. I want the linearlayout to be circular as well, how can i make a linearlayout circular? not take recntgaular space, but circular space?

this is vital for me, cause am developing sthg with circular image..and next to circular image sthg tied to it which is a linearlayout as well

user3278732
  • 1,694
  • 10
  • 31
  • 67
  • Post one snap with circular image – M D Mar 01 '14 at 10:11
  • Post snapshot of image – Piyush Mar 01 '14 at 10:15
  • 1
    don't believe a circular linear layout is realistically possible, but describe more of what u want to do, and a different approach (like overlapping them), might work. – NameSpace Mar 01 '14 at 10:15
  • http://s21.postimg.org/4zray7eef/Untitled.png I have the circular image, I also have the textview and the buttons below. the buttons are inside linearlayout horizontally i drew to them a rectangel...but they dont get attached to the circular screen rather some space appears to it. this happens because circular image is inside a linearlayout which takes rectangular space – user3278732 Mar 01 '14 at 10:19
  • Does the image show the DESIRED RESULT or WHAT YOU GOT? It looks nice. But nothing "circular" is needed to produce it. – Phantômaxx Mar 01 '14 at 10:26
  • i get similar result, apart from that white border not attached to the circular image. a bit of space shows up in between.. – user3278732 Mar 01 '14 at 10:28
  • @user3278732 so you want the children to be laid out in a crcle and not one by one ? is it what you want ? – pskink Mar 01 '14 at 10:36
  • Try this so answer: [link](http://stackoverflow.com/a/19393571) You don't need an circular linear layout, but an imageview with a circular border in it. – Joel Brito Mar 01 '14 at 10:59
  • As @NameSpace wrote, `View`s (and `ViewGroup`s) cannot be circular, or any other shape than rectangular. You will probably need to set this up with overlapping (possibly with some negative margins). – corsair992 Mar 01 '14 at 11:14

1 Answers1

1

You can create a circular Bitmap to use in a ImageView, using a white stroke around the image.

Then you can use a RelativeLayout, to put the image on the left and other ui elements on the right.

There is a very interesting post about this feature:

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/ It is written by Romain Guy (ex android team at Google).

You can use something like this. This example create a circular bitmap, with around a white stroke. You can change it, adding a white stroke with a black line.

public class CircleDrawable extends Drawable {

    private final BitmapShader mBitmapShader;
    private final Paint mPaint;
    private Paint mWhitePaint;
    int circleCenterX;
    int circleCenterY;
    int mRadus;
    private boolean mUseStroke = false;
    private int mStrokePadding = 0;

    public CircleDrawable(Bitmap bitmap) {

        mBitmapShader = new BitmapShader(bitmap,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(mBitmapShader);

    }

    public CircleDrawable(Bitmap bitmap, boolean mUseStroke) {
        this(bitmap);

        if (mUseStroke) {
            this.mUseStroke = true;
            mStrokePadding = 4;
            mWhitePaint = new Paint();
            mWhitePaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mWhitePaint.setStrokeWidth(0.75f);
            mWhitePaint.setColor(Color.WHITE);
        }
    }

    @Override
    protected void onBoundsChange(Rect bounds) {
        super.onBoundsChange(bounds);
        circleCenterX = bounds.width() / 2;
        circleCenterY = bounds.height() / 2;

        if (bounds.width() >= bounds.height())
            mRadus = bounds.width() / 2;
        else
            mRadus = bounds.height() / 2;
    }

    @Override
    public void draw(Canvas canvas) {
        if (mUseStroke) {
            canvas.drawCircle(circleCenterX, circleCenterY, mRadus, mWhitePaint);
        }
        canvas.drawCircle(circleCenterX, circleCenterY, mRadus - mStrokePadding, mPaint);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        mPaint.setColorFilter(cf);
    }

    public boolean ismUseStroke() {
        return mUseStroke;
    }

    public void setmUseStroke(boolean mUseStroke) {
        this.mUseStroke = mUseStroke;
    }

}

To use it:

CircleDrawable circle = new CircleDrawable(bitmap,true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
  imageView.setBackground(circle);
         else
   imageView.setBackgroundDrawable(circle);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841