滚动视图父级高度设置位图高度的问题当从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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...