Question

I want to make the following canvas: Draw N bitmaps at given points, but each bitmap should be rotated at a random angle.

I try something like this:

    for(int i=0;i<point_count;i++){
        star_matrix[i] = new Matrix();

        Random rand2 = new Random();
        int degree = rand2.nextInt(45);  

        star_matrix[i].postRotate(degree); 
        star_matrix[i].postTranslate(level.points[i].x - star1_size/2, level.points[i].y-star1_size/2);
    }

Then I create a bitmap

star1  = BitmapFactory.decodeResource(getResources(), R.drawable.star1_small);
star1  = Bitmap.createScaledBitmap(star1, star1_size,star1_size, true);

and draw all in cycle:

for(int i=0;i<point_count;i++){
    canvas.drawBitmap(star1, star_matrix[i], gradPaint);
}

And all the bitmaps miss their right position. All are shifted to different places.

For example, this code without matrixes draws everythong as it should be(but without rotation)

for(int i=0;i<point_count;i++){
   canvas.drawBitmap(star1, level.points[i].x - star1_size/2,  level.points[i].y-star1_size/2, pathPaint);
}

How to solve Matrix problem?

Was it helpful?

Solution

I suppose changing reordering of the following lines should help:

    star_matrix[i].postTranslate(level.points[i].x - star1_size/2, level.points[i].y-star1_size/2);
    star_matrix[i].postRotate(degree); 

Because the order of rotation and translation is important.

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