[英]Shapes in Android Studio?
您可以将以下课程用于绘制半圈
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);
}
}
输出量
有两种选择;
1.通过代码
您需要定义自己的drawable(不是View,因为View对于该任务很繁琐,为了获得最佳性能,请定义Drawable的继承者),然后在onDraw方法中绘制图形。 例如:
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.通过XML
当支持库23.2发布时,所有开发人员都可以通过api级别> 9的所有应用程序获得矢量可绘制对象。 因此,您可以下一步。
将路径添加到xml中的组标记。 您将拥有类似的东西:
<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>
就这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.