2017-02-17 116 views
3

文字我試圖使用下面的代碼繪製的圖像文本:繪製圖像

procedure TfMain.TextToImage(const AText: string; const AImage: TImage); 
begin 
    if NOT Assigned(AImage) then Exit;  
    AImage.Canvas.BeginScene; 
    AImage.Canvas.Font.Size := 18; 
    AImage.Canvas.Font.Family := 'Arial'; 
    AImage.Canvas.Fill.Color := TAlphaColorRec.Dodgerblue; 
    AImage.Canvas.Font.Style := [TFontStyle.fsbold]; 
    AImage.Canvas.FillText(AImage.AbsoluteRect, 
          AText, 
          False, 
          1, 
          [TFillTextFlag.RightToLeft], 
          TTextAlign.taCenter, 
          TTextAlign.taCenter); 
    AImage.Canvas.EndScene; 
end; 

問:爲什麼上面的程序在Windows上運行,但不是在android系統?

+0

確定'android'標籤適當方式固接? – azizbekian

+0

@azizbekian我猜是的,你看過我的Q了嗎?或者你只是看着代碼:) – RepeatUntil

+0

在Android中,我們可以通過自定義視圖來實現這一點,我們需要添加所有想要應用到Android中的文本的TTF文件。 –

回答

4

嘗試直接在TImage中,而不是TImage中的Canvas的TBitmap繪製。

您也需要創建圖像的位圖您嘗試訪問它之前:

AImage.Bitmap := TBitmap.Create(Round(AImage.Width), Round(AImage.Height)); 

所以正確的代碼將是這樣的:

procedure TfMain.TextToImage(const AText: string; const AImage: TImage); 
begin 
    if NOT Assigned(AImage) then Exit; 
    AImage.Bitmap := TBitmap.Create(Round(AImage.Width), Round(AImage.Height)); 
    AImage.Bitmap.Canvas.BeginScene; 
    try 
    //.... 
    // Use AImage.Bitmap.Canvas as needed 
    AImage.Bitmap.Canvas.FillText(AImage.AbsoluteRect, 
          AText, 
          False, 
          1, 
          [], 
          TTextAlign.Center, 
          TTextAlign.Center); 
    finally 
    AImage.Bitmap.Canvas.EndScene; 
    end; 
end; 

TImage.Paint事件代碼你會看到:

procedure TImage.Paint; 
var 
    R: TRectF; 
begin 
    if (csDesigning in ComponentState) and not Locked and not FInPaintTo then 
    begin 
    R := LocalRect; 
    InflateRect(R, -0.5, -0.5); 
    Canvas.DrawDashRect(R, 0, 0, AllCorners, AbsoluteOpacity, $A0909090); 
    end; 

    UpdateCurrentBitmap; 
    if FCurrentBitmap <> nil then 
    DrawBitmap(Canvas, LocalRect, FCurrentBitmap, AbsoluteOpacity); 
end; 

所以無論你在c上畫什麼anvas,在下一次重繪時,它將再次繪製完整的位圖,擦除您已經完成的任務。

如果你不想碰位比你需要重寫OnPaint事件TImage和呼叫function TextToImage(const AText: string; const AImage: TImage);

0

見下自定義類ImageView使用文本中Android.But極品屁股TTF或OTF文件更typfaces。

package com.fuzzydev; 

import com.fuzzydev.labeledimageview.R; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.ImageView; 

/* 
* ---------------------------------------------------------------------------- 
* "THE BEER-WARE LICENSE" (Revision 42): 
* As long as you retain this notice you 
* can do whatever you want with this stuff. If we meet some day, and you think 
* this stuff is worth it, you can buy me a beer in return Dejan Ristic 
* ---------------------------------------------------------------------------- 
*/ 

public class LabeledImageView extends ImageView { 

    private static final String TAG = "LabeledImageView"; 

    private static final int DEFAULT_TEXT_STYLE = Typeface.NORMAL; 
    private static final int DEFAULT_TEXT_COLOR = Color.WHITE; 
    private static final int DEFAULT_SCREEN_LOCATION = 0; 
    private static final int DEFAULT_X_OFFSET = 30; 
    private static final int DEFAULT_Y_OFFSET = 30; 

    private static final float DEFAULT_TEXT_SIZE = 40f; 

    private float textSize; 
    private float xPos, yPos; 
    private float xOffset, yOffset; 

    private int labelLocation; 
    private int textStyle; 
    private int textColor; 

    private String text; 
    private String customFont; 

    private Paint mTextPaint; 

    public LabeledImageView(Context context) { 
     super(context); 
     initWithDefautls(); 
    } 

    public LabeledImageView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initWithAttrs(attrs, context); 
    } 

    private void initWithDefautls() { 
     textSize = DEFAULT_TEXT_SIZE; 
     textStyle = DEFAULT_TEXT_STYLE; 
     textColor = DEFAULT_TEXT_COLOR; 
     labelLocation = DEFAULT_SCREEN_LOCATION; 
     xOffset = DEFAULT_X_OFFSET; 
     yOffset = DEFAULT_Y_OFFSET; 
     setTextPaint(); 
    } 

    private void initWithAttrs(AttributeSet attrs, Context context) { 
     initAttrs(context, attrs); 
     setTextPaint(); 
     if (customFont != null) { 
      setCustomFont(context); 
     } 
    } 

    private void initAttrs(Context context, AttributeSet attrs) { 
     TypedArray a = context.getTheme().obtainStyledAttributes(attrs, 
       R.styleable.LabeledImageView, 0, 0); 

     try { 
      text = a.getString(R.styleable.LabeledImageView_text); 
      textSize = a.getFloat(R.styleable.LabeledImageView_textSizePx, 
        DEFAULT_TEXT_SIZE); 
      textStyle = a.getInt(R.styleable.LabeledImageView_textStyle, 
        DEFAULT_TEXT_STYLE); 
      textColor = a.getInt(R.styleable.LabeledImageView_textColor, 
        DEFAULT_TEXT_COLOR); 
      labelLocation = a.getInt(
        R.styleable.LabeledImageView_labelPosition, 
        DEFAULT_SCREEN_LOCATION); 
      customFont = a.getString(R.styleable.LabeledImageView_customFont); 
      xOffset = a.getFloat(R.styleable.LabeledImageView_xOffset, 
        DEFAULT_X_OFFSET); 
      yOffset = a.getFloat(R.styleable.LabeledImageView_yOffset, 
        DEFAULT_Y_OFFSET); 
      ; 
     } finally { 
      a.recycle(); 
     } 
    } 

    private void setCustomFont(Context ctx) { 
     Typeface tf = null; 
     try { 
      tf = Typeface.createFromAsset(ctx.getAssets(), customFont); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not get typeface: " + e.getMessage()); 
     } 
     mTextPaint.setTypeface(tf); 
    } 

    private void setLabelLocation() { 

     switch (labelLocation) { 
     case 0: // Top Left 
      xPos = xOffset; 
      yPos = yOffset; 
      break; 
     case 1: // Top Right 
      xPos = getWidth() - mTextPaint.measureText(text) - xOffset; 
      yPos = yOffset; 
      break; 
     case 2: // Bottom Left 
      xPos = xOffset; 
      yPos = getHeight() - yOffset; 
      break; 
     case 3: // Bottom Right 
      xPos = getWidth() - mTextPaint.measureText(text) - xOffset; 
      yPos = getHeight() - yOffset; 
      break; 
     case 4: // Top Center 
      xPos = (getWidth()/2) - (mTextPaint.measureText(text)/2); 
      yPos = yOffset; 
      break; 
     case 5: // Bottom Center 
      xPos = (getWidth()/2) - (mTextPaint.measureText(text)/2); 
      yPos = getHeight() - yOffset; 
      break; 
     case 6: // Center 
      xPos = (getWidth()/2) - (mTextPaint.measureText(text)/2); 
      yPos = (getHeight()/2); 
     default: 
      break; 
     } 
    } 

    @Override 
    public void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 
     setLabelLocation(); 
    } 

    private void setTextPaint() { 
     mTextPaint = new Paint(); 
     mTextPaint.setTextSize(textSize); 
     mTextPaint.setColor(textColor); 
     mTextPaint.setTypeface(Typeface.defaultFromStyle(textStyle)); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     if (text != null) { 
      canvas.drawText(text, xPos, yPos, mTextPaint); 
     } 
    } 

    public void setTextSize(float textSize) { 
     this.textSize = textSize; 
    } 

    public void setCustomFont(String customFont) { 
     this.customFont = customFont; 
    } 

    public void setLabelLocation(int labelLocation) { 
     this.labelLocation = labelLocation; 
    } 

    public void setyOffset(float yOffset) { 
     this.yOffset = yOffset; 
    } 

    public void setxOffset(float xOffset) { 
     this.xOffset = xOffset; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

    public void setTextStyle(int textStyle) { 
     this.textStyle = textStyle; 
    } 

    public void setTextColor(int textColor) { 
     this.textColor = textColor; 
    } 
} 

見上文類執行這一

https://github.com/DejanRistic/LabeledImageView/tree/master/src/com/fuzzydev

鏈接

+0

這看起來像Delphi代碼嗎?這個問題清楚地標記爲* Delphi *,所以Java解決方案並沒有太多幫助。 –