문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top