798

Android用のXMLでの形状の定義のドキュメントを見つけるのにいくつか問題があります。XMLファイルに単色で塗りつぶされた単純な円を定義して、レイアウトファイルに含めたいと思います。

残念ながら、android.comのドキュメントにはShapeクラスのXML属性が含まれていません。ArcShapeを使用して円を描く必要があると思いますが、円弧から円を作成するために必要なサイズ、色、または角度を設定する方法についての説明はありません。

4

18 に答える 18

1724

これは、Androidで描画可能な単純な円です。

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

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

   <size 
       android:width="120dp"
        android:height="120dp"/>
</shape>
于 2012-08-25T12:24:53.533 に答える
847

これをビューの背景として設定します

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="1dp"
        android:color="#78d9ff"/>
</shape>

ここに画像の説明を入力してください

実線で使用する場合:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid
        android:color="#48b3ff"/>
</shape>

ここに画像の説明を入力してください

ストロークでソリッド:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#199fff"/>
    <stroke
        android:width="2dp"
        android:color="#444444"/>
</shape>

ここに画像の説明を入力してください

oval図形を円として表示するには、これらの例では、この図形を背景として使用しているビューが正方形であるか、図形タグのプロパティheightwidthプロパティを同じ値に設定する必要があります。

于 2016-01-11T15:08:59.657 に答える
103

シンプルサークルのコード

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <solid android:color="#9F2200"/>
        <stroke android:width="2dp" android:color="#fff" />
        <size android:width="80dp" android:height="80dp"/>
</shape>
于 2014-11-25T10:01:17.627 に答える
51

次のようにVectorDrawableを使用できます。

<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="200dp"
    android:height="200dp"
    android:viewportHeight="64"
    android:viewportWidth="64">

    <path
        android:fillColor="#ff00ff"
        android:pathData="M22,32
        A10,10 0 1,1 42,32
        A10,10 0 1,1 22,32 Z" />
</vector>

上記のxmlは次のようにレンダリングされます:

ここに画像の説明を入力してください

于 2016-07-29T16:17:12.187 に答える
46

AndroidSDKのサンプルを見てください。ApiDemosプロジェクトにはいくつかの例があります。

/ ApiDemos / res / drawable /

  • black_box.xml
  • shape_5.xml

グラデーションで塗りつぶされた円の場合、次のようになります。

<?xml version = "1.0" encoding = "utf-8"?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android" android:shape = "oval">
    <gradient android:startColor = "#FFFF0000" android:endColor = "#80FF00FF"
            android:angle = "270" />
</ shape>

于 2011-08-11T17:32:37.013 に答える
23

このようなサークルが必要な場合

ここに画像の説明を入力してください

以下のコードを使用してみてください。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadius="0dp"
    android:shape="ring"
    android:thicknessRatio="2"
    android:useLevel="false" >
    <solid android:color="@android:color/white" />
    <stroke
        android:width="1dp"
        android:color="@android:color/darker_gray" />
</shape>
于 2018-02-06T09:36:35.400 に答える
22
<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <!-- fill color -->
    <solid android:color="@color/white" />

    <!-- radius -->
    <stroke
        android:width="1dp"
        android:color="@color/white" />

    <!-- corners -->
    <corners
        android:radius="2dp"/>
</shape>
于 2016-01-29T10:00:42.947 に答える
19

プレマテリアルの簡単なcircle_background.xmlは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/color_accent_dark" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/color_accent" />
        </shape>
    </item>
</selector>

'android:background="@drawable/circle_background"ボタンのレイアウト定義の属性で使用できます

于 2015-06-09T23:28:08.983 に答える
16

res / drawble / circle_shape.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>

        <shape android:shape="oval">
            <solid android:color="#e42828"/>
            <stroke android:color="#3b91d7" android:width="5dp"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="250dp" android:height="250dp"/>
        </shape>
    </item>
</selector>

ここに画像の説明を入力してください

于 2019-07-06T00:36:52.750 に答える
15

AndroidXMLドローアブルファイルの円の形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@android:color/white" />
    <stroke
        android:width="1.5dp"
        android:color="@android:color/holo_red_light" />
    <size
        android:width="120dp"
        android:height="120dp" />
</shape>

スクリーンショット

ここに画像の説明を入力してください

于 2020-06-05T19:35:04.883 に答える
9
<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <stroke
        android:width="10dp"
        android:color="@color/white"/>

    <gradient
        android:startColor="@color/red"
        android:centerColor="@color/red"
        android:endColor="@color/red"
        android:angle="270"/>

    <size 
        android:width="250dp" 
        android:height="250dp"/>
</shape>
于 2017-04-10T12:05:56.400 に答える
6

以下のコードをダッシュ​​で試してください。

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
    android:width="@dimen/_60sdp"
    android:height="@dimen/_60sdp" />

<solid android:color="@color/black" />

<stroke
    android:width="@dimen/_1sdp"
    android:color="@color/white"
    android:dashWidth="@dimen/_1sdp"
    android:dashGap="@dimen/_1sdp" />

ダッシュなしでコードを試す

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<size
    android:width="@dimen/_60sdp"
    android:height="@dimen/_60sdp" />

<solid android:color="@color/black" />

<stroke
    android:width="@dimen/_1sdp"
    android:color="@color/white"
     />

ダッシュなしの出力

出力

于 2021-04-28T08:03:20.340 に答える
5

あなたはこれを試すことができます-

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadiusRatio="700"
    android:thickness="100dp"
    android:useLevel="false">

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

</shape>

また、を調整することで円の半径を調整できますandroid:thickness

ここに画像の説明を入力してください

于 2018-06-07T08:39:26.753 に答える
5

これを使ってみることができます

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape
        android:innerRadius="0dp"
        android:shape="ring"
        android:thicknessRatio="2"
        android:useLevel="false" >
        <solid android:color="@color/button_blue_two" />
    </shape>
</item>

これをテキストビューに使用している場合は、幅と高さのアスペクト比を気にする必要はありません。

于 2019-11-13T12:21:31.543 に答える
4

なんらかの理由でConstraintLayout内に円を描くことができず、上記の回答を使用できませんでした。

完璧に機能したのは、「Alt+7」を押すと出力されるテキストを含む単純なTextViewです。

 <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#0075bc"
            android:textSize="40dp"
            android:text="•"></TextView>
于 2018-05-01T13:52:40.113 に答える
2

カスタムドローアブルを作成して、円の色と半径を動的に変更できます

import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class CircleDrawable extends Drawable {

    private Paint circlePaint;
    private int fillColor;
    private int strokeColor;
    private float radius;

    public CircleDrawable(int fillColor, int strokeColor, float radius) {
        this.fillColor = fillColor;
        this.strokeColor = strokeColor;
        this.radius = radius;
        circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        int x=getBounds().centerX();
        int y=getBounds().centerY();
        //draw fill color circle
        circlePaint.setStyle(Paint.Style.FILL);
        circlePaint.setColor(fillColor);
        canvas.drawCircle(x,y,radius,circlePaint);
        // draw stroke circle
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setColor(strokeColor);
        circlePaint.setStrokeWidth(5);
        canvas.drawCircle(x,y,radius,circlePaint);
    }

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

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
         circlePaint.setColorFilter(colorFilter);
    }

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

UIからこれを設定して、円の形を取得します

imageView.setImageDrawable(new CircleDrawable(Color.RED,Color.YELLOW,100));

出力は次のようになります

ここに画像の説明を入力してください

于 2021-03-10T06:29:51.827 に答える
0
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="oval">
            <solid android:color="@color/text_color_green"/>
            <!-- Set the same value for both width and height to get a circular shape -->
            <size android:width="250dp" android:height="250dp"/>
        </shape>
    </item>
</selector>
于 2018-10-10T11:36:39.600 に答える
-22

使用するだけ

ShapeDrawable circle = new ShapeDrawable( new  OvalShape() );
于 2010-07-06T10:05:43.360 に答える