Question

I'm trying to program a speedometer to fill it later with GPS data or something. I have an ImageView which is showing the needle (480x480) as source and with the speedometer as backgroundimage (480x480). Now I try to rotate the needle with the following code: (one unit [km/h] is an angle of 2.25°)

float angle = (float) (deltaspeed * 2.25);
matrix.postRotate(angle);
Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(),matrix, true);
imgNeedle.setImageBitmap(bMapRotate);

That works fine for the moment. But while the needle turns, the size of the layout compromises. I guess it is because the diagonal of the picture is bigger than the height, and while turning the matrix, the layout stretches to its full height (which is the diagonal then). So my speedometer is always changing. Any ideas what I could do?

Speed-o-meter

Was it helpful?

Solution

okay now i've built my own view for this problem. When I override the "onDraw()"-method, it is possible to draw into that View without stretching the view in other dimensions.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    HEIGHT = this.getHeight();
    WIDTH = this.getWidth();
    matrix.setRotate(currAngle, WIDTH/2, HEIGHT/2);
    canvas.drawBitmap(needleBitMap, matrix, null);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top