Issue
I am using Android Studio 2.1.2 for creating android applications. In my application, I need to create a circle filled with red color in android view. I tried it by using a canvas like,
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x = getWidth();
int y = getHeight();
int radius = x / 2;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(2);
canvas.drawPaint(paint);
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x / 2, y / 2, radius, paint);
}
And in onCreate()
, I have added,
setContentView(new SampleView(this));
Where SampleView
is a class which contains onDraw()
. Is there any alternative way to do the same thing, without using the canvas
?
Solution
you can create a shape xml and assign it to linear layout
Some thing like this
<LinearLayout
android:background="@drawable/circle_border"
android:layout_width="100dp"
android:layout_height="100dp">
</LinearLayout>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="@color/red" />
</shape>
</item>
</selector>
this will draw a circle
Hope this helps
Answered By - Tushar Saha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.