Surfaceview is not being drawn completely when created initially - android

I am using surfaceview in my android app using following XML code.
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
The surfaceview is supposed to be drawn initially when created and when user touches it. However, when the surfaceview is created initially , it is not drawn completely. There exists a small black strip at the bottom of surfaceview. However on subsequent re-drawing, when user touches the surface, it is completely drawn. Why is this issue occuring ?

That issue happens when the layout initially is not covering the entire screen. That can either be a problem in:
The drawing
The layout
In this case, I would have to say there is something wrong with the layout. It doesn't cover the screen propperly because of height = 0dp
yes I see the weight, but if you are to make a game it is easier to use FrameLayout and put buttons on top of the canvas.
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
This would have been easier to answer with the full xml layout and the rendering code.

Related

How do I properly align ImageViews on top of one another?

I'm currently trying to make a simple tic-tac-toe game, consisting of ImageViews on top of the board (also an ImageView). However, when I run the app and click on a tile, the picture appears out of place, even though it seems I had lined up the ImageViews correctly.
What I did was I set each original tile to a blank square, and then when the user clicks on a tile, it would change to either "X" or "O," depending on whose turn it was.
I'm pretty sure the problem has to do with dpi scaling on different devices, but I'm not quite sure how to fix it. Any help would be greatly appreciated!
I've included an image of my problem below:
Instead of using an image to display the grid, I would suggest to use XML views.
Horizontal line:
<View
android:layout_width="match_parent"
android:layout_height="5dp"
android:background="#android:color/black"/>
Vertical Line:
<View
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#android:color/black"/>
And constraint them between the images.

Android Image positioning to support multiple screen resolutions

I've had the same problem for a long time now, which is basically what the title says. I have drawn some pictures to visualize what I'm trying to achieve.
Picture of man on ground
So on this first pickture, we have a man(player) standing on the GROUND of the Background image. In a 1024x728 Device this player is positioned somewhere around the Screen Height - 120px. I wan't to support multiple screen resolutions, but when I run the App on a bigger screen, this happens of course:
picture of man not on ground
The height of the background is bigger, therefor the ground is futher away from the bottom and -120px is not enough.
(In the app; pictures are drawn with Canvas as bitmaps)
So my question is, how do I achieve to get this man to always have the position on the ground? What's best practise? And can you show me some examples?
Thanks a lot.
There are two aproaches:
1) Using RelativeLayout child of this layout can be specified in relation to other childern
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/name"> <<<< look at this
</RelativeLayout>
2) Using just added percentage support library. You can specify % position on the screen. Read this for it

The background distortion when the screen turn from vertical to horizontal?

In my fragment , I set the background to a Relativelayout.
But when the screen turn from vertical (Portrait) to horizontal (Landscape) , the background image seems like distorted.
It can not set scaleType to Relativelayout.
Does there has any method can make the background auto scaling when the screen turn from vertical to horizontal?
(Does not need to detect the screen size , just let the background image auto scaling and no distortion)
Thanks in advance!
Put an Image-view inside your Relative Layout and use it as a background, like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/myBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/my_bg_image" >
</RelativeLayout>
This way you can put other views on top of your bg and it will scale properly.
If your background image is created for Portrait mode and if it has graphics or texture involved then it is obvious that it will be distorted when seen in Landscape mode.
In this scenario, instead of using one background images,you can use two different background images one for Portrait mode and one for Landscape mode.
Create two different folders of layout
/res/layout/
/res/layout-land/
Put you layout xml in both of these folders with different background images
For Example
1. /res/layout/
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background-port">
<!-- layout code will go here -->
</RelativeLayout>
Second layout will have different background image (created for landscape mode)
2. /res/layout-land/
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background-land">
<!-- layout code will go here -->
</RelativeLayout>
This is not direct answer of your question which is about using scaleType in RelativeLayout but the standard way by which your issue can be resolved
Also if your background image does not contain complex graphics or textures, think about using Nine-patch drawables for background. In this case you won't have to create different folders as mentioned above. Please check This for more information

how to keep aspect ratio with different android devices

So I'm trying to put an image inside a scrollview and have the image stretch to fit different sized screens. It'll stretch horizontally, but vertically it always comes out messed up.
I've read so many pages of people with similar problems, but their solutions never work for me. I've tried every different combination possible using different scaleType,layout_width and layout_height, along with trying changing other things around. The closest I've gotten to getting it to work is shown in the code below, using scaleType="centerCrop". It's the only one that stretches the image vertically in ratio, but it cuts off a big chunk of the image from the top and bottom, it'll scroll up and down the image, but only the middle part shows.
The image is a 480x5500 jpeg, if that matters. Originally before I started messing with all that, the app worked just fine on my phone, but then later I realized the image was crunched when I tried it on a tablet. There's gotta be a way to do this in the xml right? I really don't want to have to do things with the image in the java code part. Also I'm hoping to be able to do this using just one image, I don't want to have use a different image for different screen sizes. Please help, thanks.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/paper" />
</ScrollView>
may be this help you,
make both height and width wrap_content of ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
and no need ScrollView here.
Try using custom ImageView like AspectRatioImageView mentioned here:
https://stackoverflow.com/a/4688335/944070
the next sample is more than what you ask for :
http://www.androidviews.net/2012/11/photoview/
https://github.com/chrisbanes/PhotoView/tree/master/sample
https://play.google.com/store/apps/details?id=uk.co.senab.p
hotoview.sample
it fits the image to the screen , plus it allows to zoom in/out using pinching gestures.

Android: ImageSwitcher inside Scrollview places image at bottom of the screen

So I created a simple little app that contains rather tall images inside an ImageSwitcher and due to the height of the images, I've wrapped the ImageSwitcher inside a ScrollView (so the entire image is eventually visible). The interesting thing is that the Image doesnt start at the top of the ScrollView but rather the bottom. The scrolling all works correctly, but I was just curious as to why the top of the image is almost at the bottom of the device screen.
Does anyone know what is causing this behavior?
My images are large drawables (800x600) that are obviously being scaled down in order to fit in the device window, but that still doesn't explain why there is such a large amount of empty space above the image.
I would prefer to just scroll the ImageSwitcher itself but that hasn't worked thus far.
Here is my layout:
<ScrollView android:id="#+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageSwitcher android:id="#+id/switcher"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"/>
</ScrollView>
My suspicion is that the images are being displayed due to the resolution of the original image. I set ImageView.setScaleType(ScaleType.FIT_START); and this appears to have fixed the problem.
Try to call ImageView.setAdjustViewBounds(true) in addition to setScaleType. If you still have problems with your ImageView (inside ImageSwitcher) taking more space than needed, try to peek at the ImageView with the hierarchyviewer in the SDK tools.

Categories

Resources