0

如何找到屏幕的坐标?我知道例如电话会说 960 x 540 的分辨率,但在模拟器中,如果我按照该分辨率绘制形状,则某些边缘不会被填充。有没有解决的办法?

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

如何在画布中使用路径功能。例如画一个三角形?我已将我的尝试包含在代码中,但它没有显示三角形。

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

}
4

1 回答 1

0

对于您可以轻松调用的尺寸canvas.getWidth()canvas.getHeight()并按百分比计算您的所有值。永远不要假设一个常数!

您需要使用两个单独的 Paint 来使用两种不同的颜色。请记住,每幅画都是需要初始化的对象。

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

ps.:给背景上色更容易调用canvas.drawColor(int colorVal);

于 2013-02-19T13:28:31.427 回答