19

我正在尝试创建一个背景为半圆的 TextView。我使用 ShapeDrawable 创建了一个椭圆。我尝试通过使用 ScaleDrawable 将椭圆的垂直大小加倍并剪辑它来创建一个半圆。但是,ScaleDrawable 没有效果。为什么不?在视图背景中绘制半圆的最佳方法是什么?

资源/布局/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);
    }
//...
4

5 回答 5

29

要剪辑椭圆形,只需将其嵌入到 ClipDrawable 中,如下所示:

res/drawable/semicircle.xml

<?xml version="1.0" encoding="utf-8"?>
<clip
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="vertical"
    android:gravity="bottom">
    <shape android:shape="oval">
        <solid android:color="#444"/>
    </shape>
</clip>

ClipDdrawable共同目的是创建自定义进度条。它会剪切一部分内容,并在其“级别”属性在 [0; 10000](0=隐藏,10000=完全显示)。

  • clipOrientation是剪辑进度的方向。
  • gravity是剪辑进度开始边/边。

要获得半圆,请将其设置ClipDrawable为您的视图背景,并以编程方式调整其“级别”:

//...
findViewById(R.id.my_view).getBackground().setLevel(5000)
//...

适用于所有 Android 版本(“在 API 级别 1 中添加”)并且不需要自定义视图。

;-)

于 2015-10-25T20:54:50.130 回答
23

您可以实现自己的 Drawable。但这不能从 XML 夸大。您需要使用 View.setBackgroundDrawable() 从代码设置可绘制对象;

请参阅我的示例实现以使用 drawable 绘制半圆。

public class SemiCircleDrawable extends Drawable {

    private Paint paint;
    private RectF rectF;
    private int color;
    private Direction angle;

    public enum Direction
    {
        LEFT,
        RIGHT,
        TOP,
        BOTTOM
    }

    public SemiCircleDrawable() {
        this(Color.BLUE, Direction.LEFT);
    }

    public SemiCircleDrawable(int color, Direction angle) {
        this.color = color;
        this.angle = angle;

        paint = new Paint();
        paint.setColor(color);
        paint.setStyle(Style.FILL);
        paint.setAntiAlias(true);

        rectF = new RectF();
    }

    public int getColor() {
        return color;
    }

    /**
     * A 32bit color not a color resources.
     * @param color
     */
    public void setColor(int color) {
        this.color = color;
        paint.setColor(color);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.save();

        Rect bounds = getBounds();

        if(angle == Direction.LEFT || angle == Direction.RIGHT)
        {
            canvas.scale(2, 1);
            if(angle == Direction.RIGHT)
            {
                canvas.translate(-(bounds.right / 2), 0);
            }
        }
        else
        {
            canvas.scale(1, 2);
            if(angle == Direction.BOTTOM)
            {
                canvas.translate(0, -(bounds.bottom / 2));
            }
        }


        rectF.set(bounds);

        if(angle == Direction.LEFT)
            canvas.drawArc(rectF, 90, 180, true, paint);
        else if(angle == Direction.TOP)
            canvas.drawArc(rectF, -180, 180, true, paint);
        else if(angle == Direction.RIGHT)
            canvas.drawArc(rectF, 270, 180, true, paint);
        else if(angle == Direction.BOTTOM)
            canvas.drawArc(rectF, 0, 180, true, paint);

        canvas.restore()
    }

    @Override
    public void setAlpha(int alpha) {
        // Has no effect
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        // Has no effect
    }

    @Override
    public int getOpacity() {
        // Not Implemented
        return PixelFormat.UNKNOWN;
    }

}
于 2013-04-12T05:18:06.280 回答
2

我做了这门课,希望对你有帮助,所有变量都是西班牙语,但很简单,

构造函数 SemiCirculo 将半圆的 rgb 和分辨率(半圆的三角形数量)用作参数

CalcularPuntosPorcentaje 方法使用圆心、起始角度、角度宽度和无线电作为参数。

ImprimeCirculo 方法使用画布作为参数,它用于绘制半圆,一旦它已经用前面提到的方法计算了半圆的点。

CalcularPuntosPorcentaje 方法与 CalcularPuntosPorcentaje 方法类似,但它使用从 0 到 100 的 % 的起始角和宽度角参数

最后,SetOffset 和 SetColor 用于更改半圆的角度和颜色的默认起始位置参考

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;


public class SemiCirculo {

    private Path circulo;
    private Paint color;
    private float px, py, radio, anguloI, anchoa,offset;
    private int r, g, b;
    private int resolucion;
    private float puntox[],puntoy[];


    public SemiCirculo(int r1, int g1, int b1, int resolucion1) {

        this.offset = 0;
        this.color = new Paint();
        this.r = r1;
        this.g = g1;
        this.b = b1;
        this.color.setColor(Color.rgb(r, g, b));
        color.setAntiAlias(true);
        circulo = new Path();
        this.resolucion = resolucion1;
        this.puntox = new float[this.resolucion];
        this.puntoy = new float[this.resolucion];
        this.anguloI = 0;
        this.anchoa = 1;

    }

    public void SetOffset(float off) {
        this.offset = off;
    }

    public void SetColor(int r1,int g1, int b1){        
        this.r=r1;
        this.g=g1;
        this.b=b1;
        this.color.setColor(Color.rgb(r, g, b));
    }

    public void CalcularPuntosPorcentaje(float px1, float py1,
            float porcentaje, float radio1) {
        this.anguloI = 0 + this.offset;
        this.px = px1;
        this.py = py1;
        this.radio = radio1;
        this.anguloI = 0;
        this.anchoa = porcentaje / 100 * 360;

        this.CalcularPuntos(px, py, anguloI, anchoa, radio);
    }

    public void CalcularPuntos(float px1, float py1, float anguloI1,
            float anchoangulo, float radio1) {
        this.anguloI = anguloI1 + this.offset;

        this.anchoa = anchoangulo;
        this.px = px1;
        this.py = py1;
        this.radio = radio1 ;

        float angulo = 360 - this.anguloI - this.anchoa;

        for (int i = 0; i < resolucion; i++) {
            this.puntox[i] = this.px - (float) Math.sin(Math.toRadians(angulo))
                    * this.radio;
            this.puntoy[i] = this.py - (float) Math.cos(Math.toRadians(angulo))
                    * this.radio;
            angulo = (360 - this.anguloI - this.anchoa)
                    + ((this.anchoa / (float) (this.resolucion)) * (i + 2));
        }

        this.circulo.reset();

        this.circulo.moveTo(this.px, this.py);
        for (int i = 0; i < resolucion; i++) {
            this.circulo.lineTo(this.puntox[i], this.puntoy[i]);
        }

    }

    public void ImprimeCirculo(Canvas canvas) {

        canvas.drawPath(this.circulo, this.color);

    }

}
于 2014-02-22T19:45:33.280 回答
1

相反,您可以使用图像设置为背景...

1>使用绘画设计任何图像并将其保存为任何支持的格式,例如 .jpg 或 .png 即这将是您想要的半圆形图像。

2>将图像保存在res/drawable文件夹中

3>使用设置你的文本视图背景到该图像android:background="@drawable/yourimage.jpg"

希望这可以帮助...

于 2013-04-12T04:14:36.403 回答
0

您可以使用矢量可绘制。我在 Affinity 设计中创建了它。

在此处输入图像描述 在此处输入图像描述

放到drawable文件夹里ic_cemicircle.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="48dp"
    android:height="48dp"
    android:viewportWidth="48"
    android:viewportHeight="48">

    <path
        android:fillColor="#FF000000"
        android:pathData="M-0.0005,0.0004C13.2462,0.0004 23.9991,10.7533 23.9991,24C23.9991,37.2467 13.2462,47.9996 -0.0005,47.9996L-0.0005,0.0004Z" />

</vector>

并且还在布局 xml 中添加下一个属性

android:adjustViewBounds="true"
android:scaleType="fitXY"
于 2021-05-25T00:49:24.217 回答