【问题标题】:diamond shape in android studioandroid studio中的菱形
【发布时间】:2018-07-16 21:51:14
【问题描述】:

我在 Android Studio 上使用路径创建菱形时遇到问题。看起来我有一半多一点的钻石,但我不知道我做错了什么以及为什么其余部分没有打印出来。我一直在尝试更改我的代码几个小时,但没有任何效果。谁能指出我做错了什么?到目前为止,这是我的代码:

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.shapes.Shape;

public class Diamond extends Shape {
private int strokeWidth;
private final int fillColor;
private int strokeColor;
private Path path;
private Paint strokePaint;
private Paint fillPaint;

public Diamond(int strokeWidth, int fillColor, int strokeColor) {
    this.strokeWidth = strokeWidth;
    this.fillColor = fillColor;
    this.strokeColor = strokeColor;

    this.strokePaint = new Paint();
    this.strokePaint.setStyle(Paint.Style.STROKE);
    this.strokePaint.setStrokeWidth(strokeWidth);

    this.fillPaint = new Paint();
    this.fillPaint.setStyle(Paint.Style.FILL);
    this.fillPaint.setColor(fillColor);
}
@Override
public void draw(Canvas canvas, Paint paint) {
    canvas.drawPath(path, fillPaint);
    canvas.drawPath(path, strokePaint);

}
@Override
protected void onResize(float width, float height) {
    super.onResize(width, height);
    path = new Path();
    path.moveTo(width/2, 0);
    path.lineTo(width, height);
    path.lineTo(width/2, height*4);
    path.lineTo(0, height);
    path.close();



}

}

【问题讨论】:

    标签: android shapes


    【解决方案1】:

    我认为您应该只需要更改path.lineTo(width/2, height*4); 以将高度乘以 2,就像在path.lineTo(width/2, height*2); 中一样,使用 4 会使下半部分比上半部分倾斜更长。还有一个关于this page 绘制菱形的示例,您可以修改它以使用全宽绘制菱形,例如:

    public void drawRhombus(Canvas canvas, Paint paint, int x, int y, int width) {
        int halfWidth = width / 2;
    
        Path path = new Path();
        path.moveTo(x, y + width); // Top
        path.lineTo(x - halfWidth, y); // Left
        path.lineTo(x, y - width); // Bottom
        path.lineTo(x + halfWidth, y); // Right
        path.lineTo(x, y + width); // Back to Top
        path.close();
    
        canvas.drawPath(path, paint);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-19
      • 1970-01-01
      • 2023-03-22
      • 2011-02-06
      • 1970-01-01
      • 1970-01-01
      • 2020-12-08
      相关资源
      最近更新 更多