0

enter image description here

I have a textview.i need to set background of the textview like above image using drawable by considering the height and width of textview .how can i create a background like above by drawing on canvas.and also i need to change the color of inner circle dynamically.

My code so far

 public class BackGround extends Drawable {
        private Paint mPaint;
        private Paint mPaint1;


        @Override
        public void draw(Canvas canvas) {
            // Set the correct values in the Paint
            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
            mPaint.setAntiAlias(true);


            mPaint1 = new Paint();
            mPaint1.setColor(Color.WHITE);
            mPaint1.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint1.setFlags(Paint.ANTI_ALIAS_FLAG);
            mPaint1.setAntiAlias(true);

            canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() / 2, canvas.getWidth() - (canvas.getWidth() / 4), mPaint);
            canvas.drawCircle(canvas.getWidth() / 2, canvas.getHeight() - (canvas.getHeight() / 6), 5, mPaint1);
        }

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

        @Override
        public void setAlpha(int arg0) {
        }

        @Override
        public void setColorFilter(ColorFilter arg0) {
        }
    }
3

2 Answers 2

1

EDIT @2

with xml do this way

<item>
    <shape android:shape="oval" >
        <solid android:color="#000000" />

        <size
            android:height="15dp"
            android:width="15dp" />

        <padding
            android:bottom="5dp"
            android:left="20dp"
            android:right="20dp"
            android:top="40dp" />
    </shape>
</item>

<item>
    <shape android:shape="oval" >


        <stroke
            android:width="5dp"
            android:color="@android:color/transparent" />

        <solid android:color="#f00" />

        <size
            android:height="10dp"
            android:width="10dp" />
    </shape>
</item>

0

EDIT @1

try this way to draw circle inside circle in canvas

public class MainActivity extends Activity {
Draw draw;
LinearLayout linearLayout;

public void onCreate(Bundle s) {
    super.onCreate(s);
    setContentView(R.layout.test1);
    linearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    draw = new Draw(this);
    linearLayout.addView(draw);
}

public class Draw extends View {
    Paint paint = new Paint();

    Draw(Context context) {
        super(context);
    }

    public void onDraw(Canvas canvas) {
        paint.setColor(Color.BLUE);
        canvas.drawCircle(120, 120, 40, paint);

        paint.setColor(Color.RED);
        canvas.drawCircle(120, 140, 10, paint);
    }
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.