Skip to content

Instantly share code, notes, and snippets.

@fnk0
Last active August 29, 2015 14:17
Show Gist options
  • Save fnk0/5d174d8743fcf76d78a4 to your computer and use it in GitHub Desktop.
Save fnk0/5d174d8743fcf76d78a4 to your computer and use it in GitHub Desktop.
Inflating view
View v = LayoutInflater.from(activityContext).inflate(R.layout.fragment_share, null);
v.setLayoutParams(new RelativeLayout.LayoutParams(2048, 1536));
LinearLayout shareLayout = (LinearLayout) v.findViewById(R.id.share_layout);
shareLayout.setLayoutParams(new LinearLayout.LayoutParams(2048, 1536));
Bitmap b = PictureUtils.loadBitmapFromView(shareLayout);
PictureUtils.saveBitmap(b, false);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:id="@+id/share_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/green_200"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey_white_1000">
<ImageView
android:id="@+id/share_image"
android:layout_width="match_parent"
android:layout_height="250dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<TextView
android:id="@+id/canopy_cover"
android:textColor="@color/grey_white_1000"
android:textSize="20sp"
android:text="Canopy Cover: 26.5%"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Jan 16, 2014"
android:textColor="@color/grey_white_1000"
android:textColorHint="@android:color/white"
android:textSize="16sp" />
</RelativeLayout>
<EditText
android:id="@+id/user_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="User message"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/activity_vertical_margin">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_toLeftOf="@+id/title"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:gravity="center_vertical"
android:text="Canopeo"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@android:color/white" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_below="@id/share_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
<Button
android:id="@+id/share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:background="@drawable/button_selector"
android:text="SHARE"
android:textColor="@android:color/white" />
</LinearLayout>
</RelativeLayout>
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;
}
public static String saveBitmap(Bitmap bm, File mediaStorageDir, String fileName) {
// Create a media file name
File mediaFile = new File(mediaStorageDir.getPath() + File.separator + fileName);
OutputStream fOutputStream = null;
try {
fOutputStream = new FileOutputStream(mediaFile);
bm.compress(Bitmap.CompressFormat.JPEG, 90, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
return mediaFile.getPath();
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment