Android 如何在画布外部绘制并制作位图?
我正在画几行文字。几行之后,它就离开了屏幕。我要做的是捕获位图中的所有行(以及画布外的行)Android 如何在画布外部绘制并制作位图?,android,canvas,view,bitmap,Android,Canvas,View,Bitmap,我正在画几行文字。几行之后,它就离开了屏幕。我要做的是捕获位图中的所有行(以及画布外的行) I have the code below which only works for within the canvas(screen). private class DeciderView extends View { private Paint paint; String text = ""; String[] options = { "ee
I have the code below which only works for within the canvas(screen).
private class DeciderView extends View {
private Paint paint;
String text = "";
String[] options = { "een", "twee", "drie", "vier", "vijf", "zes", "zeven", "acht",
"negen", "tien", "elf", "twaalf", "dertien", "vertien", "vijftien" };
public DeciderView(Context context) {
super(context);
// Keep screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Remove title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setBackgroundColor(Color.WHITE);
paint = new Paint();
paint.setTextSize(75);
paint.setColor(Color.BLACK);
paint.setStrokeWidth(10);
paint.setAntiAlias(true);
setDrawingCacheEnabled(true);
setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH);
}
private void drawInput(Canvas canvas) {
Paint p = new Paint();
p.setTextAlign(Align.CENTER);
p.setTextSize(canvas.getWidth() / 10f);
p.setColor(Color.BLACK);
float xText = canvas.getWidth() / 2f;
float yText = (canvas.getHeight() / 4f);
for (int i = 0; i < options.length; i++) {
text += options[i] + "\n";
}
for (String line : text.split("\n")) {
canvas.drawText(line, xText, yText, p);
yText -= (p.ascent() + p.descent()) * 2.5f;
}
}
@Override
public void onDraw(Canvas canvas) {
drawInput(canvas);
this.setDrawingCacheEnabled(true);
Bitmap b = Bitmap.createBitmap(this.getDrawingCache());
b = Bitmap.createScaledBitmap(b, canvas.getWidth(), canvas.getHeight(), false);
try {
b.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(
new File(Environment.getExternalStorageDirectory().getPath()
+ "/Pictures/aaaaa.jpg")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (b != null) {
b.recycle();
b = null;
}
}
}
下面的代码仅适用于画布(屏幕)中的。
私有类DeciderView扩展视图{
私人油漆;
字符串文本=”;
字符串[]选项={“een”、“twee”、“drie”、“vier”、“vijf”、“zes”、“zeven”、“acht”,
“negen”、“tien”、“elf”、“twaalf”、“dertien”、“vertien”、“vijftien”};
公共决策视图(上下文){
超级(上下文);
//保持屏幕打开
getWindow().addFlags(WindowManager.LayoutParams.FLAG\u保持屏幕打开);
//删除标题栏
requestWindowFeature(窗口。功能\u无\u标题);
//删除通知栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_全屏,
WindowManager.LayoutParams.FLAG(全屏);
这个.setBackgroundColor(Color.WHITE);
油漆=新油漆();
油漆.尺寸(75);
油漆。设置颜色(颜色。黑色);
油漆。设置行程宽度(10);
paint.setAntiAlias(真);
setDrawingCacheEnabled(真);
设置绘图缓存质量(绘图缓存质量高);
}
私有void drawInput(画布){
油漆p=新油漆();
p、 setTextAlign(Align.CENTER);
p、 setTextSize(canvas.getWidth()/10f);
p、 设置颜色(颜色为黑色);
float xText=canvas.getWidth()/2f;
float yText=(canvas.getHeight()/4f);
对于(int i=0;i
因此,基本上我想从所有行生成位图,即使这些行是在画布外部绘制的。为什么不直接绘制到位图?您可以创建一个有位图支持的新画布。使用普通的画布绘制命令,输出将写入位图而不是屏幕。如果您还想将其绘制到屏幕上,只需在最后将新位图绘制到屏幕上即可。要做到这一点,你只需要
Canvas myCanvas = new Canvas(myBitmap);
谢谢,这个很好用。我遇到的唯一问题是位图保存到SD卡后质量较低。