Question

Below code work on only in device width range width > 400 to width<550 but not other devices like Tab( width> 550).How I can generalize

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

Uploaded image captured on 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

Was it helpful?

Solution

You need to change your code like

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

}

Just change Images with respective Device Width and Height.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top