How to make Wallpaper permanently fit to screen and in center? - java

I am trying to make the wallpaper fit to screen. I tried many solutions on this site but none worked for me except this one, but the image doesn't cover the screen. It kind of shrinks and it can appear anywhere on the screen, mostly in the center. The image size changes again on next reboot. E-g it appears in the middle and is much smaller than the screen size but when I reboot my device, the image size exceeds the bounds of the screen
How can I make it fit to the screen?
If I can't, how can I make it appear in the center permanently?
Does it depend on the dimensions (width, height) of the image too?
Below is my code:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
imageIDs[imageIDforWallPaper]);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
WallpaperManager wm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
try {
wm.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
wm.suggestDesiredDimensions(w, h);

What you are looking for is similar to a splash screen.
The image shrinks because it has fixed width and height, and most probably they don't match with your device's dimensions. Every image will have a different width/height ratio, it isn't therefore a good idea trying to perfectly fit an image to one particular screen, as you may be able to do so (by modifying the image dimensions), but then you wouldn't be able to obtain the same result on every device: Devices and Displays
Create a 9-patch image which adapts to every screen: 9-patch image introduction
Yes it depends on width and height, but these values vary according to screen size and density. When you design an image you need to use dip (density-independent pixels) and create one image for each category of screen densities: Design for multiple screens
read also here: Android splash screen image sizes to fit all devices

Related

Image Autoscale to fit width of a container

I am trying to create a google+ like app were a post contains an image. I use a box layout as the container for the image, but the image does not take up the width of the parent and auto scale the height to preserve the pixel aspect ratio on android as in google plus. I can achieve this on the browser using the css rule by setting the max-width of the image to 100%.
To better understand this, see the code below
private void loadImageArea(){
BundleContext context=getBundleContext();
UIBuilder builder=context.getUIBuilder();
Container boxHeader=(Container) builder.findByName("contentWrap", getForm());
Container imageWrap=(Container) builder.findByName("imageWrap", getForm());
Label imageLabel=new Label();
imageLabel.setUIID("ImageLabel");
Image icon=getImage();
ImageIO io;
URLImage img;
Image dst=null;
int containerWidth=Display.getInstance().getDisplayWidth()- boxHeader.getStyle().getPadding(Component.LEFT)-boxHeader.getStyle().getPadding(Component.RIGHT);
int width=icon.getWidth();
if(width>containerWidth){
Log.p("Container width: "+containerWidth);
width=containerWidth;
dst=icon.scaledWidth(width);
}
imageLabel.setIcon(dst);
imageWrap.removeAll();
imageWrap.addComponent(imageLabel);
}
1)On the simulator it works after I calculate and set the scaled width of the image as the parent container returns a width of zero. Also Scaling is very poor and pixelated. I am trying to implement some filter algorithms on top of Pisces for better results, not sure of performance and memory, but would try
2)On Android ,Why is the image not using the set width and and scaling accordingly. As you can see from the output, it is centralized and down scaled.
Kind Regards!
You can set the image as bgImage and select the option background behavior as scale to fit which will preserve aspect ratio while showing the whole image in the available space of the component.
You can also just get a scaled version of the image using the appropriate methods of the Image class.

Splash screen which fills the whole samrtphone screen, but not crop

I want to have a splash screen, something like a full picture, which doesn't crop in heigth or width on different smartphone screens.
Now I achieved a splash screen with android:scaleType="fitXY", but now the image is cropped on top or bottom or if the devices screen size changes to another aspect ratio it is cropped on the left and right.
What do I have to do? I've already read the android developer article Supporting Multiple Screens, but I don't get it how to achieve this.
A simple picture in the middle of the screen is just simple to get, but a picture which fills the screen is hard to get. Can you help me pls?
you should use center_crop per this purpose. From the doc
Scale the image uniformly (maintain the image's aspect ratio) so that
both dimensions (width and height) of the image will be equal to or
larger than the corresponding dimension of the view (minus padding).
There is no way to create one single asset and expect it to do not be cropper and to do not create black areas when the application is deployed in different screen sizes.
The android platform is designed to work dynamically with multiple screen sizes that any manufacture can change at any time, including new resolutions that you haven't thought about it yet.
Android can specify minimums for screen hight/width categories in which your resources will fall, but those are generics.
In order to use them, you will have to specify qualifiers in your drawables and create a different splash screen for every qualifier, as for example if you use drawable-w420dp, all the resources there will be used when the screen has a minimum width of 420dp (notice that are not pixels)
So you have two options:
You can use one single splash image and design margins of that image flexible enough in order to cope with the image being cropped in certain cases. You can play with different scaleTypes in your ImageView and take as a reference this website http://etcodehome.blogspot.co.uk/2011/05/android-imageview-scaletype-samples.html even though as commented before, "center-crop" will be your best shot.
You can programatically use a specific image for a specific resolution.
2.1 Put in the assets directory, all the splash images that you want for all the specific resolutions or aspect ratios that you want to use
2.1 Get the screen size of the device with Get screen dimensions in pixels
2.2 Now you can load from the assets the image that you want dynamically
Use the below code
android:layout_width="match_parent"
android:layout_height="match_parent"
which will fill the entire screen.
Try Using Width and Height of image to "match_parent"

Fullscreen Rotating Background Image

Developing an app for Android and I'm required to have a fullscreen image rotate (spin around infinitely, pivoted at the centre of the screen) in the background.
I have tried rotating the ImageView using RotateAnimation which works well, except the image is cropped to the size of the parent view.
I have also tried expanding the parent view to a set size (750dp) but it doesn't work well across all the different screen sizes.
Does anyone know of an easy way to implement this, which would involve scaling to uniformly fit the screen size?
Cheers,
Dylan
Screenshot of the image:

How to resize a non-square image to square thumbnail (by adding white space)?

I'd like to create a square thumbnail of an image using Java. I've already managed to resize images through a couple of ways. However I'd like to create a real square image, also from a non-square image.
Example: the source has a size of 200x400 (widht/height)
the target size is 100x100
The algorithm would then need to resize the image to 50x100 and add 25x100 pixels of whitespace each on the left and on the right.
Can anyone help me with this?
Just create a 100x100 background; add the scaled image to it. Use Math.max(width, height) to determine the scale factor. Then, plot the scaled image over the background, use calculations (offset x, offset y) to put it in the proper position.

android camera stretched in landscape mode

The app I'm writing requires camera functionality.
So to learn about how to operate the camera, I followed this script:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html
I have put the activity in my manifest, set the screen orientation for it on landscape mode.
The problem I'm having is, when the camera is held sideways (so I hold my Galaxy Tab P1000 in landscape position) the view is stretched out.
To be more specific about my script, I used an exact copy of code that Google made. It can be found in the android-sdk\samples\android-8\ApiDemos\src\com\example\android\apis\graphics\
The file itself is called CameraPreview.
I really have no clue why the screen looks so stretched. Of course, the format is weird and not square, but still, when using the default camera app installed on the device, it doesn't deform at all. This camera deforms the image when I hold it sideways and move the camera even a little.
What I did was: I held my galaxy tab to take a picture of an object (laptop in this case) then took a picture with my phone of my Galaxy. On the Galaxy I have the camera screen open in the app i'm making. This counts for both images. One I hold sideways and one I hold in portrait view. The pics are a bit unclear but you can see that in the landscape picture, the camera has become massively wide.
I faced the same problem yesterday. After a researching "Camera" sources I found a reason for camera preview being stretched.
The reason is: SurfaceView aspect ratio (width/height) MUST be same as Camera.Size aspect ratio used in preview parameters. And if aspect ratio is not the same, you've got stretched image.
So, the fastest workaround is to set SurfaceView to size like 320px x 240px - smallest supported size from Parameters.getSupportedPreviewSizes().
Also, you can look at Camera standard application sources, it uses the custom layout for controlling the SurfaceView size (see PreviewFrameLayout.java, onMeasure() function).
Use
git clone https://android.googlesource.com/platform/packages/apps/Camera.git
to get Camera sources.
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
camera = Camera.open();
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(holder);
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPictureSizes();
parameters.setPictureSize(sizes.get(0).width, sizes.get(0).height); // mac dinh solution 0
parameters.set("orientation","portrait");
//parameters.setPreviewSize(viewWidth, viewHeight);
List<Size> size = parameters.getSupportedPreviewSizes();
parameters.setPreviewSize(size.get(0).width, size.get(0).height);
camera.setParameters(parameters);
camera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
You need only getSupportedPreviewSizes() and save it to a List:
List<Size> size = parameters.getSupportedPreviewSizes();
parameters.setPreviewSize(size.get(0).width, size.get(0).height);
camera.setParameters(parameters);
camera.startPreview();
I hope this helps you.

Categories

Resources