I have a ViewPager with a graphic in between:
mPager.setPageMarginDrawable(R.drawable.margin);
I have set the image to repeat in y direction.
margin:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/image"
android:tileMode="repeat"
android:dither="true" />
The problem is that it does not align to margin space. It seems that its repeated independently of the size of the ViewPager. On one device I mangaged to get the size that it fits into because its a multiple of the screen width in landscape and portraint, but on other devices it wont work.
Is there a solution for my problem?
thanks
okay solved it my own.
I scale the margin drawable programatically so that the display width is a multiple of it.
not beautiful, but working. better solutins are welcome.
Related
So in my App, when the keyboard appears, the whole layout is resized to fit above it, which is exactly what I want except for the background image, I want it to stay the same size.
After some research, I saw a solution was to set the background programmatically (not using an ImageView in the XML):
getWindow().setBackgroundDrawableResource(R.drawable.background);
Which works to some extent, as it dosn't get resized when the keyboard appears, but the image is stretched to fit the screen's ratio. I found another solution to combine using xml resources:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat">
</bitmap>
Using this, the drawable stays the right ratio but isn't scaled down to the size of the screen (tried using the gravity attribute but it didn't do anything).
You don't have control over scaling with a background drawable. What you could do is put an image view over the root layout taking the entire size. Then you can use the scale options of the ImageView to scale it as you would any other image (probably FITX in this case, since you don't want it to scale with a change in Y). Just make the ImageView the first view inside the parent so it takes the bottom of the z order.
I have this scenario. So the image is larger than the container
I want it to scale down or scale up depending on the container size but at the same time put it on the center and crop the remaining (horizontally).
So I did something like this.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/header_long"
android:gravity="center"
android:antialias="true"
android:dither="true"/>
However it looks like this now. The top, bottom, left and right is cropped.
I want it to be something like this.
try this
android:scaleType="centerCrop"
Like ben75 proposes, you can put your bitmap into am ImageView and use
android:scaleType="centerCrop"
If you want to use your image as a background, consider making it a 9-patch image. They define areas that may be stretched if the image is too small. They also define padding. Note that 9-patch images grow as needed but they never shrink, so make them as small as the smallest size you like them to get.
I'm trying to set a background image in my Android application.
I have a JPEG image and I want to fill the screen with the height of the image, maintaining the same aspect ratio.
I tried by creating an XML bitmap file in drawable and setting it as:
android:background="#drawable/background_image"
This is the XML
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/wallpaper1_1920x1200"
android:tileMode="disabled"
android:gravity="center" >
</bitmap>
With the 'center' tag it doesn't stretch the image and it doesn't resize it.
Is there any way to show the full height of the image (and cropping sides) maintaining the original aspect ratio?
You can wrap your bitmap with Scale Drawable (http://developer.android.com/guide/topics/resources/drawable-resource.html#Scale)
And then use that drawable as a background for your layout.
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/wallpaper1_1920x1200"
android:scaleGravity="clip_horizontal"
android:scaleHeight="100%"
android:scaleWidth="100%" />
One solution might be to turn it into a 9 patch png file, allowing you to define where it can and can't stretch. See http://developer.android.com/guide/topics/graphics/2d-graphics.html, the section near the end on 9 patch images.
So I have this canvas on where I paint bitmaps
An example would be a bitmap as a background.. so I will make this full screen on my 533x320 dip Samsung S2
So, when I load this same app on a, lets say, HTC with 480x320 dip.. my background image will now be larger then the screen - how is this normally handled?
I know some would might answer that I can just check the DIP sizes and use that.. which would also work for this background image.. but what about the 10 chess pieces I have where one of them is not out of the screen because it extends the 480dp of the HTC but works great on my 533dp S2?
How is this normally handled?
There are lot of ways to do that. Make sure you always use fill_parent or wrap_Content for your width and height attributes. In case of percentage based width's use LinearLayout for your layouts and use Layout_weight attribute for giving the width or height.
Remember to give width or height as 0dp if you are using layout weight attribute.
The way I did it is to make a drawable for the background /drawable/backrepeat.xml that repeats an image (metalgrid.jpg) in both directions.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/metalgrid"
android:tileMode="repeat"
android:dither="true" />
Then I have a style
<style name="page_background_gen">
<item name="android:background">#drawable/backrepeat</item>
</style>
And then I use that style in my layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout1"
style="#style/page_background_gen"
android:layout_width="fill_parent">
....
</RelativeLayout>
This will fill the background with a repeated image without messign with the scale of the image.
I have a RelativeLayout which currently has a fixed size. Widths, heights, margins, font heights of child views are specified so everything looks just right.
I now want to scale the layout (to fit screen size). The layout should scale as if it was a flat image, so everything gets smaller in proportion (fonts, margins etc.)
I made a simplified example, below. Scaled to 0.5, this would display the text "ONE QUARTER" with margin left 200dip and margin top 120dip.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/RelativeLayout01"
android:layout_width="1600dip"
android:layout_height="960dip"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:text="ONE QUARTER"
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="400dip"
android:layout_marginTop="240dip"
></TextView>
</RelativeLayout>
Of course, I'm not asking anyone to help me hand code an algorithm to scale all these values: just wondering if there's some simple way to achieve this...
Thanks!
If you just want your app to look ok on another device then specifying things in dip and sp should do the trick.
If you actually want to shrink or expand the scale of your app on the same device then you would have to do it manually, perhaps using themes or styles.