设置位图高度的scrollview(父级)高度有问题(从textview创建位图时-子级)

bxpogfeg  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(312)

我试图通过使用printhelper类打印位图来打印热敏打印机的收据位图由父滚动视图中的子textview组成,但如果发票超过屏幕大小,printhelper中的打印预览将不显示完整数据
邮件中的祝酒词 loadBitmapFromView() 方法总是显示相同的大小,不管实际大小如何。
代码如下:

<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;
     }
 }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题