为什么我的卡片视图无法绘制到画布上? [英] Why is my cardview not being drawn onto canvas?
本文介绍了为什么我的卡片视图无法绘制到画布上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将cardview绘制到其画布pdf上.不幸的是,没有绘制卡片视图.但是,绘制了一些测试文本.为什么没有画我的Carview?
I'm trying to draw a cardview onto a pdf its canvas. Unfortunately the cardview is not drawn. However some test text is drawn. Why is my carview not drawn?
代码:
// start a page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas c = page.getCanvas();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(1000, 50);
CardView card = new CardView(this);
card.setLayoutParams(params);
card.setRadius(4f);
Bitmap cardBitmap = Utils.loadBitmapFromView(card);
c.drawText("tessssssttt", 30, 30, p);
c.drawBitmap(cardBitmap, new Rect(0,0,1000,50), new Rect(5, 50, 995, 100), p);
document.finishPage(page);
查看位图:
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;
}
推荐答案
以下代码将CardView绘制到位图.
但是没有画阴影.
The following code draws CardView to bitmap.
But it's shadow dose not drawn.
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:
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:
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>
这篇关于为什么我的卡片视图无法绘制到画布上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文