0

I have a drawable. I want to rotate around its center before writing to a text on drawable

drawable= this.getResources().getDrawable(R.drawable.image);

final int IMAGE_WIDTH = drawable.getIntrinsicWidth();
final int IMAGE_HEIGHT = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(IMAGE_WIDTH, 
                                      IMAGE_HEIGHT, 
                                      Bitmap.Config.ARGB_8888);
Canvas imageCanvas = new Canvas(bitmap); 

Paint imagePaint = new Paint();
imagePaint.setTextAlign(Align.CENTER);
imagePaint.setTextSize(16f);

drawable.draw(imageCanvas);

imageCanvas.drawText("", 
                     IMAGE_WIDTH / 2, 
                     (IMAGE_HEIGHT - imagePaint.ascent()) / 2, 
                     imagePaint);                   

LayerDrawable finalImage = new LayerDrawable(
     new Drawable[]{drawable, new BitmapDrawable(bitmap)}); 
Cœur
  • 37,241
  • 25
  • 195
  • 267
comen
  • 3
  • 2

3 Answers3

0

You can use matrix to do all your rotation games and then set it to your canvas.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

Best way to do this is View Animation. http://developer.android.com/guide/topics/resources/animation-resource.html#Tween

0

I found the solution

Bitmap  bmpOriginal = BitmapFactory.decodeResource(this.getResources(), R.drawable.image);                      
                    final int IMAGE_WIDTH = drawable.getIntrinsicWidth();
                    final int IMAGE_HEIGHT = drawable.getIntrinsicHeight();                                         
                    Matrix matrix = new Matrix();
                    matrix.postRotate(rotate);
                    Bitmap resizedBitmap = Bitmap.createBitmap(bmpOriginal, 0, 0,width, height, matrix, true);
                    Canvas imageCanvas = new Canvas(resizedBitmap); 
                    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

                    Paint imagePaint = new Paint();
                    imagePaint.setTextAlign(Align.CENTER);
                    imagePaint.setTextSize(14f);

                    bmd.draw(imageCanvas);

                    imageCanvas.drawText("Text", 
                                             IMAGE_WIDTH / 2, 
                                             (IMAGE_HEIGHT - imagePaint.ascent()) / 2, 
                                             imagePaint);                   

                    LayerDrawable finalImage = new LayerDrawable(
                         new Drawable[]{bmd, new BitmapDrawable(resizedBitmap)}); 
comen
  • 3
  • 2