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

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);

Related

Fit the size of the ImageView as well as perform rotation on it

My Activity
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap cameraBitmapNull = BitmapFactory.decodeByteArray(data, 0, data.length, options);
int wid = options.outWidth;
int hgt = options.outHeight;
Matrix nm = new Matrix();
Camera.Size cameraSize = camera.getParameters().getPictureSize();
float ratio = relativeLayout.getHeight() * 1f / cameraSize.height;
if (getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) {
nm.postRotate(90);
nm.postTranslate(hgt, 0);
wid = options.outHeight;
hgt = options.outWidth;
ratio = relativeLayout.getWidth() * 1f / cameraSize.height;
} else {
wid = options.outWidth;
hgt = options.outHeight;
ratio = relativeLayout.getHeight() * 1f / cameraSize.height;
}
float ratios = relativeLayout.getHeight() * 1f / cameraSize.height;
float[] f = new float[9];
matrix.getValues(f);
f[0] = f[0] / ratios;
f[4] = f[4] / ratios;
f[5] = f[5] / ratios;
f[2] = f[2] / ratios;
matrix.setValues(f);
Bitmap newBitmap = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
imgfool.setImageBitmap(cameraBitmap);
canvas.drawBitmap(cameraBitmap, nm, null);
imgfool.setVisibility(View.VISIBLE);
//imgfool.setScaleType(ImageView.ScaleType.MATRIX); //required
// nm.postRotate((float)0);
// imgfool.setRotation((float) 90);
// Bitmap rotated = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), nm, true);
//imgfool.setRotation(90);
imgfool.setImageBitmap(cameraBitmap);
cameraSurfaceView.setVisibility(View.GONE);
takeScreenShot(oopss);
My XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="55dp"
android:id="#+id/frame"
>
<RelativeLayout
android:id="#+id/containerImg"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/oopss">
<ImageView
android:id="#+id/imgfool"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:rotation="90"
android:scaleType="fitXY"
/>
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:contentDescription="#string/app_name"
android:scaleType="matrix"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/redirect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_gravity="bottom"
android:layout_marginBottom="10dp"
android:background="#000"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginRight="10dp"
android:layout_weight=".33"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/label"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/glow"
android:text="Back"
android:textColor="#fff"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</FrameLayout>
Here I have a imageview which do rotate to 90 degree but leave some space above and below eventhough i use match_parent and fitXY into my xml file.I searched a lot on this but did not found any appropriate solution of it.So please help me out with this. Actually the imageview here sets the bitmap image taken from surfaceview but When i set it on an imageview it gets rotated automatically.So I thought to rotate the ImageView to get the proper Image to be set on it.

translate matrix is not working in android

I'm using Matrix and Bitmap for imageView,
I want to setup position and Scaling for image as imageView.
(imageView is always showed x = 0, y = 0 )
Scalling is worked , but translate position is not working.
Please help me regarding this.
here is my code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View root = inflater.inflate(R.layout.fragment_entrance, container);
ImageButton setting_button = (ImageButton) root.findViewById(R.id.settings_next);
setting_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), LoginActivity.class);
intent.putExtra("next_page", false);
startActivity(intent);
}
});
final ImageView backGroundImageView = (ImageView) root.findViewById(R.id.fragment_entrance_background_imageview);
mLogoImageView = (ImageView) root.findViewById(R.id.fragment_entrance_logo_imageview);
Matrix mMatrix = new Matrix();
mMatrix.reset();
mMatrix.postTranslate(mCenterX - mImgWidth*0.5f, mCenterY - mImgHeight * 0.5f);
mMatrix.postScale(mScaleX, mScaleY, mCenterX, mCenterY);
mLogoImageView.invalidate();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), mLogoImgId);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, (int) mImgWidth, (int) mImgHeight, mMatrix, true);
mLogoImageView.setImageBitmap(newBitmap);
return root;
}
and here is xml file.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/entrance"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context="com.receptionist.receptionist.Entrance.EntranceFragment">
<ImageView
android:id="#+id/fragment_entrance_background_imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:padding="0dp">
<ImageView
android:id="#+id/fragment_entrance_logo_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"/>
</RelativeLayout>
<ImageView
android:id="#+id/dummy_button"
android:layout_width="111dp"
android:layout_height="94dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="40dp"
android:background="#color/black_overlay"
android:src="#drawable/button_next" />
<ImageButton
android:id="#+id/settings_next"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="right|top"
android:background="#color/black_overlay"
android:src="#drawable/setting_w" />
</FrameLayout>

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 multiple image views as a single file

In my application , one Relative Layout has totally three ImageView()'s . I want to save all the three imageviews as a single .png file to the device storage on a single button click.
My code:
Layout XML :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/make" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:layout_above="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/am0" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Save Meme"
android:textStyle="bold"
android:textColor="#color/wt" />
<ImageView
android:id="#+id/top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="64dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/down"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="74dp"
android:src="#drawable/ic_launcher" />
Activity Code :
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sm);
main1 = (ImageView) findViewById(R.id.imageView1);
top = (ImageView) findViewById(R.id.top);
down = (ImageView) findViewById(R.id.down);
Bundle rec = getIntent().getExtras();
int rec1 = getIntent().getExtras().getInt("ams0");
Bitmap bmp = (Bitmap) rec.getParcelable("bm0");
Bitmap bmp1 = (Bitmap) rec.getParcelable("bm1");
main1.setImageResource(rec1);
top.setImageBitmap(bmp);
down.setImageBitmap(bmp1);
}
How do I save the image views as a file ?
Try using a Сanvas
Bitmap mainBitmap = ((BitmapDrawable)main1.getDrawable()).getBitmap().copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(mainBitmap);
canvas.drawBitmap(bmp.copy(Bitmap.Config.ARGB_8888, true), 1, 1, null);
canvas.drawBitmap(bmp1.copy(Bitmap.Config.ARGB_8888, true), 1, 1, null);
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/DCIM/Camera/myImages.png");
mainBitmap.compress(Bitmap.CompressFormat.PNG, 50, os);
} catch (IOException e) {
e.printStackTrace();
}

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