关于在Java中使用画布为Android绘制形状和路径的一些问题?

关于在Java中使用画布为Android绘制形状和路径的一些问题?,java,android,Java,Android,如何找到屏幕的坐标?我知道,例如,手机的分辨率为960 x 540,但在模拟器中,如果我按照该分辨率绘制形状,则某些边缘没有填充。有办法解决这个问题吗 对于矩形的颜色,可以看到有两个矩形,其中两个具有相同的颜色,尽管为drawPaint提供了两种不同的颜色。只需设置一个新变量,例如drawPaint2,就会返回错误。如何改变两者的颜色 如何在画布中使用path函数。例如画一个三角形?我已经在代码中包含了我的尝试,但它没有显示三角形 public class DrawView extend

如何找到屏幕的坐标?我知道,例如,手机的分辨率为960 x 540,但在模拟器中,如果我按照该分辨率绘制形状,则某些边缘没有填充。有办法解决这个问题吗

对于矩形的颜色,可以看到有两个矩形,其中两个具有相同的颜色,尽管为drawPaint提供了两种不同的颜色。只需设置一个新变量,例如drawPaint2,就会返回错误。如何改变两者的颜色

如何在画布中使用path函数。例如画一个三角形?我已经在代码中包含了我的尝试,但它没有显示三角形

    public class DrawView extends View implements OnTouchListener
{

    private Paint backgroundPaint = new Paint();
    private Paint drawPaint = new Paint();
    private Paint circlePaint = new Paint();
    private Paint textPaint = new Paint();
    private Paint path = new Paint();
    private float sx, sy;

    public DrawView(Context context)
    {
        super(context);         

        setFocusable(true);
        setFocusableInTouchMode(true);

        backgroundPaint.setColor(Color.CYAN);
        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setStyle(Style.FILL);

        drawPaint.setColor(Color.WHITE);
        drawPaint.setStyle(Style.FILL);

        circlePaint.setColor(Color.DKGRAY);
        circlePaint.setStyle(Style.FILL);

        textPaint.setColor(Color.WHITE);
        textPaint.setStyle(Style.FILL);

        drawPaint.setColor(Color.GREEN);
        drawPaint.setStyle(Style.FILL);

        circlePaint.setColor(Color.RED);
        circlePaint.setStyle(Style.FILL);

        path.setColor(android.graphics.Color.RED);
        path.setStyle(Paint.Style.FILL);            
        Path path = new Path();
        path.moveTo(1, 1);
        path.lineTo(20, 50);
        path.moveTo(20, 50);
        path.lineTo(100, 100);
        path.moveTo(100, 100);
        path.lineTo(1, 1);
        path.close();

        this.setOnTouchListener(this);
    }

    @Override
    public void onDraw(Canvas canvas)
    {

        //canvas.drawPath(path, paint); <-- error

        // to draw background
        canvas.drawRect(this.getLeft(), this.getTop(), this.getRight(), this.getBottom(), backgroundPaint);

        //to draw two rectangle with blue and green paint
        canvas.drawRect(100,100, 340,540, drawPaint); 
        canvas.drawRect(00,00, 120,80, drawPaint); 

        //draw text with paint
        canvas.drawText("Hello Dear Leader!", 110, 160, textPaint);

        //draw a circle with green paint with the touch coordinates
        canvas.drawCircle(sx-30,sy-30, 30, circlePaint);

        //draw a circle with red paint with the touch coordinates
        canvas.drawCircle(sx-80, sy-80, 10, circlePaint);

    }

    public boolean onTouch(View v, MotionEvent event)
    {   
        //update the coordinates for the OnDraw method above, with wherever we touch
        sx = event.getX();
        sy = event.getY();

        invalidate();
        return true;
    }

}
公共类DrawView扩展视图实现OnTouchListener
{
私人油漆背景油漆=新油漆();
私人油漆drawPaint=新油漆();
private Paint circlePaint=新油漆();
私有绘画文本绘画=新绘画();
私有绘制路径=新绘制();
私人浮动sx,sy;
公共绘图视图(上下文)
{
超级(上下文);
设置聚焦(真);
setFocusableInTouchMode(真);
背景漆。setColor(颜色。青色);
backgroundPaint.setAntiAlias(真);
背景漆.设置样式(样式.填充);
drawPaint.setColor(颜色:白色);
drawPaint.setStyle(Style.FILL);
circlePaint.setColor(Color.DKGRAY);
循环绘制设置样式(样式填充);
textPaint.setColor(Color.WHITE);
textPaint.setStyle(Style.FILL);
drawPaint.setColor(颜色:绿色);
drawPaint.setStyle(Style.FILL);
circlePaint.setColor(颜色.红色);
循环绘制设置样式(样式填充);
setColor(android.graphics.Color.RED);
path.setStyle(Paint.Style.FILL);
路径路径=新路径();
路径。移动到(1,1);
线路图(20,50);
路径移动到(20,50);
线路图(100100);
路径移动到(100100);
线路图(1,1);
path.close();
this.setOnTouchListener(this);
}
@凌驾
公共空白onDraw(画布)
{

//canvas.drawPath(path,paint);的大小,您可以轻松调用
canvas.getWidth()
canvas.getHeight()
,并按其中的百分比执行所有值。千万不要假设一个常量

你需要使用两种不同的颜料来使用两种不同的颜色。记住,每种颜料都是一个需要初始化的对象

// objects
private Paint drawPaint_WH = new Paint();
private Paint drawPaint_GR = new Paint();

// during construction
drawPaint_WH.setColor(Color.WHITE);
drawPaint_WH.setStyle(Style.FILL);

drawPaint_GR.setColor(Color. GREEN);
drawPaint_GR.setStyle(Style.FILL);

// and then during draw()
canvas.drawRect(100,100, 340,540, drawPaint_WH); 
canvas.drawRect(0,0, 120,80, drawPaint_GR); 
然后做一个三角形:

// object
private Path trianglePath;

// during construction
trianglePath = new Path();
trianglePath.moveTo(10, 10); // starting point
trianglePath.lineTo(10, 50); // 1st vertix
trianglePath.lineTo(50, 10); // 2nd vertix
trianglePath.lineTo(10, 10); // 3rd vertix and close

// then during draw()
canvas.drawPath(trianglePath, drawPaint_GR) // or whatever paint you want

注:是否对背景进行着色?调用canvas.drawColor(int colorVal)更容易;

。您所说的“错误”是什么意思?很抱歉重复这个问题,但如何使用getWidth()、getHeight来缩放形状?是否有任何方法可以使用getLeft()、getRight()、getTop()、getBottom()相反?宽度=左-右;高度=上-下;如何缩放?只是一些基本的数学。如果我想让三角形(例如)从屏幕的50%开始
路径。移动到(getWidth()/2,getHeight/2);
那么三角形的边将是屏幕高度的20%
int side=getHeight()/5、 
并使用它绘制路径.lineTo(getWidth()/2,(getHeight()/2)+侧边);