我正在尝试从我的RelativeLayout生成图像并希望共享它。我设法生成图像并共享,但是它在我的图像后面绘制了黑色背景,我尝试将颜色设置为透明,但仍然具有黑色背景。我的代码看起来像这样
findViewById(R.id.actionImage).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//constant.ad_count = constant.ad_count + 1;
// methods.ShowIntAds();
Bitmap bitmap =getBitmapFromView(main);
try {
File file = new File(QuoteDetailsActivity.this.getExternalCacheDir(),"logicchip.png");
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
//file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/png");
startActivity(Intent.createChooser(intent, "Share image via"));
} catch (Exception e) {
e.printStackTrace();
}
}
});
和
private Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
canvas.drawColor(Color.TRANSPARENT);
/*Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
} else{
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
}*/
view.draw(canvas);
return returnedBitmap;
}
我的XML
<?xml version="1.0" encoding="utf-8"?>
<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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".activities.QuoteDetailsActivity"
tools:showIn="@layout/quote_details_activity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_background"
android:orientation="vertical"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<ImageView
android:contentDescription="@null"
android:layout_width="@dimen/likeIcon"
android:layout_height="@dimen/likeIcon"
android:layout_gravity="center_vertical"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="1dp"
android:tint="@color/quote_time_icon"
android:src="@drawable/ic_time" />
<TextView
android:id="@+id/lblTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_weight="1"
android:textColor="@color/quote_time_text"
android:textSize="@dimen/lblTime"
android:textStyle="bold" />
<TextView
android:id="@+id/textPageCount1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:textColor="@color/quote_time_text"
android:textSize="@dimen/lblTime"
android:textStyle="bold" />
</LinearLayout>
<RelativeLayout
android:id="@+id/mybg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.view.ViewPager
android:id="@+id/quoteViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
</android.support.v4.view.ViewPager>
</RelativeLayout>
<LinearLayout
android:id="@+id/admob"
android:orientation="horizontal"
android:visibility="gone"
android:gravity="center"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<View
android:id="@+id/bottomShadow"
android:layout_width="fill_parent"
android:layout_height="1.5dp"
android:background="@color/toolbar_devider" >
</View>
<LinearLayout
android:id="@+id/pageNavLayout"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar"
android:background="@drawable/title_bar"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/butPrev"
style="@style/SelectableItemBackground"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true">
<ImageView
android:contentDescription="@null"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:scaleType="fitCenter"
android:src="@drawable/tool_prev"
android:tint="@color/toolbar_icon"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<TextView
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/toolbar_devider" />
<RelativeLayout
android:id="@+id/img_fav"
style="@style/SelectableItemBackground"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true">
<ImageView
android:contentDescription="@null"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:layout_centerInParent="true"
android:src="@drawable/ic_clipboard"
android:tint="@color/toolbar_icon" />
</RelativeLayout>
<TextView
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/toolbar_devider" />
<RelativeLayout
android:id="@+id/actionImage"
style="@style/SelectableItemBackground"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true">
<ImageView
android:contentDescription="@null"
android:id="@+id/imgImage"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:src="@drawable/ic_background"
android:layout_centerInParent="true"
android:tint="@color/toolbar_icon" />
</RelativeLayout>
<TextView
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/toolbar_devider" />
<RelativeLayout
android:id="@+id/actionFav"
style="@style/SelectableItemBackground"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true">
<ImageView
android:contentDescription="@null"
android:id="@+id/imgFavourite"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:src="@drawable/fav_new_ic"
android:layout_centerInParent="true"
android:tint="@color/toolbar_icon" />
</RelativeLayout>
<TextView
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/toolbar_devider" />
<RelativeLayout
android:id="@+id/butShare"
style="@style/SelectableItemBackground"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true">
<ImageView
android:contentDescription="@null"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:layout_centerInParent="true"
android:src="@drawable/fab_share"
android:tint="@color/toolbar_icon" />
</RelativeLayout>
<TextView
android:layout_width="1.5dp"
android:layout_height="match_parent"
android:background="@color/toolbar_devider" />
<RelativeLayout
android:id="@+id/butNext"
style="@style/SelectableItemBackground"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:focusable="true">
<ImageView
android:contentDescription="@null"
android:layout_width="@dimen/nav_but_size"
android:layout_height="@dimen/nav_but_size"
android:layout_centerInParent="true"
android:src="@drawable/tool_next"
android:tint="@color/toolbar_icon" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
注意:我想获取名为mybg的视图图像
让我知道是否有人可以帮助我生成没有黑色背景的全尺寸图像。 谢谢