用dp维度绘制

时间:2014-07-24 15:06:53

标签: android paint pixel

我有一个循环油画创建一个圆圈,所有工作但在不同的设备上我得到不同的油漆尺寸,我认为这是一个密度问题,我该如何解决问题?

这是我的代码:

for(int i=0; i<360; i = i + 12){
    x = (float) (view.getWidth()/2 + raggio_in * Math.cos(Math.toRadians(i)));
    y = (float) (view.getHeight()/2 + raggio_in * Math.sin(Math.toRadians(i)));                 
    Paint paint = new Paint();
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(getResources().getColor(R.color.tutorial_text));     
    if(i < progress){
        paint.setColor(Color.WHITE);
        }
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.rotate(i, x, y);
    canvas.drawRoundRect(new RectF((float) x-deltaX, (float) y-deltaY, (float) x+deltaX, (float) y+deltaY), raggio_elemento, raggio_elemento, paint);
    canvas.restore();
    x = (float) (view.getWidth()/2 + raggio_out * Math.cos(Math.toRadians(i)));
    y = (float) (view.getHeight()/2 + raggio_out * Math.sin(Math.toRadians(i)));
    paint = new Paint();
    paint.setColor(getResources().getColor(R.color.tutorial_text));
    if(i < progress){
        paint.setColor(Color.WHITE);
    }
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.rotate(i, x, y);
    canvas.drawRoundRect(new RectF((float) x-deltaX, (float) y-deltaY, (float) x+deltaX, (float) y+deltaY), raggio_elemento, raggio_elemento, paint);
    canvas.restore();   
}

1 个答案:

答案 0 :(得分:0)

您可以用dp这种方式计算维度:

pixels * density + 0.5f

density可以访问context.getResources().getDisplayMetrics().density

0.5f用于舍入