capture frame layout has this issue - android

this is my xml file:
<RelativeLayout
android:id="#+id/relativeLayoutTemplateMain"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/LinearLayouttools"
android:layout_above="#+id/lltexttools"
android:layout_gravity="center">
<FrameLayout
android:id="#+id/frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="centerInside" />
</FrameLayout>
</RelativeLayout>
i capture framelayout with this code:
fOut = new FileOutputStream(file);
framelayout.setDrawingCacheEnabled(true);
Bitmap bitmap = framelayout.getDrawingCache();
bitmap.compress(Bitmap.CompressFormat.jpeg, 100, fOut);
fOut.flush();
fOut.close();
but Around of the picture is black:
http://uupload.ir/files/z8cv_smsbaaz.ir_4.jpg
also when i set scaleType="FitXY":
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="FitXY"/>
image scale(i dont want scale image)
how i solve this problem?

you can use below method to create view to Bitmap .
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;
}
Converting a view to Bitmap without displaying it in Android?

Related

Converting FrameLayout to Video?

In my Framelayout I have an ImageView and TextView and I was able to convert the FrameLayout into a Bitmap and Save it by using the following snippet code
Bitmap bitmap = Bitmap.createBitmap(frameLayout.getWidth(), frameLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
frameLayout.draw(canvas);
try {
FileOutputStream fileOutputStream = new FileOutputStream(Environment.getExternalStorageDirectory() + path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
}
Now I want to achieve the same behavior with my VideoView. So I have another FrameLayout with VideoView and TextView and I want to convert that FrameLayout into a VideoView so the TextView is incorporated inside the Video.
I am wondering if it is possible and any ideas are welcome..
Edit 1: My .xml content
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<VideoView
android:id="#+id/surface_camera"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/buttonstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" /></FrameLayout>

Exception: android.widget.LinearLayout cannot be cast to android.widget.ImageView

I need to set the images to grid view dynamically. but i'm getting exception: android.widget.LinearLayout cannot be cast to android.widget.ImageView
I need to get the view of the grid without onclick. So please help me.
private void setTickMark(String icon_name_val) {
ImageView imageView = (ImageView) rootView;
//getting clicked image
String iconimg = icon_name_val;
String iconimgname = iconimg;
// int idval = getResources().getIdentifier(iconimgname, "drawable",getActivity().getPackageName());
// Drawable drawable = getResources().getDrawable(idval);
// Log.d("dra",String.valueOf(drawable));
int resID = getResources().getIdentifier(iconimgname, "drawable", getActivity().getPackageName());
Bitmap bitmapbus = BitmapFactory.decodeResource(getResources(), R.drawable.bus);
Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), resID);
Bitmap resized = Bitmap.createScaledBitmap(bitmap1, bitmapbus.getWidth(), bitmapbus.getHeight(), true);
Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.tick);
// Bitmap resultBitmap = Bitmap.createBitmap(bitmap2.getWidth(), bitmap2.getHeight(), Bitmap.Config.ARGB_8888);
Bitmap resultBitmap = Bitmap.createBitmap(bitmapbus.getWidth(), bitmapbus.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(resultBitmap);
c.drawBitmap(resized, 0, 0, null);
c.drawBitmap(bitmap2, 0, 0, null);
imageView.setImageBitmap(resultBitmap);
}
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/popup"
android:layout_height="0dp"
android:layout_weight=".90"
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_marginTop="20dp">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:columnWidth="60dp"
android:numColumns="auto_fit"
android:verticalSpacing="20dp"
android:horizontalSpacing="20dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginTop="4dp"
android:duplicateParentState="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/save"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/cancel"/>
</LinearLayout>
</LinearLayout>
Add an imageview to your gridview
then only define ImageView imageView = (ImageView) rootView.findViewById(R.id.ImageView1);

Android: Take Screen Shoot for a layout

I am trying to create a PhotoFrame app, for that i have a frame and i add an image to that frame from gallery.This Frame is in one of the layout in the main layout am trying to take screen shoot for that layout.
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:id="#+id/root"
tools:context="com.ccs.photoframe.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="vertical">
<Button
android:id="#+id/btn_select"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Select Photo"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layout_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".8"
android:background="#drawable/photo_frame"
android:orientation="vertical"
>
<ImageView
android:id="#+id/img_save_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="70dp"
>
<ImageView
android:id="#+id/ivImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="matrix"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".1"
android:orientation="horizontal">
<Button
android:id="#+id/btn_save_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Save Photo"/>
</LinearLayout>
I am trying to take screen shoot in the LinearLayout "layout_frame"
I tried ...
btn_save_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View v1 = layout_frame.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);
}
});
But this taking screen shoot for the entire layout.
Is there any possible way to take screen shoot for the specific location.
Thanks in advance:)
Yes you can get screen shot of any view like this.
ImageView redoo_btn = (ImageView) findViewById(R.id.redoo_btn);
FrameLayout paintLayout = (FrameLayout) findViewById(R.id.drawing);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.drawing1);
Bitmap bm = Takeshot(linearLayout);
Bitmap bm = Takeshot(redoo_btn);
Bitmap bm = Takeshot(paintLayout);
Bitmap Takeshot(View view) {
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
// view.setImageBitmap(b);
return b;
}
Yes you can do this way:
LinearLayout mlinearlaoytfrme=(LinearLayout)findViewById(R.id.layout_frame);
Bitmap mBItmap=takeScreenshotReceipt();
img_save_photo = (ImageView) findViewById(R.id.img_save_photo);
img_save_photo.setVisibility(View.VISIBLE);
img_save_photo.setBackgroundDrawable(bitmapDrawable);
private Bitmap takeScreenshotReceipt()
{
View v1 = getRootView().findViewById(R.id.layout_frame);
v1.setDrawingCacheEnabled(true);
return v1.getDrawingCache();
}
Try this method
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}

Save Layout as Bitmap

I want to save my Linearlayout as an bmp.
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout_QrCode" >
<Space
android:layout_width="15dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
<ImageView
android:layout_width="185dp"
android:layout_height="185dp"
android:id="#+id/imageView_qrcode" />
<Space
android:layout_width="match_parent"
android:layout_height="15dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="417dp"
android:layout_height="match_parent" >
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:id="#+id/space2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView_QrSticker_label"
android:layout_alignParentStart="true"
android:layout_above="#+id/space2"
android:textStyle="bold"
android:textSize="40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView_QrSticker_serial"
android:layout_below="#+id/space2"
android:layout_alignParentStart="true"
android:textSize="30sp" />
</RelativeLayout>
</LinearLayout>
Therefore I use the following code to perform this:
public Bitmap viewToBitmap(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
But If I run this code my bitmap only contains the ImageView (ImageView_qrcode). I checked it. If I inflate the Linearview the TextViews are shown correctly but the output bmp only contains the ImageView does somebody know why?
You can to try this solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null) {
bgDrawable.draw(canvas);
} else {
canvas.drawColor(Color.WHITE);
}
view.draw(canvas);
return returnedBitmap;
}
There is solution with drawing cache:
public Bitmap getBitmapFromView(View view){
view.setDrawingCacheEnabled(true);
return view.getDrawingCache();
}
Use setDrawingCacheEnabled(true);
View content = (LinearLayout)findViewById(R.id.linearLayout_QrCode);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File file = new File("/sdcard/" + yourimagename + ".png");
try {
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
content.invalidate();
} catch (Exception e) {
e.printStackTrace();
} finally {
content.setDrawingCacheEnabled(false);
}
Thanks to #Gil Moshayof it works now!
After calling the getBitmapFromView method via post it works fine
qrCodeSticker.setImageBitmap(((MainActivity) getActivity()).createQrCode(stringForQrCode));
labelSticker.setText(qrDataList[1]);
serialSticker.setText(qrDataList[2]);
qrcodefinal.post(new Runnable() {
#Override
public void run() {
qrcodefinal.setImageBitmap(MainActivity.getBitmapFromView(qrStickerLayout));
}
});

combining two bitmaps into one bitmap throws an OutOfMemoryException

I am using the code below to combine two bitmaps into one bitmap:
real = (RelativeLayout)findViewById(R.id.real);
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
loadBitmapFromView(real).compress(Bitmap.CompressFormat.PNG, 100, bs);
byte[] byteArray = bs.toByteArray();
i.putExtra("bittext", byteArray);
setResult(RESULT_OK,i);
finish();
}
});
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<RelativeLayout
android:id="#+id/real"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="25dp"
android:layout_above="#+id/text"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"enter code here
android:id="#+id/imagetext"
android:scaleType="matrix"/>
</RelativeLayout>
<Button android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/text"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"/>`enter code here`
<Button
android:id="#+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/text"
android:layout_margin="10dp"
android:background="#drawable/apply" />
</RelativeLayout>
Error occurred when the #+id/apply button is clicked, it can not go to the previous activity along with the bitmap. this problem was occurred only one device (Videocon mobile only). What is the issue, and how do I fix it?

Categories

Resources