我正在尝试在非活动类中获取视图并将其转换为位图,这是我的尝试,但视图似乎有0作为维度。 我的简单xml文件:
custom_view.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@drawable/pin_events"
android:id="@+id/rel">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerInside"
android:src="@drawable/sport"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
这是我尝试获取位图:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.custom_view, null);
RelativeLayout view = (RelativeLayout) v.findViewById(R.id.rel);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bm = view.getDrawingCache();
答案 0 :(得分:1)
您可以尝试以下方法:
view.setDrawingCacheEnabled(true);
view.setLayoutParams(new ViewGroup.LayoutParams(0, 0));
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache