我在画布上绘制旋转文本,然后将其以 JPEG 格式保存到 SD 卡中。我面临的问题是画布预览看起来不错,但保存图像中的旋转文本没有正确显示。当我使用默认的 Android 字体时,最终的 JPEG 与画布预览相同,但此代码不适用于自定义字体。
我正在使用自定义视图类进行画布绘图,这是我的代码
public class MyBringBack extends View {
Bitmap bitmap;
Typeface type;
public MyBringBack(Context context) {
super(context);
// TODO Auto-generated constructor stub
type = Typeface.createFromAsset(context.getAssets(),"fonts/rockwell-bold.ttf");
setDrawingCacheEnabled(true);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
// paint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
paint.setTextSize(23);
Paint paint1 = new Paint();
paint1.setTextSize(40);
paint1.setColor(Color.RED);
canvas.rotate(90,434,110);
canvas.drawText("Demo Text Demo Text Demo Text ", 434, 110, paint);
canvas.restore();
canvas.save();
paint1.setTypeface(type);
canvas.rotate(90,130,185);
canvas.drawText("Text using Typeface ", 130, 185, paint1);
canvas.restore();
canvas.save();
canvas.rotate(90,180,185);
canvas.drawText("Text using Typeface ",180, 185, paint1);
canvas.restore();
canvas.save();
canvas.rotate(90,230,185);
canvas.drawText("Text using Typeface ", 230, 185, paint1);
canvas.restore();
canvas.save();
this.setDrawingCacheEnabled(true);
Bitmap c= Bitmap.createScaledBitmap(this.getDrawingCache(), canvas.getWidth(),canvas.getHeight(), false);
/* Saving File To SD Card */
OutputStream outStream = null;
File bgv = new File("/sdcard/");
/* To build directory if needed */
bgv.mkdirs();
File file = new File(bgv, "final22.jpg");
try {
outStream = new FileOutputStream(file);
c.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我的代码有问题吗?
请帮忙...
提前致谢 :)