【问题标题】:Draw a semicircle in the background of a View在 View 的背景中画一个半圆
【发布时间】:2013-04-12 03:51:32
【问题描述】:

我正在尝试创建一个背景为半圆形的 TextView。我使用 ShapeDrawable 创建了一个椭圆。我试图通过使用 ScaleDrawable 将椭圆的垂直大小加倍并剪裁它来创建一个半圆。但是,ScaleDrawable 没有效果。为什么不?在 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);
    }
//...

【问题讨论】:

标签: android


【解决方案1】:

要裁剪椭圆形,只需将其嵌入到 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 中添加”)并且不需要自定义视图。

;-)

【讨论】:

  • 剪辑周围也可以有边框吗?
  • 可以在子形状中添加描边:(在椭圆形 标签内)但这可能不是你想要的,因为这个边框也会被剪掉。
  • 谢谢约翰。因此,不可能以剪辑形状与父级接壤。
【解决方案2】:

您可以实现自己的 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;
    }

}

【讨论】:

【解决方案3】:

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

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

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

ImprimeCirculo方法以canvas为参数,用之前提到的方法计算完半圆的点后,用来绘制半圆。

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);

    }

}

【讨论】:

  • 方法名称在 Java 中不应大写。它是西班牙语的事实也使这里的人们难以理解。
【解决方案4】:

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

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

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

3>使用 android:background="@drawable/yourimage.jpg" 将您的 textview 背景设置为该图像

希望这会有所帮助...

【讨论】:

    【解决方案5】:

    您可以使用 Vector Drawable。我在 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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多