2017-02-17 144 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解决方案并没有太多帮助。 –