无法概括速度表的代码

时间:2014-02-24 05:27:11

标签: android canvas ondraw

以下代码仅适用于设备宽度范围宽度&gt; 400到宽度<550但不是像Tab这样的其他设备(宽度> 550)。我如何概括

speedo_meter = getImage(R.drawable.newmagnetometer_final_480);
center_wheel = getImage(R.drawable.center_wheel480);
needle = getImage(R.drawable.arrow480);
canvas.drawBitmap(speedo_meter, (canvas.getWidth() / 2)- speedo_meter.getWidth() / 2,      (canvas.getHeight() / 2)- speedo_meter.getHeight() / 2, null);


// Main Meter Needle

matrix_needle = new Matrix();
 matrix_needle.setTranslate((canvas.getWidth() / 2)- needle.getWidth() ,(canvas.getHeight() / 2)- needle.getHeight());
matrix_needle.postRotate(angle_of_deviation,canvas.getWidth() / 2, 2 * needle.getHeight() - 10);
canvas.drawBitmap(needle, matrix_needle, paint_needle);

// Main Meter Wheel
canvas.drawBitmap(center_wheel, (canvas.getWidth() / 2)- center_wheel.getWidth() / 2,       (canvas.getHeight() / 2)- center_wheel.getHeight() / 2, null);

在Tab上捕获的上传图像 I want create code for device independent I create code but it work on particular height and width   devices but not on all like Tab

1 个答案:

答案 0 :(得分:1)

您需要更改代码,如

protected void onDraw(Canvas canvas) {


    canvas.drawBitmap(speedo_meter, (canvas.getWidth() / 2)- speedo_meter.getWidth() / 2,
             (canvas.getHeight() / 3)- speedo_meter.getHeight() / 2, null);


        // Main Meter Needle
        matrix_needle = new Matrix();
        matrix_needle.setTranslate((canvas.getWidth() / 2)- needle.getWidth() / 2,
                (canvas.getHeight() / 3)- needle.getHeight());


        matrix_needle.postRotate(110,
                canvas.getWidth() / 2, (canvas.getHeight() /(float)2.9));

        canvas.drawBitmap(needle, matrix_needle, paint_needle);

}

只需使用相应的设备宽度和高度更改图像。