简体   繁体   中英

Shapes in Android Studio?

i want to draw a circle, semicircle and triangle shape in android studio. Is there any simple way to draw shapes in android studio or should i use images for each shape?

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

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. 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

When support library 23.2 came out, All developers received a vector drawables available through all applications with api level >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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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