Bitmap size changes - android

I have an ImageView containing an image. This image is rotated by button clicks, but sometimes it gets smaller or gets its original size. I have no idea what causes this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
//tablelayout here
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="200dp"
android:maxHeight="200dp"
/>
</LinearLayout>
</RelativeLayout>
Inital settings:
iv = (ImageView)findViewById(R.id.imageView1);
int id = getResources().getIdentifier("landolt", "drawable", getPackageName());
iv.setImageResource(id);
myImg = BitmapFactory.decodeResource(getResources(), R.drawable.landolt);
matrix = new Matrix();
size = 200;
randomize = new Random();
random = randomize.nextInt(8) + 1;
rotate = getAngle(random); //function created by me
matrix.postRotate(rotate);
rotatedBitmap = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true);
iv.setImageBitmap(rotated);
So the image is rotated, no size change in code.
A rotation on button click:
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (rotate == -90)
{
//rotate back to original direction
old_rotate = -rotate;
matrix.postRotate(old_rotate);
//next rotation
random = randomize.nextInt(8) + 1;
rotate = getAngle(random);
matrix.postRotate(rotate);
rotatedBitmap = Bitmap.createBitmap(myImg, 0, 0, myImg.getWidth(), myImg.getHeight(), matrix, true);
iv.setLayoutParams(new LinearLayout.LayoutParams(size, size));
iv.setImageBitmap(rotated);
}
}
});
When I launch the activity, the image appears with its original size, sometimes smaller. When I click on a button the image gets smaller or gets as big as its original size.
What is going on?

I figured out why does the image keep changing size. It simply doesn't have enough place in the imageView when rotated. I found the solution to my problem on this link.

Related

How Do You Rotate An Icon On An ImageButton Without Rotating The Actual Button? [duplicate]

I want to rotate the image inside the button on click of the button. I tried with just ImageView and it works but for accessibility purpose I need to use Button instead of ImageView.
Before click :
Here the down arrow is button background.
After Click :
Click of button displays extra data and background arrow is rotated. How can I animate and rotate button background on click?
Layout code for reference :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<LinearLayout
android:id="#+id/cases_actions_row"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/case_action_item_1"
style="#style/card_action_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:id="#+id/case_action_item_2"
style="#style/card_action_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/cases_expand_button"
style="#style/card_action_button"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#drawable/ic_arrow" />
</RelativeLayout>
The simplest method would be to rotate the entire button (and as Frank N. Stein suggested in the comments, an ImageButton is probably best suited, although there's nothing to stop you from using a different widget).
There are several ways to rotate the button, but a ViewPropertyAnimator is again likely the most straightforward:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float deg = button.getRotation() + 180F;
button.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());
}
});
Edit: By the way, if you want the arrow to reverse its original animation, you could instead try:
float deg = (button.getRotation() == 180F) ? 0F : 180F;
instead of float deg = button.getRotation() + 180F;
You can set bitmap as background in button.
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
First take bitmap from any source and then rotate it and then set it as background in button.
BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);
button.setBackground(bdrawable);

ImageView not fitting sides of screen

Well my app should let me take a picture of my face and display it until i delete it where I can take another one. What is happening when I first open the app is that the pink side lines of the layout backround shows but when I take a picture the pink lines dissapear. This shouldnt happen because the imageview that i am setting the backround bitmap to is match_parent on width and height
The second problem is that the when I set the background imageview back to the background before which is transparent in the center so you can see where the camera is pointing does not happen even though I set it back. The screen should revert back to the transparent center but it stays the screenshot that is taken when takePicture is run. Thanks for any help as i am ripping my hair out right now
background_view = (ImageView) view.findViewById(R.id.backround_view);
background = BitmapFactory.decodeResource(getResources(), R.drawable.camera_backround);
background_view.setImageBitmap(background);
private void takePicture() {
if (picturePresent == false) {
edit_button.setVisibility(View.INVISIBLE);
pictureBitmap = getBitmapFromView();
edit_button.setVisibility(View.VISIBLE);
closeCamera();
stopBackgroundThread();
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), pictureBitmap);
background_view.setBackground(bitmapDrawable);
picturePresent = true;
} else {
}
}
private void deletePicture() {
if (picturePresent == true) {
startBackgroundThread();
openCamera(mTextureView.getWidth(), mTextureView.getHeight());
background_view.setImageBitmap(background);
picturePresent = false;
} else {
}
}
public Bitmap getBitmapFromView() {
Bitmap bitmap = mTextureView.getBitmap();
Bitmap bitmap2 = BitmapFactory. decodeResource(getResources(), R.drawable.camera_backround);
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bitmap, new Matrix(), null);
canvas.drawBitmap(bitmap2, new Matrix(), null);
return bmOverlay;
}
XML
<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"
tools:context="yourfacelivewallpaper.alexcz.yourfacelivewallpaper.CameraFragment"
android:background="#ffbf1ec5">
<!-- TODO: Update blank fragment layout -->
<view
android:layout_width="fill_parent"
android:layout_height="fill_parent"
class="yourfacelivewallpaper.alexcz.yourfacelivewallpaper.AutoFitTextureView"
android:id="#+id/texture"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/backround_view"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:longClickable="false"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"/>
android:scaleType="fitXY"
Add above code in xml file.
If your image is smaller than your ImageView it won't fill the screen. You should add android:scaleType="fitCenter" to your ImageView's layout properties.

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... :/

Android - Rotate image inside the button

I want to rotate the image inside the button on click of the button. I tried with just ImageView and it works but for accessibility purpose I need to use Button instead of ImageView.
Before click :
Here the down arrow is button background.
After Click :
Click of button displays extra data and background arrow is rotated. How can I animate and rotate button background on click?
Layout code for reference :
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
<LinearLayout
android:id="#+id/cases_actions_row"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/case_action_item_1"
style="#style/card_action_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
<Button
android:id="#+id/case_action_item_2"
style="#style/card_action_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/cases_expand_button"
style="#style/card_action_button"
android:layout_height="20dp"
android:layout_width="20dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="#drawable/ic_arrow" />
</RelativeLayout>
The simplest method would be to rotate the entire button (and as Frank N. Stein suggested in the comments, an ImageButton is probably best suited, although there's nothing to stop you from using a different widget).
There are several ways to rotate the button, but a ViewPropertyAnimator is again likely the most straightforward:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
float deg = button.getRotation() + 180F;
button.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());
}
});
Edit: By the way, if you want the arrow to reverse its original animation, you could instead try:
float deg = (button.getRotation() == 180F) ? 0F : 180F;
instead of float deg = button.getRotation() + 180F;
You can set bitmap as background in button.
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmapOrg,width,height,true);
Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap , 0, 0, scaledBitmap .getWidth(), scaledBitmap .getHeight(), matrix, true);
First take bitmap from any source and then rotate it and then set it as background in button.
BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);
button.setBackground(bdrawable);

android frame layout with horizontal scroll bar and ovelapped images

i want to create a Frame layout to place a number of images from database, which need to be a horizontal scrollable list. Image Views are creating dynamically based on database value. Images must be overlapped like the attachment . Here is my xml
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_marginTop="20dp"
>
<FrameLayout
android:id="#+id/frmLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:layout_weight="1"
>
</FrameLayout>
</HorizontalScrollView>
and Here is java
public void getAllImages(){
cursorImages = dbAdapter.fetchImages();
if (cursorImages.moveToFirst()) {
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 2;
do {
String filename = cursorImages.getString(cursorImages
.getColumnIndex("filename"));
try {
InputStream ims = getAssets().open("images/" + filename);
Bitmap bm = BitmapFactory.decodeStream(ims,null,options);
Matrix mat = new Matrix();
mat.postRotate(30);
Bitmap bMapRotate = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), mat, true);
ImageView im = new ImageView (this);
im.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
im.setImageBitmap(bMapRotate);
frmLayout.addView(im,new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
} catch (IOException ex) {
}
} while (cursorImages.moveToNext());
}
cursorImages.close();
}

Categories

Resources