简体   繁体   English

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

[英]Some questions on using the canvas in Java to draw shapes and paths for Android?

How do I find the coordinates of the screen? 如何找到屏幕的坐标? I know eg a phone would have say a 960 x 540 resolution, but in the emulators some of the edges are not filled if I draw a shape to that resolution. 我知道例如一部电话的分辨率为960 x 540,但如果在模拟器中将形状绘制为该形状,则某些边缘将不被填充。 Is there a way around this? 有没有解决的办法?

For the colour of the rectangle, it is seen there are two rectangles, and two of them have the same colour despite giving two separate colours for drawPaint. 对于矩形的颜色,可以看到有两个矩形,尽管为drawPaint提供了两种不同的颜色,但其中两个具有相同的颜色。 Just setting a new variable eg drawPaint2 returns errors. 仅设置一个新变量,例如drawPaint2会返回错误。 How to change the colour of both? 如何改变两者的颜色?

How to use the path function in the canvas. 如何在画布中使用路径功能。 Eg to draw a triangle? 例如画一个三角形? I have included my attempt in the code but it doesn't display a triangle. 我已将尝试包括在代码中,但未显示三角形。

    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;
    }

}

for the size you can easily call canvas.getWidth() and canvas.getHeight() and do all your values a percentage of those. 对于大小,您可以轻松地调用canvas.getWidth()canvas.getHeight()并将所有值canvas.getHeight()百分比。 Do never assume a constant! 永远不要假设常量!

You need to use two separate Paints to use two different colors. 您需要使用两种单独的“涂料”才能使用两种不同的颜色。 And remember that each paint is an object that needs initialisation. 请记住,每种绘画都是需要初始化的对象。

// 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); 

and then to make a triangle: 然后制作一个三角形:

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

ps.: do color the background it's easier to call canvas.drawColor(int colorVal); ps .:确实为背景着色,所以更容易调用canvas.drawColor(int colorVal);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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