如何解决滚动视图父级高度设置位图高度的问题当从textview创建位图时-子级
我正在尝试通过使用printhelper类打印位图来打印热敏打印机的收据 位图由父滚动视图中的子textview组成 但是如果发票超过屏幕大小,则printHelper中的打印预览不会显示完整数据
尽管实际大小,loadBitmapFromView()
方法中的Toast消息始终显示相同的大小。
代码在这里:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SalesInvoicePreviewActivity">
<ScrollView
android:id="@+id/sales_preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/summary_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="16sp" />
</ScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/sales_print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="32dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:text="Print Invoice"/>
</RelativeLayout>
TextView tv;
ScrollView scrollView;
FloatingActionButton printInvoiceButton;
PrintHelper.OnPrintFinishCallback on;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sales_invoice_preview);
setTitle("Preview & print Sales Invoice");
Intent intent = getIntent();
String message = intent.getStringExtra(SalesInvoiceActivity.EXTRAS_CONTENT);
on = () -> {
finish();
startActivity(new Intent(SalesInvoicePreviewActivity.this,SalesInvoiceActivity.class));
};
tv = findViewById(R.id.summary_tv);
tv.setDrawingCacheEnabled(true);
scrollView = findViewById(R.id.sales_preview);
printInvoiceButton = findViewById(R.id.sales_print);
tv.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tv.setText(message);
tv.setTextColor(Color.BLACK);
printInvoiceButton.setOnClickListener(view -> {
printInvoice(tv);
});
}
public void printInvoice(TextView v) {
Bitmap b2 = loadBitmapFromView(v);
// b2.setImageBitmap(summary.getDrawingCache());
PrintHelper photoPrinter = new PrintHelper(this);
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
photoPrinter.printBitmap("printing sales invoice",b2,on);
Toast.makeText(this,"printing invoice",Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed() {
startActivity(new Intent(SalesInvoicePreviewActivity.this,SalesInvoiceActivity.class));
}
public Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(scrollView.getWidth(),scrollView.getHeight(),Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
Toast.makeText(SalesInvoicePreviewActivity.this,scrollView.getHeight() + " = height",Toast.LENGTH_SHORT).show();
v.layout(v.getLeft(),v.getTop(),v.getRight(),v.getBottom());
scrollView.draw(c);
return b;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。