Android,将Layout绘制到画布时TextView内容的问题

时间:2017-07-03 12:43:05

标签: android android-layout canvas xamarin bitmap

我尝试在Android上进行屏幕截图,但仅限于部分屏幕(包含一些视图的布局)。

除了Textviews之外,一切正常。正如您在下面看到的,我的Textview的内容被切断了。我不明白为什么。

请帮忙吗?

这就是我需要的东西和我拥有的东西(首先是真正的截图,第二个是用我的代码创建的位图)。正如你所看到的,一些textview缺少文字:(

真正的截图: enter image description here

我的代码创建的位图: enter image description here

这是我的代码AXML:

<LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:id="@+id/activity_moovelive_linearlayout_printScreenAndShareToSocialNetwork">
                    <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            android:layout_margin="5dp"
                            android:id="@+id/activity_moovelive_linearlayout_sport">
                            <FFImageLoading.Views.ImageViewAsync
                                android:id="@+id/activity_moovelive_imageview_iconesport"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:scaleType="fitCenter"
                                android:adjustViewBounds="true"
                                android:maxWidth="42dp"
                                android:maxHeight="42dp"
                                android:layout_alignParentLeft="true" />
                            <TextView
                                android:id="@+id/activity_moovelive_textview_sport"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:paddingLeft="10dp"
                                android:textSize="18sp" />
                        </LinearLayout>
                    <com.google.android.gms.maps.MapView
                        android:id="@+id/activity_moovelive_map"
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        android:layout_margin="5dp"
                        map:mapType="normal"
                        map:liteMode="true" />
                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center|bottom"
                        android:layout_margin="10dp"
                        android:layout_centerVertical="true"
                        android:id="@+id/activity_moovelive_linearlayout_chrono" >
                        <TextView
                            android:id="@+id/activity_moovelive_textview_chrono"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/moovenow_noire"
                            android:textSize="36sp" />
                        <TextView
                            android:id="@+id/activity_moovelive_textview_chrono_ms"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/moovenow_noire"
                            android:textSize="18sp" />
                    </LinearLayout>
                    <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="10dp">
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:layout_weight="1">
                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:gravity="center">
                            <TextView
                                android:id="@+id/activity_moovelive_textview_distanceParcourue_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textColor="@color/moovenow_noire"
                                android:textSize="18sp"
                                android:textStyle="bold" />
                            <TextView
                                android:id="@+id/activity_moovelive_textview_distanceParcourue_desc"
                                android:text="@string/fragment_home_chronoMoovelive_textview_distanceParcourue_desc"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textColor="@color/moovenow_grisfonce"
                                android:textSize="14sp" />
                        </LinearLayout>
                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:gravity="center">
                            <TextView
                                android:id="@+id/activity_moovelive_textview_vitesseMoyenne_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textStyle="bold"
                                android:textColor="@color/moovenow_noire"
                                android:textSize="18sp" />
                            <TextView
                                android:id="@+id/activity_moovelive_textview_vitesseMoyenne_desc"
                                android:text="@string/fragment_home_chronoMoovelive_textview_vitesseMoyenne_desc"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textColor="@color/moovenow_grisfonce"
                                android:textSize="14sp" />
                        </LinearLayout>
                    </LinearLayout>
                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:layout_weight="1">
                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:gravity="center">
                            <TextView
                                android:id="@+id/activity_moovelive_textview_rythme_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textStyle="bold"
                                android:textColor="@color/moovenow_noire"
                                android:textSize="18sp" />
                            <TextView
                                android:id="@+id/activity_moovelive_textview_rythme__desc"
                                android:text="@string/fragment_home_chronoMoovelive_textview_rythme_desc"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textColor="@color/moovenow_grisfonce"
                                android:textSize="14sp" />
                        </LinearLayout>
                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="10dp"
                            android:gravity="center">
                            <TextView
                                android:id="@+id/activity_moovelive_textview_denivele_value"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textStyle="bold"
                                android:textColor="@color/moovenow_noire"
                                android:textSize="18sp" />
                            <TextView
                                android:id="@+id/activity_moovelive_textview_denivele_desc"
                                android:text="@string/fragment_home_chronoMoovelive_textview_denivele_desc"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:textColor="@color/moovenow_grisfonce"
                                android:textSize="14sp" />
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>
                </LinearLayout>

以下是将布局绘制到画布中的代码:

//Create the bitmap with the content of the view... full width / minimum height possible
                int iMeasureSpec = View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);
                m_layoutPrintScreenAndShareToSocialNetwork.Measure(iMeasureSpec, iMeasureSpec);

                Android.Util.DisplayMetrics metrics = new Android.Util.DisplayMetrics();
                WindowManager.DefaultDisplay.GetMetrics(metrics);

                int iMeasuresWidth = metrics.WidthPixels;
                int iMeasuredHeight = m_layoutPrintScreenAndShareToSocialNetwork.MeasuredHeight;

                Bitmap bitmap = Bitmap.CreateBitmap(iMeasuresWidth, iMeasuredHeight, Bitmap.Config.Argb8888);
                bitmap.EraseColor(Color.Transparent);

                //Draw on canvas
                Canvas canvas = new Canvas(bitmap);
                m_layoutPrintScreenAndShareToSocialNetwork.Draw(canvas);

提前感谢您的任何帮助或建议! :)

1 个答案:

答案 0 :(得分:0)

我终于找到了解决方法。我没有用方法layout.Draw(Bitmap)创建画布,而是使用视图的drawingCache。 请遵循以下代码:

        m_layoutPrintScreenAndShareToSocialNetwork.DrawingCacheEnabled = true;
        m_layoutPrintScreenAndShareToSocialNetwork.BuildDrawingCache();

        Bitmap screenshotBitmap = m_layoutPrintScreenAndShareToSocialNetwork.DrawingCache;

        //.... [save bitmap to file]

        m_layoutPrintScreenAndShareToSocialNetwork.DrawingCacheEnabled = false;
        m_layoutPrintScreenAndShareToSocialNetwork.DestroyDrawingCache();