Trouble centering a bitmap on an ImageView - android

I have an ImageView (image_view) in an xml and I have the layout gravity set to center and the scale set to center_inside....but when I pass my compressed bmp to the image_view, it puts it on the middle-left of the screen (in portrait mode)...any ideas on a workaround?
here is me setting it to the image view:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap565);
I also compress it in an output stream:
try {
FileOutputStream fos = new FileOutputStream(filepath);
bitmap565.compress(CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Thanks all!
<ImageButton android:id="#+id/ImageButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#drawable/results" android:layout_gravity="top|center"></ImageButton><ImageView
android:screenOrientation="portrait"
android:layout_width="fill_parent"
android:id="#+id/image_view"
android:layout_gravity="center" android:layout_weight="1.0" android:layout_height="wrap_content" android:scaleType="centerInside"/>
<Button
android:screenOrientation="portrait"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" android:layout_width="fill_parent" android:textStyle="bold" android:id="#+id/detect_face" android:text="Detect"/>

you can solve your problem just using FrameLayout.

Your ImageView's layout_width is set to fill_parent, so layout_gravity won't affect its horizontal position within its parent. Try using gravity instead of layout_gravity, or wrap_content instead of fill_parent.

Related

How to take screenshot in ConstraintLayout?

I have recently started using ConstraintLayout and am loving it. But have come across a problem to be solved. Need to take a screenshot of a part view of the layout and that is easy in relative or linear layout as we make a layout around the view needed screenshot which is not the case in ConstraintLayout.
This is my view :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<View
android:id="#+id/sharable_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/tv_streak_best"
app:layout_constraintLeft_toLeftOf="#id/total_distance_steps_left_guideline"
app:layout_constraintRight_toRightOf="#id/total_distance_steps_right_guideline"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/streak_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="0dp"
android:layout_height="0dp"
android:max="360"
android:progress="0"
android:progressDrawable="#drawable/progress_gradient_bg"
android:secondaryProgress="360"
app:layout_constraintBottom_toBottomOf="#id/streak_bottom_guideline"
app:layout_constraintDimensionRatio="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.sharesmile.share.views.LBTextView
android:id="#+id/tv_streak_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textColor="#color/clr_5d"
android:textSize="58sp"
app:layout_constraintBottom_toBottomOf="#id/streak_progress"
app:layout_constraintLeft_toLeftOf="#id/streak_progress"
app:layout_constraintRight_toRightOf="#id/streak_progress"
app:layout_constraintTop_toTopOf="#id/streak_progress"
app:layout_constraintVertical_bias="0.35" />
<com.sharesmile.share.views.LBITextView
android:id="#+id/tv_streak_count_days"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/day_streak"
android:textColor="#color/clr_5d"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="#id/streak_progress"
app:layout_constraintRight_toRightOf="#id/streak_progress"
app:layout_constraintTop_toBottomOf="#+id/tv_streak_count" />
<LinearLayout
android:id="#+id/streak_goal_icons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="#id/tv_streak_count_days"
app:layout_constraintRight_toRightOf="#id/tv_streak_count_days"
app:layout_constraintTop_toBottomOf="#id/tv_streak_count_days">
</LinearLayout>
... more code here
</android.support.constraint.ConstraintLayout>
Now I wish to take screen shot of the progressbar and few elements below it. I tried to create a view around it and take screenshot of that but it doesn't work. What can be done to take the screenshot?
You can use
tools:visibility="gone"
to hide the view as below
<View
android:id="#+id/sharable_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="#+id/tv_streak_best"
app:layout_constraintLeft_toLeftOf="#id/total_distance_steps_left_guideline"
app:layout_constraintRight_toRightOf="#id/total_distance_steps_right_guideline"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="gone" />
add below as attribute at root view
xmlns:tools="http://schemas.android.com/tools"
Do not need to hide/show your view
private void captureScreen(View v) {
v.setDrawingCacheEnabled(true);
Bitmap bitmap = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File file = new File(extr, "fff" + ".jpg");
FileOutputStream f = null;
try {
f = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, f);
f.flush();
f.close();
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Screen", "screen");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
pass your view to above function
captureScreen(view you want to take a screen shot);

setImageBitmap() modifies the predifined layout

I prepared a layout with 2 images of same size in a vertical LinearLayout so both appear with exact same size in AS.
The layout is as follow:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/partner_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/fake_background" />
<LinearLayout
android:id="#+id/partner_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="0dp">
<ImageView
android:id="#+id/imageGuide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/logo_Site"
android:scaleType="centerInside"
android:src="#drawable/my_logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="0dp"
>
<ImageView
android:id="#+id/partner_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/logo_Site"
android:scaleType="centerInside"
android:src="#drawable/my_logo" />
</RelativeLayout>
</LinearLayout>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:background="#F2F2F2"/>
<ListView android:id="#+id/right_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#android:color/white"
android:dividerHeight="1dp"
android:background="#F2F2F2"/>
</android.support.v4.widget.DrawerLayout>
The RelativeLayouts are here because I will have some other objects in.
If I execute it on my device, everything is ok, I get the exact same rendering.
But now, I want to change the bottom logo with one downloaded from my server.
I download the exact same image and then, I set it like that:
ImageView partnerLogo = (ImageView) findViewById(R.id.partner_logo);
file = new File(getApplicationContext().getFilesDir(), "/partner/partnerLogo.png");
uri = Uri.fromFile(file);
bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap != null) {
partnerLogo.setImageBitmap(bitmap);
}
When I look at bitmap in the debugger, the image is 512x512 as expected.
But the result is that one:
Could you tell me why the layout is modified?
Edit
I made some tests to try to set both images programmatically and I discovered that using:
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap != null) {
partnerLogo.setImageBitmap(bitmap);
}
set an image smaller than using:
partnerLogo.setImageDrawable(getResources().getDrawable(R.drawable.my_logo));
But both images are exactly the same PNG images!?!
So I think the problem comes from setImageBimap() and not from the layout.
EDIT 2
According to many post about similar problems, it seems that Android needs a drawable to be able to scale it as needed.
That would confirm my experiment given in first EDIT.
So I tried that:
ImageView partnerLogo = (ImageView) findViewById(R.id.partner_logo);
file = new File(getApplicationContext().getFilesDir(), "/partner/partnerLogo.png");
uri = Uri.fromFile(file);
BitmapDrawable drawable = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), uri);
if (bitmap != null) {
drawable = new BitmapDrawable(getResources(), bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap != null) {
partnerLogo.setImageDrawable(drawable);
}
But that doesn't work better !?!
On your layout on the LinearLayout add android:weightSum="2"
On your both RelativeLayout containing a ImageView change android:layout_height="wrap_content" to android:layout_height="0dp"
But those RelativeLayout are superfluous you can remove it and put on the ImageView's
android:layout_height="0dp"
android:layout_weight="1"
This ensures that the vertical space will be divided between the two images
Just modified relevant code. Add it and check.
Modification Like:
Adding weightSum to LinearLayout
Giving equal weight to the LinearLayout's immediate children
Modified your LinearLayout (partner_layout ).
This is just modified piece of your XML file.
<LinearLayout
android:id="#+id/partner_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:padding="0dp">
<ImageView
android:id="#+id/imageGuide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/logo_Site"
android:scaleType="centerInside"
android:src="#drawable/my_logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partner_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:padding="0dp">
<ImageView
android:id="#+id/partner_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:contentDescription="#string/logo_Site"
android:scaleType="centerInside"
android:src="#drawable/my_logo" />
</RelativeLayout>
</LinearLayout
Our first aim should be how to resolve the issue.It seems that it could be handled by your xml layout.
So in my suggestion if you are using relative layout try to fix width and height of your imageView.
Other wise try some other layout like tableLayout, frameLayout or gridLyout and try to change width and height of your imageview with various available option(match_parents/wrap_content etc or also try with fix size).and also add
android:scaleType="fitXY"
Hope this will resolve your issue.
When You set Images from Drawable then Android system Automatically resize that image accrding available width and height but when you set Any image using setImageBitmap() method it dose not resize and actual image shown.
Although the system performs scaling and resizing to make your
application work on different screens, you should make the effort to
optimize your application for different screen sizes and densities. In
doing so, you maximize the user experience for all devices and your
users believe that your application was actually designed for their
devices—rather than simply stretched to fit the screen on their
devices.
Find your solution and more details regarding Images,Bitmap scaling and resizing ,drawables in below links :
How to stop Android form automatically scaling drawables?
Supporting Multiple Screens
So the only solution I found is to scale the image by hand, based on the layout built with fake images.
And the code is based on the one found quiet a long time ago somewhere in SO:
final ImageView myLogo = (ImageView) findViewById(R.id.image_logo);
bitmap = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.my_logo_512x512);
final ImageView partnerLogo = (ImageView) findViewById(R.id.partner_logo);
file = new File(getApplicationContext().getFilesDir(), "/partner/partnerLogo.png");
uri = Uri.fromFile(file);
try {
bitmap2 = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
if (bitmap != null) {
ViewTreeObserver vto = myLogo.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
myLogo.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = myLogo.getMeasuredHeight();
finalWidth = myLogo.getMeasuredWidth();
pictureWidth = bitmap.getWidth();
pictureHeight = bitmap.getHeight();
float ratioImage = (float)pictureWidth / (float)pictureHeight;
float ratioView = (float)finalWidth / (float)finalHeight;
if (ratioView > ratioImage) { // finalHeight is the limit
finalWidth = (int)((float)pictureWidth * ((float) finalHeight / (float) pictureHeight));
} else { // finalWidth is the limit
finalHeight = (int)((float)pictureHeight * ((float) finalWidth / (float) pictureWidth));
}
bitmap = Bitmap.createScaledBitmap(bitmap, finalWidth, finalHeight, false);
myLogo.setImageBitmap(bitmap);
if (bitmap2 != null) {
bitmap2 = Bitmap.createScaledBitmap(bitmap2, finalWidth, finalHeight, false);
partnerLogo.setImageBitmap(bitmap2);
}
return true;
}
});
}
I saw that a comic downvoted my question, I hope that next time he will motivate it.

Android - Image coming from web service has different size on different devices

I know there are lots of question about this topic. But still I need help. I want to show my ImageView has equal width and height in my TableLayout. But on different devices I couldn't be able to do this.
Here is I get the image using URL in AsyncTask, and set the ImageView on onPostExecute method.
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
// set the imageview
bmImage.setImageBitmap(result);
}
Here is my XML file (news.xml) where my ImageView located
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingEnd="10dp"
android:layout_marginRight="10dp"
android:layout_marginEnd="10dp"
android:paddingBottom="10dp"
android:id="#+id/mylayout"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:clickable="true"
android:background="#drawable/border"/>
<TextView
android:layout_width="match_parent"
android:layout_height="75dp"
android:maxLines="4"
android:id="#+id/text"
android:layout_below="#+id/image"
android:layout_alignEnd="#+id/image"
android:layout_alignRight="#+id/image"
android:clickable="true"
android:textSize="18sp"
android:background="#e3e3e3"
android:paddingTop="10dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
I thought that I can create a TableLayout, and locate the View (above) inside a TableRow like, side by side 2 view.(view is above)
To do this I get the screen width and subtract the paddings and divide by 2 and give that width to the ImageView to be able to get same size for all image views.
Here is my code,
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
tableLayout = (TableLayout) view.findViewById(R.id.tableLayout);
Log.e("screenwidth",":"+screenWidth);
// here is my paddings converted to pixel and will be subtracted from screen width
int pixValue = (int) (20 * Resources.getSystem().getDisplayMetrics().density);
int imageWidthPix = ( screenWidth - pixValue ) / 2;
Log.e("imageWidthPix ",":"+imageWidthPix );
for (int i = 0; i < categoryNews.get(myString).size(); i++) {
if (i % 2 == 0) {
tr = new TableRow(getActivity());
tableLayout.addView(tr);
}
//here is where my ImageView layout (news.xml)
View oneNews = inflater.inflate(R.layout.news, null);
ImageView imageView = (ImageView) oneNews.findViewById(R.id.image);
TextView textView = (TextView) oneNews.findViewById(R.id.text);
//here I set the width as I want
imageView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidthPix, RelativeLayout.LayoutParams.WRAP_CONTENT));
String imagePath = "http://my.url.com/" + categoryNews.get(myString).get(i).getImagePath();
new DownloadImageTask(imageView,getActivity().getApplicationContext())
.execute(imagePath);
I got the perfect result on my phone. But on the different device which has different screen size, ImageView has spaces on top and bottom. I tried to convert nine patch chunk and convert that into again bitmap but there were no difference. If I wouldn't get the images from web service, I would put other resolution images into mipmap-hpdi, mipmap-mdpi etc folders and there would be no error. But I am getting images dynamically so how can achieve that ImageView has half of screen width?
Thanks in advance.
Here I have edited your xml
Try Linear layout with weights,change the weight according to your need, change the layout_weight to make your image view and textview smaller or larger.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:orientation="vertical"
android:id="#+id/mylayout"
android:weightSum="10">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="3"
android:id="#+id/image"
android:src="#drawable/sd"
android:clickable="true"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:maxLines="4"
android:id="#+id/text"
android:clickable="true"
android:textSize="18sp"
android:background="#e3e3e3"
android:paddingTop="10dp"
android:gravity="center_vertical"
/>
</LinearLayout>

Need to take screenshot without Imagebuttons

I am trying to take screenshot of my app screen programatically. The Layout file has some ImageButtons and a Textview field. When the capture screen happens, the resulted image contains the buttons too. Is there a way to take screenshot without the imagebuttons?
Below are the code snippet and layout files
activity_main.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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xyz.abc.def.MainActivity">
<include
layout="#layout/buttons_layout"
android:id="#+id/includedLayout"
android:visibility="visible"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:id="#+id/text1"
android:gravity="center"
android:textSize="32dp"
android:shadowColor="#edeff2"
android:textStyle="bold"
android:shadowDx="0.0"
android:shadowDy="0.0"
android:shadowRadius="25"
/>
</RelativeLayout>
button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right"
>
<ImageButton
android:id="#+id/refreshButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/refresh"
/>
<ImageButton
android:id="#+id/shareButton"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_margin="10dp"
android:background="#drawable/share"
/>
</LinearLayout>
</LinearLayout>
Code to take screenshot
private void captureScreen() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
Any help will be appreciated. Thanks :)
If it is just the text view you want as I can see it is full screen.
Try using
View view = findViewById(R.id.text1); // instead of R.id.activity_main
view.setDrawingCacheEnabled(true);
Basically point is keep those views in separate container whose screen shot you want and then get the drawable of that particular container.
You can make the image button invisible just before taking the screenshot and then make it visible.
private void captureScreen() {
//make unwanted views invisible
findViewById(R.id.refreshButton).setVisibility(View.INVISIBLE);
findViewById(R.id.shareButton).setVisibility(View.INVISIBLE);
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
String filePath = Environment.getExternalStorageDirectory() + File.separator +now+".jpg";
View view = findViewById(R.id.activity_main);
view.setDrawingCacheEnabled(true);
mBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
sendScreen(filePath);
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
//make those views visible again
findViewById(R.id.refreshButton).setVisibility(View.VISIBLE);
findViewById(R.id.shareButton).setVisibility(View.VISIBLE);
}

Image is not fullscreen

I want to display an image to be fullscreen :
public class PhotoPleinEcranActivity extends Activity {
private Bundle data;
private ImageView img;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.photo_plein_ecran);
img = (ImageView)findViewById(R.id.zoom);
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = true; //Disable Dithering mode
bfOptions.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage = new byte[32 * 1024];
data = getIntent().getExtras();
FileInputStream fs = null;
Bitmap bm;
try {
fs = new FileInputStream(new File(data.getString("path")));
if(fs != null) {
bm = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
img.setImageBitmap(bm);
}
} catch (IOException e) {
} finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
}
}
}
}
}
The layout :
<?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="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:orientation="horizontal">
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
/>
</LinearLayout>
Manifest file :
<activity
android:name="com.ambre.impots.PhotoPleinEcranActivity"
android:label="#string/galleryPhotos"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
At runtime there is a grey field at the bottom of the image ( I surrounded the grey field by a yellow color ) :
So how to make that grey field disappear ?
This will solve your problem.
Add below line in ImageView.
android:scaleType="fitXY"
add android:scaleType="fitXY" to your ImageView, fitXY, will fill width and height independently to match the destination
add property scaleType to fitXY like this
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dip"
android:layout_marginBottom="0dip"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:scaleType="fitXY" />
Change your image view like this
<ImageView
android:id="#+id/zoom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dip"
android:layout_marginTop="0dip"
android:adjustViewBounds="true"
android:paddingBottom="0dip"
android:paddingTop="0dip"
android:scaleType="fitXY" />

Categories

Resources