2013-07-05 56 views
1

我試圖在圖像上繪製一個字符串,代碼工作但沒有獲得透明度我已經使用了幾個alpha值,但不起作用。在Android中的位圖上繪製Trasparent文本

paint.setAlpha(alpha); 

有人能告訴我什麼是透明或錯在這裏做什麼IM值的範圍

public static Bitmap drawtext(Bitmap src, String txt,int alpha) { 
     int w = src.getWidth(); 
     int h = src.getHeight(); 
     Bitmap result = Bitmap.createBitmap(w, h, src.getConfig()); 
     Canvas canvas = new Canvas(result); 
     canvas.drawBitmap(src, 0, 0, null); 
     Paint paint = new Paint(); 
     paint.setAlpha(alpha); 
     paint.setColor(Color.RED); 
     paint.setTextSize(18); 
     paint.setAntiAlias(true); 
     paint.setUnderlineText(true); 
     canvas.drawText(txt, 20, 25, paint); 

     return result; 
    } 
+0

什麼是阿爾法?試試這個'paint.setAlpha(0x80)'。 – Raghunandan

+0

@Raghunandan我嘗試了100的值,這個0X80代表 – techno

+0

這就是alpha值。顏色表示爲壓縮整數,由4個字節組成:阿爾法,紅色,綠色,藍色。 – Raghunandan

回答

0

試試這個代碼或download demo here

public Bitmap drawText(String text, int textWidth, int color) { 

      // Get text dimensions 
      TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG); 
      textPaint.setStyle(Paint.Style.FILL); 
      textPaint.setColor(Color.parseColor("#ff00ff")); 
      textPaint.setTextSize(30); 

      StaticLayout mTextLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false); 

      // Create bitmap and canvas to draw to 
      Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Bitmap.Config.ARGB_4444); 
      Canvas c = new Canvas(b); 

      // Draw background 
      Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG); 
      paint.setStyle(Paint.Style.FILL); 
      paint.setColor(color); 
      c.drawPaint(paint); 

      // Draw text 
      c.save(); 
      c.translate(0, 0); 
      mTextLayout.draw(c); 
      c.restore(); 

      return b; 
     } 

設置返回的圖像的ImageView

//background transparent 

     int colorT=Color.TRANSPARENT; 
     Bitmap img1=drawText(text,width,colorT); 
     img2.setImageBitmap(img1);