我正在尝试创建一个背景为半圆的TextView。我使用ShapeDrawable创建一个椭圆。我试图通过使用ScaleDrawable将椭圆的大小垂直尺寸加倍并剪切它来创建半圆。但是,ScaleDrawable无效。为什么不?在视图背景中绘制半圆的最佳方法是什么?
RES /布局/ 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 /抽拉/ 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 /抽拉/ 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);
}
//...
答案 0 :(得分:27)
要剪切椭圆形状,只需将其嵌入ClipDrawable中,如下所示:
<强> RES /抽拉/ semicircle.xml 强>
clipOrientation
ClipDdrawable
通用目的是创建自定义进度条。它剪辑其内容的一部分,并在其“级别”属性增加[0;范围]时逐步显示它。 10000](0 =隐藏,10000 =完全显示)。
gravity
是剪辑进度的方向。 ClipDrawable
是剪辑进度开始边缘/侧面。要获得半圈,请将此//...
findViewById(R.id.my_view).getBackground().setLevel(5000)
//...
设置为您的观看背景,并以编程方式调整其“级别”:
ToolStripControlHost
适用于所有Android版本(“在API级别1中添加”)并且不需要自定义视图。
- )
答案 1 :(得分:23)
您可以实现自己的Drawable。但这不能从XML中膨胀。您需要使用View.setBackgroundDrawable();
从代码中设置drawable请参阅我的示例实现,以使用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);
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);
}
@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 0;
}
}
答案 2 :(得分: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);
}
}
答案 3 :(得分:1)
相反,你可以使用图像设置为背景......
1&gt;使用绘画设计任何图像并将其保存为任何支持的格式,例如.jpg或.png,即这将是您想要的半圆形图像。
2&gt;将图像保存在res / drawable文件夹中
3&gt;使用android:background="@drawable/yourimage.jpg"
希望这会有所帮助...
答案 4 :(得分:0)
您可以使用 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"