[英]Draw text inside a circle
我正在開發一個Android應用程序,我想繪制一個帶有文本的圓圈。 我希望填充為白色,黑色邊框和黑色文字。 現在我有一個ShapeDrawable
:
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xFFFFFF);
然而,這會使整個圓圈變白(並且白色背景你看不到它),經過一段時間的搜索,你怎么能在形狀上添加文字,我似乎無法找到有效的答案。 我還要注意,我將根據用戶輸入添加任意數量的不同文本的圓圈。 任何幫助將非常感激!
您可以嘗試這種替代方法。
創建一個可繪制的文件oval.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
<solid android:color="#fff"/>
<stroke android:width="2px" android:color="#000"/>
</shape>
然后創建一個RelativeLayout並使用橢圓drawable設置背景
<RelativeLayout
android:id="@+id/circle"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:background="@drawable/oval" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/hello_world" />
</RelativeLayout>
結果將是這樣的:
我很肯定在這里回復,但這可能對其他人有所幫助。
ShapeDrawable colorCode = new ShapeDrawable(new OvalShape());
colorCode.getPaint().setStyle(Paint.Style.FILL); //See more paint style for border circle etc. like STROKE
colorCode.getPaint().setAntiAlias(true);
colorCode.getPaint().setColor(getResources().getColor(YOUR_COLOUR_HERE_FROM_XML));
colorCode.setIntrinsicHeight(Globals.dp2px(5, getActivity())); //converting dp to px, you can just put any integer instead of dp2px method
colorCode.setIntrinsicWidth(Globals.dp2px(5, getActivity()));
greenText.setBackgroundDrawable(colorCode);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.