我正在尝试将cardview绘制到其画布上的pdf上。不幸的是没有绘制卡片视图。但是会绘制一些测试文本。为什么我的carview没有画出来?
代码:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}
查看位图:
application.yml
答案 0 :(得分:1)
以下代码将CardView绘制到位图 但它没有画出阴影剂量。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = getLayoutInflater().inflate(R.layout.card_view, null, false);
Bitmap bitmap = loadBitmapFromView(view, 500, 500);
//This is sample code. So I print the bitmap to ImageView, not to PDF.
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageBitmap(bitmap);
}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
v.setMinimumWidth(width);
v.setMinimumHeight(height);
v.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
);
v.layout(0, 0, width, height);
v.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
card_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#33000000">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:gravity="center"
android:text="This is sample."/>
</android.support.v7.widget.CardView>
</FrameLayout>