Android: AnimationDrawable squeezing my image - android

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.

Related

Android Game development - Imageview automatically shrinks at screen edge

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);

How to force android view to redraw it's background-drawable? / probably an animation is blocking it to work properly

Is there a way to force an android view to redraw it's background?
I have set a drawable shape with a gradient as background which works fine but after changing the view's height you can see those ugly gradient steps.
What can I do?
I tried view.invalidate() and view.refreshDrawableState() without any visible difference.
Thank you!
//EDIT:
Here are some more details and my code:
After your answers I think the banding is a result of my probably bad code. Here is what I'm doing:
Scaling a view
setting its new width and height because the scaled view does not accept user input in other areas then the "old" one (see android weird (at least for me) behaviour of control after scaling / doesn't accept input in it's whole area )
trying to set the background again (which contains a gradient and a rectangle with rounded corners)
Here is my code:
public void onAnimationEnd(Animation animation)
{
MyView view = (MyView) ((ExtendedScaleAnimation) animation).getView();
view.getLayoutParams().width = toWidth;
view.getLayoutParams().height = toHeight;
view.clearAnimation();
view.requestLayout();
view.refreshBackground(); // background will be changed
}
On the device you can see that the background drawable is changed but there is a flickering which seems to me like the backround change is applied before the animation is over. Programatically it should be over! Or am I wrong?
Thank you again!
Can you try after resize:
v.setBackgroundResource(null);
v.setBackgroundResource(...my drawable...);

How to position a FrameAnimation in android?

I have a image loaded as a background and when the screen is touched I want to animate that portion of the screen with a frameanimation. My frameanimation consists of 9 different png files which are made from just the portion of the screen I want animated. I can get it working when I use entire backgrounds as the frames for the animations, but when I use setbounds() to tell the frameanimation where to draw, I end up with the frameanimation being scaled up to fill the entire screen, which also erase my background. How can I get the frameanimation to stay it's original size and locate it at the same time? I can post code later if this isn't clear, i'm not at the comp right now
public boolean onTouchEvent (MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
this.setBackgroundResource (R.drawable.nova);
Object bg = this.getBackground
touchAni.setBounds (152, 156, 152+140, 156+140);
touchAni = (AnimationDrawable) bg;
if (!touchAni.isRunning())
touchAni.start ();
else
{
touchAni.stop();
touchAni.start();
}
}
}
The way I have resolved a similar problem is that I added another image component to my layout xml on top of the component I want to animate. Normally that image component is android:visibility="gone"
but when I want to run the frame animation I set it visible and start the animation.
This way you can place the animation component wherever you want in your layout.
Well, after much puttering around, I finally found the BounceActivity code here: http://rsequence.com/android_blog/node/107. Seems really complicated for such a simple task but gets me what I want.
Of note, if anyone else finds it useful, the Handler() codes needs to have a switch in it so it's not spinning off cpu cycles to BounceView.draw() when nothing is happening on the screen.

What is the alternative to AbsoluteLayout in Android?

I see posts saying that FrameLayout is the alternative, and that I should use margins to position things (this strikes me as wildly counter intuitive, but ok... if it works, I'll take it). However, I can't get it to work, so, I'm looking for assistance.
here's my code
FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.btn);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(100,100,100,100);
btn.setLayoutParams(lp); //i've tried with and without this line, no change
layout.addView(btn , lp);
The button is drawn at 0,0 no matter what I do. When I change the lp's LayoutParam to FILL_PARENT the button is then stretched to take up the entire screen (which makes sense).
HOW do you get it to draw somewhere else on the screen, irrespective of what else is there?
As always, super grateful in advance.
[EDIT]
It seems my question isn't entirely clear (given the answers) so...
In the code above, the intent is to create a button, pass it to a layout and have it draw at 100,100 on the screen.
I'm aware of the fact that this may mean different things on different devices. I'm ok with that. I simply need a way to, programatically, and at run time, place an item at a SPECIFIC location. I don't want to rely on gravity (or the laws of thermodynamics). I just want to specify a location and have the element appear there :)
As many have pointed out, setting a button at an absolute pixel position on the screen is a really bad idea, and will never work across all of the available android phones. I'm sure there is a better way to achieve the layout you want.
However, to answer the question as asked: to position it at runtime you can use the AbsoluteLayout.LayoutParms.
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);
A good explanation of the different available layouts is available at
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
As I do not completely understand from your description what you're trying to accomplish (do you want to overlay stuff?) maybe the visual examples there can help you in understanding how the layouts work and how to position stuff on them.
What exactly is your final objective with the layout? If all you need is a button -- say, in the center of the screen, you can just make a layout with:
<RelativeLayout
android:xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<Button
android:id="#+id/the_button"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:text="#string/the_button_text"
/>
</RelativeLayout>
AbsoluteLayout is a bad idea because of the large number of screen resolutions you need to support. 100 pixels over on one screen may be halfway, while it may be less than a quarter across the screen on a higher dpi device.

Moving views inside a layout/view in Android

I have more than one question, but I'll start with the more important and problematic one:
I have a FrameLayout with a ImageView inside it. I need to get the size of the "usable area" of the screen my activity is ocupping, so I set the onSizeChanged on my View (I extended the ImageView class). Everything worked fine here. Now I have a 1000x1000 image that I want to show on the screen, but without scaling. I want it to be clipped, really. If I set the ImageView dimensions using viewObject.setLayoutParams(new Gallery.LayoutParams(1000, 1000)); I get the image being showed correctly, but then the onSizeChanged event returns me always the "1000 x 1000" custom size rather than the real screen size value.
Any ideas of how can I show an image with its real size (no scale!) and still get the view to report the screen available space? I can change the layout as needed as well, of course.
. Amplexos.
Are you asking to get the dimensions of the ImageView? If so then you can get that using getLocalVisibleRect. Here's roughly how it's done:
ImageView yourImageView;
public void onCreate(...){
setContentView(...)
yourImageView = (ImageView) findViewById(...);
(...)
}
getImageViewSize(){
Rect imageViewSize = new Rect();
yourImageView.getLocalVisibleRect(imageViewSize);
// imageViewSize now has all the values you need
Log.d("Your log tag", "ImageView width = " + (imageViewSize.right -
imageViewSize.left));
}
There is however a catch. You have to make sure that you don't try to get the size of the view until after view is finished being laid out on the screen. In other words, if you try to get its size in onCreate, its size will be 0. You have to get it afterwards, for example at the same time as you resize your image, assuming that's done with a button. (If you're using a SurfaceHolder you can also call it during the surfaceCreated callback, but I doubt you're using one of those...)

Categories

Resources