Android uses Canvas to draw text on pictures

  • 2021-08-12 03:33:32
  • OfStack

"Android" Android Font, Thickness and other attributes of Paint

In Android SDK, the Typeface class is used to define fonts, which can be set by common font type names, such as setting the default bold type:


Paint mp = new paint();
mp.setTypeface(Typeface.DEFAULT_BOLD)

Common font type names are:

* Typeface. DEFAULT//General font types

* Typeface.DEFAULT_BOLD//Boldface type

* Typeface. MONOSPACE//Constant width font type

* Typeface.SANS_SERIF//sans serif font type

* Typeface. SERIF//serif font type

In addition to font type settings, you can also set font styles for font types, such as setting bold:


Paint mp = new Paint();
Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
p.setTypeface( font );

Common font style names are:

* Typeface. BOLD//Bold

* Typeface.BOLD_ITALIC//bold italics

* Typeface. ITALIC//Italics

* Typeface. NORMAL//General

But sometimes the above settings don't work during the drawing process, so there are the following settings:


Paint mp = new Paint();
mp.setFakeBoldText(true); //true Is bold, false Is non-bold 
mp.setTextSkewX(-0.5f);  //float Type parameter, negative number indicates right slant and integer left slant 
mp.setUnderlineText(true); //true To underline, false Is not underlined 
mp.setStrikeThruText(true); //true For strikethrough, false Is a non-strikeout line 

Common methods of Paint include:


mp.setTextSize(); // Set the font size, int Type, such as 12
mp.setStrokeWidth(w); // Set the line width, float Type, such as 2.5f The default drawing text does not need to be set (the default value seems to be 0 ), but if it is set, when drawing text again, 1 Be sure to return to 0

For bold Chinese settings, it seems that only through setFakeBoldText (true) to achieve, although the effect does not look very real (font hollow effect). Actually, it is found that the final drawing effect has something to do with the mobile phone hardware, such as the previous drawing test program.

1 small application, drawing text on the picture, the following is the method of drawing text, and can realize automatic line wrap, font automatic adaptation screen size


private void drawNewBitmap(ImageView imageView, String str) {
   Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.background);
   int width = photo.getWidth();
   int hight = photo.getHeight();
   // Establish 1 Empty Bitmap
   Bitmap icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888);
   //  Initializes the image drawn by the canvas to icon Upper 
   Canvas canvas = new Canvas(icon);
   //  Create a brush 
   Paint photoPaint = new Paint(); 
   //  Get clearer image sampling and anti-jitter 
   photoPaint.setDither(true); 
   //  Filter 1 Lower, anti-drama teeth 
   photoPaint.setFilterBitmap(true);
   
   Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//  Create 1 Coordinates of the specified new rectangle 
   Rect dst = new Rect(0, 0, width, hight);//  Create 1 Coordinates of the specified new rectangle 
   canvas.drawBitmap(photo, src, dst, photoPaint);//  Will photo  Zoom or expand to dst Fill area used photoPaint
          // Custom brush 
   TextPaint textPaint=myTextPaint();
        drawText(canvas,textPaint,str,45,hight/5,width);
   
   canvas.save(Canvas.ALL_SAVE_FLAG);
   canvas.restore();
   
   imageView.setImageBitmap(icon);
   saveMyBitmap(this,icon);
 }

// Set the font and color of the brush 
 public TextPaint myTextPaint(){
   
   TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//  Set brush 
   int TEXT_SIZE = Math.round(25 * getRATIO());
   textPaint.setTextSize(TEXT_SIZE);//  Font size 
    textPaint.setTypeface(Typeface.DEFAULT_BOLD);//  Adopt the default width 
    textPaint.setColor(Color.argb(255,94,38,18));//  Colors used 
    return textPaint;

// Write text and wrap words 
 public void drawText(Canvas canvas, TextPaint Paint,String textString,int x,int y,int width) { 
  //int Width=Math.round(width* getRATIO());
  int start_x=Math.round(x * getRATIO());
  int start_y=Math.round(y * getRATIO());
  StaticLayout staticLayout=new StaticLayout(textString, Paint, width-start_x*2, 
        Alignment.ALIGN_NORMAL, 1.5f, 0.0f, false); 
   
  // Drawing position  
  canvas.translate(start_x, start_y); 
  staticLayout.draw(canvas); 
 }

Related articles: