2014-06-08 2 views
0

Je suis en train de créer une image jpeg en utilisant le code ci-dessous:Avoir plusieurs polices dans graphique Peinture Android

Bitmap src = BitmapFactory.decodeResource(activity.getResources(), R.drawable.background_splash); // the original file yourimage.jpg i added in resources 
      Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); 

      String yourText = "My custom Text adding to Image"; 

      Canvas cs = new Canvas(dest); 
      Paint tPaint = new Paint(); 
      tPaint.setTextSize(35); 
      tPaint.setColor(Color.BLUE); 

     tPaint.setTypeface(Typeface.createFromAsset(getAssets(), 
        PATH_FONT_PUNJABI)); 

      cs.drawBitmap(src, 0f, 0f, null); 
      float height = tPaint.measureText("yY"); 
      float width = tPaint.measureText(yourText); 
      float x_coord = (src.getWidth() - width)/2; 
      cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can 
      try { 
       dest.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/ImageAfterAddingText.jpg"))); 
       // dest is Bitmap, if you want to preview the final image, you can display it on screen also before saving 
       System.out.println("Image Created"); 
      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

Maintenant, je veux un certain texte à l'aide de la police A et certains d'utiliser la police B. Comment Je réalise ceci?

Merci,

Aman

Répondre

0

La meilleure façon est de le faire avec 2 commandes de texte de tirage séparés. Définir la première police, dessiner la chaîne 1, définir la deuxième police, dessiner la chaîne 2. Si une chaîne vient après l'autre, vous pouvez obtenir la taille de la chaîne comme dessiné avec measureText ou getTextBounds de l'objet Paint

+0

Pouvez-vous expliquer avec une aide d'exemple. –