画一个半圆的视图背景 [英] Draw a semicircle in the background of a View

查看:162
本文介绍了画一个半圆的视图背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个TextView其背景是半圈。我创建使用ShapeDrawable一个椭圆形。我试图用ScaleDrawable翻一番椭圆形的大小垂直尺寸,并把它夹创建一个半圆。然而,ScaleDrawable不起作用。为什么不呢?什么是画一个半圆的视图背景的最好方法是什么?

RES /布局/ activity_main.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    >
    <的TextView
        机器人:ID =@ + ID / main_view
        机器人:背景=@可绘制/半圆
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentRight =真
        机器人:重力=center_horizo​​ntal
    />
    < / RelativeLayout的>
 

RES /绘制/ semicircle.xml

 < XML版本=1.0编码=UTF-8&GT?;
<规模以上的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:可绘制=@可绘制/圈
    机器人:scaleGravity =顶| clip_vertical
    机器人:scaleHeight =200%
    机器人:scaleWidth =100%>
< /规模与GT;
 

RES /绘制/ circle.xml

 < XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:形状=椭圆形
    [固体
        机器人:颜色=#444/>
< /形状>
 

的src /.../ MainActivity.java

  // ...
公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main_activity);
        findViewById(R.id.main_view).getBackground()执行setLevel(10000)。
    }
// ...
 

解决方案

您可以实现你自己的绘制对象。但是,这不能从XML的膨胀。您需要设置从code。使用View.setBackgroundDrawable()的绘制;

请参阅我的样例实现使用绘制画一个半圆。

 公共类SemiCircleDrawable扩展绘制对象{

    民营涂料粉刷;
    私人RectF rectF;
    私人诠释色彩;
    私营方向角;

    公共枚举方向
    {
        剩下,
        对,
        最佳,
        底部
    }

    公共SemiCircleDrawable(){
        这个(Color.BLUE,Direction.LEFT);
    }

    公共SemiCircleDrawable(INT颜色,方向角){
        this.color =颜色;
        this.angle =角;
        油漆=新的油漆();
        paint.setColor(颜色);
        paint.setStyle(Style.FILL);
        rectF =新RectF();
    }

    公众诠释的getColor(){
        返回的颜色;
    }

    / **
     * 32位色彩并不出彩的资源。
     * @参数颜色
     * /
    公共无效setColor(INT颜色){
        this.color =颜色;
        paint.setColor(颜色);
    }

    @覆盖
    公共无效画(油画画布){
        canvas.save();

        矩形边界的getBounds =();

        如果(角== Direction.LEFT ||角度== Direction.RIGHT)
        {
            canvas.scale(2,1);
            如果(角== Direction.RIGHT)
            {
                canvas.translate( - (bounds.right / 2),0);
            }
        }
        其他
        {
            canvas.scale(1,2);
            如果(角== Direction.BOTTOM)
            {
                canvas.translate(0, - (bounds.bottom / 2));
            }
        }


        rectF.set(边界);

        如果(角== Direction.LEFT)
            canvas.drawArc(rectF,90,180,真实,油漆);
        否则,如果(角== Direction.TOP)
            canvas.drawArc(rectF,-180,180,真实,油漆);
        否则,如果(角== Direction.RIGHT)
            canvas.drawArc(rectF,270,180,真实,油漆);
        否则,如果(角== Direction.BOTTOM)
            canvas.drawArc(rectF,0,180,真实,油漆);
    }

    @覆盖
    公共无效setAlpha(INT阿尔法){
        //没有任何影响
    }

    @覆盖
    公共无效setColorFilter(ColorFilter CF){
        //没有任何影响
    }

    @覆盖
    公众诠释getOpacity(){
        // 未实现
        返回0;
    }

}
 

I am trying to create a TextView whose background is a half circle. I create a oval using a ShapeDrawable. I tried to create a semicircle by using ScaleDrawable to double the size vertical size of the oval and clip it. However, the ScaleDrawable has no effect. Why not? What is the best way to draw a semicircle in the background of a 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);
    }
//...

解决方案

You can implement you own Drawable. But that cannot be inflated from XML. You need to set the drawable from code using View.setBackgroundDrawable();

See my sample implementation to draw a semi circle using 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;
    }

}

这篇关于画一个半圆的视图背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆