I am trying to create a TextView whose background is a half circle. I create a oval using a ShapeDrawable. I tried to create a semicircle by using ScaleDrawable to double the size vertical size of the oval and clip it. However, the ScaleDrawable has no effect. Why not? What is the best way to draw a semicircle in the background of a View?
res/layout/activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/main_view"
android:background="@drawable/semicircle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
/>
</RelativeLayout>
res/drawable/semicircle.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/circle"
android:scaleGravity="top|clip_vertical"
android:scaleHeight="200%"
android:scaleWidth="100%" >
</scale>
res/drawable/circle.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval"
<solid
android:color="#444" />
</shape>
src/.../MainActivity.java
//...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
findViewById(R.id.main_view).getBackground().setLevel(10000);
}
//...
I made this class hope it help you, all variable are in spanish but its quite simple,
the constructor SemiCirculo use as parameters the rgb for the semicircle and the resolution (number of triangles for your semicircle)
the CalcularPuntosPorcentaje method use as parameters the center of the circle, the starting angle, the widht of the angle, and the radio.
the method ImprimeCirculo use the canvas as parameter, it is used to draw the semicircle once it has allready been calcultaed the points of the semicircle with the previus mentioned method.
the CalcularPuntosPorcentaje method is similar to CalcularPuntosPorcentaje, but insted of the starting and the widht angle parameters it use a % from 0 to 100
finaly the SetOffset and SetColor are used to change the default starting poing o reference for the angle and the color of the semicircle
instead you can use the image to set as a background...
1>design any image using paint and save it to any supported format such as .jpg or .png i.e this will be your image with semicircle which you want.
2>save the image in the res/drawable folder
3>set your textview background to that image using
android:background="@drawable/yourimage.jpg"
hope this helps...
You can implement you own Drawable. But that cannot be inflated from XML. You need to set the drawable from code using View.setBackgroundDrawable();
See my sample implementation to draw a semi circle using drawable.
To clip the oval shape, just embed it in a ClipDrawable like this:
res/drawable/semicircle.xml
ClipDdrawable
common purpose is to create custom progress bars. It clips a part of its content and progressively displays it when its "level" property increases in a range of [0; 10000] (0=hidden, 10000=fully displayed).clipOrientation
is the orientation of the clipping progress.gravity
is the clipping progress start edge/side.To get a half circle, set this
ClipDrawable
as your view background, and programmatically tweak its "level":Works on all Android versions ("Added in API level 1") and requires no custom view.
;-)