Create Bitmap From View in Android (with CardView) - android

In my app, I need to work with GoogleMap and on Markers, I need to put the next custom view
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="true"
android:layout_margin="1dp"
android:elevation="10dp"
app:cardCornerRadius="18dp">
<app.into.additional.DownloadImageView
android:id="#+id/offer_image_1"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="1dp" />
</android.support.v7.widget.CardView>
DownloadImageView is a custom class that uses the Picasso framework to download some images.
The problem is the next one: if I add the above layout in a fragment, the ImageView is rounded as I need. When I try to create a Bitmap using the above layout, the Image loaded inside ImageView doesn't keep the round property, and it's shows square.
This is the code I'm using to create the Bitmap :
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
marker.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
marker.draw(canvas);

Related

Android: Displaying player icon on map based on coords

I have an image of map (fixed size) to be displayed as background and I want to display a player icon over it. I am getting player's current coords as android.graphic.Point.
I tried RelativeLayout with setting up margins, which kinda worked, but when the display is rotated, the icon is obviously moved elsewhere. Also I think this absolute positioning can't work on different screen size...
Any suggestions how to do this as intended?
This is my XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/mapLayout"
tools:context="cz.alois_seckar.vseadventrura.MapActivity">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/playerIcon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/player" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mapImage"
android:src="#drawable/spacemap"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back_button"
android:id="#+id/mapBackButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="goBack"/>
</RelativeLayout>
And this is how I am (re)placing player icon
RelativeLayout mapLayout = (RelativeLayout) findViewById(R.id.mapLayout);
mapLayout.removeView(playerIcon);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(40, 40);
Point playerCoords = game.getWorld().getCurrentSpace().getPosition();
params.leftMargin = playerCoords.x;
params.topMargin = playerCoords.y;
mapLayout.addView(playerIcon, params);
I solved the problem like this:
//Get bitmap to draw
Bitmap playerPic = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(game.getPlayer(), "drawable", getPackageName()));
//Create a new image bitmap and attach a brand new canvas to it
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inMutable = true;
Bitmap mapPic = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(game.getMap(), "drawable", getPackageName()), opts);
Canvas tempCanvas = new Canvas(mapPic);
//Draw the image bitmap into the cavas
Point playerCoords = game.getWorld().getCurrentSpace().getPosition();
tempCanvas.drawBitmap(playerPic, playerCoords.x * mapPic.getWidth() / 10, playerCoords.y * mapPic.getHeight() / 10, null);
//Attach the canvas to the ImageView
ImageView mapImage = (ImageView) findViewById(R.id.mapImage);
mapImage.setImageDrawable(new BitmapDrawable(getResources(), mapPic));
only problem would be size of playerPic - could be too big or do small based on actual DPI... :/

how to create bitmap of scollview of images in android?

I’ve made a demo for film-strip in ,android. I am binding Images to my LinearLayout runtime, Now i want to save whole, Scrollable View to a bitmap, I have tried a lot but all gives solution to only save current visible screen to bitmap,can anyone please help me how to save whole scrollable View(Which is not visible to screen) in a bitmap, my code is as below:
xml
<HorizontalScrollView
android:id="#+id/horizontal_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp"
android:scrollbars="none" >
<ScrollView
android:id="#+id/scroler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/mygallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</HorizontalScrollView>
java
View insertPhoto(int i) {
LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setLayoutParams(new LayoutParams(252, 252));
layout.setGravity(Gravity.FILL);
final ImageView imageView = new ImageView(getApplicationContext());
imageView1 = new ImageView(getApplicationContext());
imageView.setLayoutParams(new LayoutParams(250, 250));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageLoader.displayImage("file://" + dataT.get(i).sdcardPath,
imageView, new SimpleImageLoadingListener() {
public void onLoadingStarted(String imageUri, View view) {
imageView.setImageResource(R.drawable.no_media);
super.onLoadingStarted(imageUri, view);
}
});
layout.addView(imageView);
return layout;
}
using this I’ve made film-strip in vertical orientation. And now I want bitmap of this film-strip for save it.
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = null;
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
saveImageToInternalStorage(b);
return b;
}

ImageView inside of a ScrollView

I am working on a android project. I n that I have to deal with ScrollViews to scroll image. But when I am using ImageView inside ScrollView image given to me is not fit in UI layout. So I am thinking I convert a ScrollView to ImageView. Is there any procedure in android.
Plz help me.
I found a nice solution, in your ImageView parameters, just add -
android:adjustViewBounds="true"
Try this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher"
android:contentDescription="#string/describe_something_here"/>
</RelativeLayout>
</ScrollView>
Hope this helps.
If you want to fit your imageview inside your emulator screen then why to use scrollview.
You can scale your imageview inside your emulator screen like this
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
matrix.postRotate(45);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
// add ImageView to the Layout
linLayout.addView(imageView,
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
)
);
// set LinearLayout as ContentView
setContentView(linLayout);
Hope it helps
hello I think you can solve your problem like this
<anyLayout>
<ScrollView>
<RelativeLayout>
<ImageView/>
</RelativeLayout>
</ScrollView>
</anyLayout>
scroll view work within a layout.
<ScrollView>
<RelativeLayout>
<ImageView/>
</RelativeLayout>
</ScrollView>
This will work for you
I find a very easy solution, all you need to do is add
android:src="#drawable/hayhouse"
to your imageview in layout xml file.

Creating a Bitmap of a View

I try to create a View programaticaly by inflating an layout.
View marker = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.overlay_view, null);
Now i want to convert this View to a Bitmap:
public static Bitmap createDrawableFromView(Context context, View view){
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
Bitmap cache = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(cache);
view.draw(canvas);
return cache;
}
I also tried measure(1, 1) and layout(0 ,0 ,0 ,0) without any changes.
The Bitmap looks like the filled layout.
Now the Problem: If i put something into the View thats makes the View bigger than the Displaysize in width or height (example: a TextView with a long text), the Bitmap is also bigger.
The Views XML width and height is WRAP_CONTENT but it didnt works.
I never put this View somewhere to display it (becouse i only need the Bitmap).
Question: How can i WRAP the View to a maximum of width = displayWidth?
I tried view.setLayoutParams(new LayoutParam(100, 100)) but it was also bigger than 100?! I really dont understand whats happened, when i inflate the layout and set the content of the View.
The view.getMeasuredWidth and Height is always bigger as my display?! (if i put something bigger in it)
Here is an Example of the Layout:
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/somedrawable"
android:padding="10dp"
android:clickable="true">
<TextView android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"/>
<TextView android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/name"
android:text="Description"
android:textSize="20dp"
android:textColor="#ffffff"/>
</RelativeLayout>
I found a solution:
first step: add new LayoutParams to the view. This is important, becouse without you will get a NullPointer.
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Second:
view.measure(MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels), MeasureSpec.AT_MOST),
MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST));
for view.measure() i used MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels, MeasureSpec.AT_MOST));. First parameter is the display width or heigt and the second is the mode AT_MOST, this is needed to "wrap" the View.
Works great! (for me :p )

Android: how to programmatically set width of ImageView inside TableRow

How do you programmatically set the width of an ImageView inside a TableRow? I do NOT want the cell width to change, only the width of the image inside the cell.
Layout: (The ellipsis are there to reduce the code)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TableRow android:gravity="center_vertical">
<Button>...</Button>
<ImageView
android:id="#+id/fruit"
android:layout_width="120dp"
android:layout_height="44dp"
android:src="#drawable/apple"
android:scaleType="matrix"
></ImageView>
<Button>...</Button>
</TableRow>
<TableRow>...</TableRow>
</TableLayout>
Java:
ImageView iv = (ImageView) findViewById(R.id.fruit);
LayoutParams frame = iv.getLayoutParams();
frame.width = 100;
iv.setLayoutParams(frame);
EDIT: i want the image to be cropped inside the bounds i set and have the table cell be the original width. Below is the code example that worked for me thanks to parag's suggestion:
Bitmap bmp = BitmapFactory.decodeResource(getResources() , R.drawable.apple);
int width = 50;
int height = 44;
Bitmap resizedbitmap = Bitmap.createBitmap(bmp, iv.getScrollX(), iv.getScrollY(), width, height);
iv.setImageBitmap(resizedbitmap);
Another way that u resized the bitmap
ImageView img=(ImageView)findViewById(R.id.ImageView01);
Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.sc01);
int width=200;
int height=200;
Bitmap resizedbitmap=Bitmap.createScaledBitmap(bmp, width, height, true);
img.setImageBitmap(resizedbitmap);
Do you want to scale the image? Or crop it?
To scale, try ImageView.setImageMatrix and Matrix.preScale.
If you don't want to figure out the Matrices, you could try Rects (bounding boxes). Get the original rect with ImageView.getDrawingRect and then define whatever Rect you want and use Matrix.setRectToRect to create your image matrix.
Otherwise, have you tried ImageView.setMaxWidth?

Categories

Resources