简体   繁体   English

Android Studio中的形状?

[英]Shapes in Android Studio?

i want to draw a circle, semicircle and triangle shape in android studio. 我想在Android Studio中绘制圆形,半圆形和三角形。 Is there any simple way to draw shapes in android studio or should i use images for each shape? 有没有简单的方法可以在android studio中绘制形状,还是应该为每个形状使用图像?

i tried this as drawable source 我尝试将此作为可绘制的源

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

but its not good enough to create exact outer shape of circle and other shapes. 但它不足以创建精确的圆形和其他形状的外部形状。 Is there any other way to do it? 还有其他方法吗?

半圆 三角形 圈

you can use following class for Draw Half Circle 您可以将以下课程用于绘制半圈

public class MyView extends View {


    public MyView(Context context) {

        super(context);

        // TODO Auto-generated constructor stub

    }


    @Override

    protected void onDraw(Canvas canvas) {

        // TODO Auto-generated method stub

        super.onDraw(canvas);

        float width = (float) getWidth();

        float height = (float) getHeight();

        float radius;


        if (width > height) {

            radius = height / 4;

        } else {

            radius = width / 4;

        }


        Path path = new Path();

        path.addCircle(width / 2,

                height / 2, radius,

                Path.Direction.CW);


        Paint paint = new Paint();

        paint.setColor(Color.BLACK);

        paint.setStrokeWidth(5);


        paint.setStyle(Paint.Style.FILL);

        float center_x, center_y;
        final RectF oval = new RectF();


        paint.setStyle(Paint.Style.STROKE);
        center_x = width / 2;

        center_y = height / 2;

        oval.set(center_x - radius,

                center_y - radius,

                center_x + radius,

                center_y + radius);

        canvas.drawArc(oval, 90, 180, false, paint);


    }

}

Output 输出量

在此处输入图片说明

There are two options; 有两种选择;

1.Through code 1.通过代码

You need to define your own drawable (not View because View is to heavy for this task, for best performance define inheritor of Drawable) and then draw your figures in method onDraw. 您需要定义自己的drawable(不是View,因为View对于该任务很繁琐,为了获得最佳性能,请定义Drawable的继承者),然后在onDraw方法中绘制图形。 For example: 例如:

public class MyDrawable extends Drawable {
    @Override
    public void onDraw(Canvas canvas) {
        //call method from canvas to draw your figures or whatever, 
        //provide them by your custom paint (but please don't create them here)
    }
}

2. Through XML 2.通过XML

When support library 23.2 came out, All developers received a vector drawables available through all applications with api level >9. 当支持库23.2发布时,所有开发人员都可以通过api级别> 9的所有应用程序获得矢量可绘制对象。 So you can do next. 因此,您可以下一步。

  1. Connect support vector drawables ( here description how you can do it ) 连接支持向量可绘制对象( 在此描述如何实现
  2. Create each figure in separate path tag ( here description how you can do it ) 在单独的路径标签中创建每个图形( 在此描述如何操作
  3. Add pathes to group tag in your xml. 将路径添加到xml中的组标记。 You will have something like that: 您将拥有类似的东西:

      <vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="64dp" android:width="64dp" android:viewportHeight="600" android:viewportWidth="600" > <group> <path android:pathData="some path data" /> <path android:pathData="some path data" /> <path android:pathData="some path data" /> </group> </vector> 

And that's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM