[英]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.