【发布时间】:2022-10-04 21:37:42
【问题描述】:
我想将视图保存为高分辨率的位图。不幸的是,Kotlin 的drawToBitmap() 给出了一个非常小的位图(低分辨率)。我怎样才能得到一个高分辨率的位图呢?
这段代码应该创建一个更大的视图/布局的位图版本,但它并没有完全做到,我做错了什么?
int tableLayoutId = 1;
float scaleFactor = 4f;
TableLayout tableLayout = new TableLayout(Activity.this);
tableLayout.setId(tableLayoutId);
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TabLayout.LayoutParams.WRAP_CONTENT,
TabLayout.LayoutParams.WRAP_CONTENT));
tableLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tableLayout.layout(0, 0, tableLayout.getMeasuredWidth(), tableLayout.getMeasuredHeight());
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(Math.round(tableLayout.getWidth() * scaleFactor), Math.round(tableLayout.getHeight() * scaleFactor), Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
tableLayout.draw(bitmapCanvas);
如果您有更好的代码,请随时发布
-
什么叫低?什么叫高?
-
\"它不完全这样做\"——你到底是什么意思?
-
我不明白人们如何对 SO 投反对票。这个问题清楚地表明了研究(有一个完整的代码sn-p),这显然是一个合法的问题。上面的两个 cmets(要求更精确)是建设性的,无论是谁否决了这个问题,都只是促成了对 SO 的“敌对社区”的共同感觉。只是说。在评论中,所以没有人可以否决:-)。
标签: java android kotlin view bitmap