Android Game development - Imageview automatically shrinks at screen edge - android

I have a bunch of dynamically created ImageViews representing different objects in a game, which is working fine. However if any Image reaches the edge of the screen, it shrinks. Looks like android is attempting to create a smooth transition, but this is not wanted.
I found another thread with the same issue here: Animation Drawable gets automatically shrinks at the corners ?, however his solution does not work for me, it only enhances the issue as it starts shrinking once the margin hits the screen edge.
This is my code:
final LayoutParams _updated_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
_updated_params.setMargins((int)m_x, (int)m_y, 0, 0);
m_image.setLayoutParams(_updated_params);</code>
Where m_x/m_y is the absolute position of the image and m_image is an ImageView instance.
Does anyone know how I can turn off this automatic resizing?

Add setScaleType
m_image.setScaleType(ScaleType.MATRIX);

Use setScaleType
m_image.setScaleType(ScaleType.FIT_XY);

Related

Set padding postInvalidate not removing padding on the bottom part on left side of puzzle

Hi currently I'm working on a puzzle that slice and randomize one image. Upon setting up I have manually set the padding into 2 for all corners then after the puzzle is finished padding should be set to 0 on all corners as well to display the image in full. Problem is there is an odd thing that happen wherein only the right side of the puzzle removes the padding completely while the other side still has it's padding on the bottom part. This only happens on devices with lower specs. (Nexus One and alike)
Here's my code for the setting of padding after completing the puzzle:
if(true){
enable_puzzle = false;
for (int i=0; i < parent.getChildCount(); i++){
LinearLayout cont = (LinearLayout) parent.getChildAt(i);
for (int c = 0; c < cont.getChildCount(); c++){
View v = cont.getChildAt(c);
v.setClickable(false);
v.setPadding(0,0,0,0);
v.postInvalidate();
}
cont.postInvalidate(); //Tried this didn't work
}
parent.postInvalidate(); //Tried this didn't work
}
for better understanding of how I placed my layout it's pretty simple. I have a linearLayout that in horizontal orientation which holds the imageViews which will be places into the parent container in vertical orientation.
Here's the screenshot for sample.
The puzzle on create (see odd padding on the bottom part on left side)
And upon completing:
This is the screenshot on my Nexus S emulator. Same thing happen on real device with lower specs.
UPDATE:
Upon checking it appears that the main cause was the width of the container for the images. Since I have a 4x4 puzzle and uses weight instead of fixed sizes the width for each images is not evenly distributed causing a 1px/dp difference. Now the only problem is on how can I get the width of the parent horizontal layout to be divided and round up to get an even result for each imageviews?
There is a workaround for this, you can create a listener something like onPuzzelCompleteListener(), something that you are doing even now to remove the padding. Here you can show an ImageView with the complete image. This should solve your padding issue as for now.

Android: AnimationDrawable squeezing my image

I have this weird behavior that I cannot find a solution around...
This only happens in my emulator API Level 10, on my smartphone (Android 4.1) works fine.
I wrote a dynamic animation to show a rolling dice, where 10 random frames are chosen as the frames of the animation, with a duration of 50ms.
When I press the button, the animation run, but squeezed into zero height..... (You can still see some colors from the animating dice), the layout also get messed up.
This does not happen on my phone though.
Here is the portion of the java code:
public void RollDice(View view) {
int id=-1;
AnimationDrawable diceAnimation = new AnimationDrawable();
//create 10 random frames from dice1.jpg to dice6.jpg into diceAnimation
for(int j=0;j<10;j++){
id=getResources().getIdentifier("dice"+String.valueOf(rng.nextInt(6)+1), "drawable", getPackageName());
diceAnimation.addFrame(getResources().getDrawable(id), 50);
}
//assigning and starting animation
diceAnimation.setOneShot(true);
dice.setImageDrawable(diceAnimation);
diceAnimation.start();
}
and the portion of the xml:
<ImageView
android:id="#+id/Dice1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/diceimage"
android:src="#drawable/dice1" />
The button onclick call RollDice().
As final remark, IF I hardcode in ImageView, say android:layout_height="100dp", then the dice will show during animation, but streched into funny shape...
I tried most scaling method, adjustviewbound etc with no luck...
I don't know whether your problem is solved or not.
But I would like to add the answer to this question.
You can try this :
Firstly , set you image view layout_width from match_parent to wrap_content.
Second make use of this below code so as to make the dice disappear completely :
frameAnimation.setVisibility(GONE);
Might have an answer for your, try it out with caution, since I'm a beginner myself.
ViewGroup.MarginLayoutParams mParams = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
ImageView view = (ImageView)findViewById(R.id.image_dice);
layoutParams = new RelativeLayout.LayoutParams(mParams);
layoutParams.width=120;
layoutParams.height=120;
view.setLayoutParams(layoutParams);
Applying that to your image will adjust the .xml on the fly, atleast it works for me when I'm grabbing alot of differently sized images from the web.

Can android padding be made to be not clickable?

EDIT: Answer found through Emil's comments. Code ended up like this:
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
view.setLayoutParams(params);
Thanks all :D
I'm currently writing an Android application and I have a dynamically created tree which displays ImageView elements within it. However, to arrange these images I've used padding left to line them up correctly, dynamically changing how the padding is calculated so that everything turns out evenly. However when I do this, the padding covers up some of the images to the left, and even though you can see the images behind this padding, when you click on that image, it will evaluate the image who's padding is being used (the furthest left element). I'm basically wondering if there is a way to programatically set something that does what padding does to images but isn't clickable? Margins would make sense, but you cannot set margins on ImageViews.
The tree set up is like this:
-----A-----
--B--C--D--
-E-F----G--
Imagine every letter is an ImageView, dynamically placed with padding. But if I click on B or C, D's code will be evaluated because it's left padding covers B and C. The same goes for E or F, click those, and G will be evaluated. I cannot figure out a different way to place these ImageViews. Any help is greatly appreciated.
Consider replacing your padding with layout_margin property. that way the area that separates the padded view is not considered as part of the view but ratter as part of the layout it sits in.
So use
android:layout_margin="10dp"
or:
android:layout_marginLeft="10dp"
for your needs.

move the screen content to the left outside the screen android

is there a way to move the whole screen content outside the screen bounds, with titlebar and all, on the left for example, i tried
content = ((LinearLayout) act.findViewById(android.R.id.content)
.getParent());
to get the screen content and add a margin to it
FrameLayout.LayoutParams pr = (android.widget.FrameLayout.LayoutParams) content
.getLayoutParams();
pr.rightMargin = xOffset;
content.setLayoutParams(pr);
but it doesnt work, the margin appears but the content is not moved just resized to fit the screen, anyone know how to do this the simple way?
you can set the margin in negative(-) values.
for example if your want the layout to move upper use this:
android:marginTop = "-50dip or more u can put"
Are you trying to flip between views? As in, moving that layout off the screen to show another one? If so you should look at using a widget called ViewFlipper which will do exactly as you need. It can also be animated etc.
http://developer.android.com/reference/android/widget/ViewFlipper.html

How can avoid text from being cut off on a button?

I am customizing a button in Android changing its background (using a 9patch png file) and changing its font typeface. I put the button in a RelativeLayout since I need precise positioning and dimensioning of this item in a custom layout. I am doing all this programmatically (from creation to setting and display).
I found this question with the same problem, but the solution will not work for me. I used
continueBtn.setPadding(0,0,0,0);
but the graphical output remains the same. More than half the text is cut off in the view.
An excerpt of the code I am using is the following:
Button continueBtn = new Button(getContext());
p = new RelativeLayout.LayoutParams(width, 45);
p.addRule(CENTER_HORIZONTAL);
continueBtn.setLayoutParams(p);
continueBtn.setTypeface(...);
continueBtn.setTextSize(14);
outerLayout.addView(continueBtn);
continueBtn.setPadding(0, 0, 0, 0);
No matter where I put the setPadding method call, things won't change.
What am I doing wrong? Any ideas?
You are setting a constant height for the button of 45 (pixels, I guess). However, apparently the text you are trying to put in it is taller than the amount of space android will let you use. Make your button taller (by changing the LayoutParams) and it will fit without cutting off. Alternately, use a smaller text size so the text will be shorter and fit.

Categories

Resources