回転したテキストをキャンバスに描画し、SD カードに JPEG として保存しています。私が直面している問題は、キャンバスのプレビューは問題ないように見えますが、保存された画像の回転したテキストが正しく表示されないことです。デフォルトの 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();
}
}
}
私のコードに問題はありますか?
助けてください...
前もって感謝します :)