Je crée un jeu de plateau de tuiles. Je souhaite faire pivoter une pièce de mosaïque bitmap de quelques degrés prédéfinis.
Lorsque je fais pivoter mon bitmap, la taille change.
Par exemple, si je veux un morceau de carreau triangle 75x75, en rotation, je récupère un 68x68 de ce code. Comment puis-je garder les choses de la même taille pour que tout reste de la taille de la planche?Rotation bitmap et maintenir la taille
Voici ce que j'utilise pour faire tourner:
public class RotatebitmapActivity extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.t123);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 75;
int newHeight = 75;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(120);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
}
Vérifiez ma réponse [ici] [1], workd sans problème [1]: http://stackoverflow.com/questions/8712652/rotating-image-on-a-canvas-in -android/28293393 # 28293393 – MSaudi