Android scaling image vertically - android

I am trying to create a mini progress bar that will indicate how much data a user has used. It doesn't even need to animate, I just need it to change in size based on a variable I have set up. So when I load up the screen, how much of the progress bar is filled up will depend on a variable I have set up in Java.
I have managed to get this working horizontally by using
scale = new ScaleAnimation(0.0f, 1.5f, 1.0f, 1.0f,1.0f, 1.0f);
This will start the image from nothing and stretching out to its full size, so I can use it as a horizontal progress bar which is great.
Now I woulda thought doing this vertically instead would be a simple matter of just changing the fromY to 0 and the toY to the new size I want. However when I do this, by changing the fromY to 0.0f and the toY to 2.0f, the image actually moves and stretches at the same time rather than stretching. Also the image moves position for some reason. It starts off higher up than where I positioned in using XML, and moves down to the original position and stretches out to the new scale I set up.
The code I use to try and stretch it vertically is
scale = new ScaleAnimation(1.0f, 1.0f, 0.0f, 2.0f, 1.0f, 1.0f);
So can someone please explain why it stretches horizontally fine, but when I try to stretch it vertically, it suddenly moves and stretchs at the same time.
Would be very grateful for any help.

I appreciate you have solved the animation issue you mentioned, but based on you saying you don't even need the bar to animate, the ClipDrawable class may make your life simpler. You can define one in an xml layout and then set the clip level from your code, which will clip the given drawable by the given amount.
Check this out for more details on how to use it:
http://developer.android.com/guide/topics/resources/drawable-resource.html#Clip
Hope that's of some help.

Related

Circular scale animation

I'm trying to achieve an animation which scales over a view like this:
At the moment I'm using a scale animation loaded from xml:
This in action looks like this:
It scales more like a square.
I was using ObjectAnimator/ValueAnimator with properties before.
Some devices had specific problems with measuring heights for scaling so I reverted to using xml definitions.
Does anyone have any clue how to achieve the above?
you must define programmatically you ScaleAnimation.
first get radius of your circle(ImageView) at first position(before start Animation).
for get radius you can get width of your imageview and then division to 2.
second you must get width and height of your square.
third do it :
anim_to = (width_square + height_Square) / radius_circle;
finally set "anim_to" to your ScaleAnimation and start animation like this:
ScaleAnimation my_reveal = new ScaleAnimation(1,anim_to, 1,anim_to, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
my_reveal.setInterpolator(new DecelerateInterpolator());
my_reveal.setDuration(500);
my_circle.startAnimation(my_reveal);
sorry for my bad ENG ;)

Zoom in images with their layout

I want to zoom in to a specific point on screen. I have a layout containing child image views. Scaling layout is working great but the problem is in content images; when I click one of them, after scale layout, it triggers click of another image. The positions of the images are still the same so the onclick trigger is not scaled. How can I scale layout with its image children simultaneously?
This is my scale code:
ScaleAnimation zoomInAnimation = new ScaleAnimation(1, 2f, 1, 2f, zoomX, zoomY);
subcategoryLayout.startAnimation(zoomInAnimation);
I'm not sure I properly understand your question, but it sounds as if you are not filling the elements after the animation ends. You might try using setFillAfter

Android - How to check the display edges

hi I'm working with the animations, I currently have 4 directional buttons, depending on which button I press, the image will move in that direction. My problem is so that the image goes beyond the edges of the screen, and I have no idea how to do it xD. For the animation use this ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(IMG3, "translationX", Position_Start,Position_End);
Of course! All you need to do is get the size of the screen in pixels (see here). the top left corner of the screen will be (0,0) and the bottom right will be your output from one of the functions provided.
Then, all you need to do is get the position of your view (animation) using View.getLocationOnScreen() and/or getLocationInWindow() to get the top left corner and using View.getWidth() and View.getHeight() to get the size of your view.
The final step is to make sure that View.getLocationOnScreen() is never less than (0,0) and View.getLocationOnScreen() + (getWidth(),getHeight()) is always less than display.getSize(size), or whatever method you use to get screen size.

How to explain the Coordinate system on Android animation?

As we know , the android coordinate system is start from the top left corner of the android screen. The x-axis is down growth and the y-axis is right growth.But I found it's not right for the animation.
For example, I initialized the TranslateAnimation using the constructed function:
TranslateAnimation ta = new TranslateAnimation(0.0f, 200, 0.0f, 200);
Does the coordinate system have changed ? I found it didn't start from the top left corner.
Then I initialized the other translateAnimation for moving up and right direction :
TranslateAnimation ta = new TranslateAnimation(0.0f, 200, 0.0f, -200);
ta.setReaptMode(Animation.REVERSE);
The same behavior would be found.
I am confused about it.
I believe that constructor for TranslateAnimation uses deltas. See this. Or look at the constructor sig. : (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta). So if you want your anim. to jump up first, you could use a negative third ctor param.
More precisely:
An animation can never start until after the layout has been measured. One usually shouldn't have to worry about how this works beyond that the algorithm is mostly very good and you can take control of its strategies by setting layout parameters. In short, by the time an animation might be started, we know where you want the view to be on the screen, because you set layout parameters.
Translate animation then takes deltas from that position. So your current animation shouldn't start from the top left, but rather wherever those layout params were evaluated by onMeasure.
Some would say- how annoying. It's gonna get complicated even if you just want to do some simple up-down type animations... Well, here's an advisable development strategy; it snould make android animation development a breeeze. Set an animationListener on every animation. In onAnimationEnd, in possibly a parametized way, reset the layout parameters on the view your animating to where you expect it to be. That way, you'll get no surprising "jumps" when you re-apply an animation again. You may need to invalidate in some circumstances, or clearAnimation. The reason that this works is that the measure pass will be caused to come round again and you'll have a new offset for your TranslateAnimation. Finally, you may want to do all this resetting posted to the message queue of a view using post(Runnable runnable) in the listener, so you're off the last pass of the animation draw.
I too found android Animations can occasionally surprise you and cause jumpy behaviour. But if you do it like this, constructors taking delta params shouldn't be confusing again.

Android bitmap shift/move issues

I have a problem that I can not really solve. I have created a graphing app which will contain data with alot of data points. To enable scrolling in the graph i create a bitmap of the graph data, and the shift / move the bitmap to the right or left of the screen depending on the user input. The bitmap is always the graph view height and graph view width. When the user moves the bitmap, i shift the bitmap with:
memoryCanvas.drawBitmap(memoryBitmap, bitmapShift, 0.0f, paint);
where shift is a a float value containing the shifting values.
Now, on most devices i have tried this is, it works very nice. I have tried it on HTC Desire, Galaxy Tab and Galaxy S. When testing this on my own Galaxy S however, i get strange results that i can not explain. Do note that my Galaxy S contains a custom rom (with android 4.0.4), so that is probably the reason why i get this behavior, but i still can not understand it or properly mitigate it.
So in a normal use case behavior, the bitmaps get shifted by bitmapShift number of pixels, and then i fill in the empty space by normal line drawing. But when using my phone, dragging the bitmap either direction slowly, so the bitmapShift values are around 0.5, the bitmap does not move, or only moves sometimes. I have compared the bitmapShift on the other platforms and they are in the same range, 0.5 when dragging slowly. This behavior does of course screw up my drawings a lot. This does also happen when doing fast dragging, but its most visible when doing it slowly.
I can not really figure out what causes this behavior. The bitmap does not move according to my bitmapShift value. It does on all other platforms i have tried. If i skip using bitmaps, and only draw lines according to the shifting values, everything works fine.
Does anyone have any idea on what could cause this behavior? Im kinda running out after sitting some days trying to figure it out. The critical code is below. This code is in my onDraw function.
memoryCanvas.setBitmap(emptyBitmap); //First set another bitmap to clear
memoryCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); //Clear it
memoryCanvas.drawBitmap(memoryBitmap, bitmapShift, 0.0f, paint); //Draw shifted bitmap on it
memoryCanvas.drawLines(lineDrawPoints, 0 , cnt, paint); //Draw remaining lines
memoryCanvas.setBitmap(memoryBitmap); //Set the original
memoryCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); //Clear original
memoryCanvas.drawBitmap(emptyBitmap, 0.0f, 0.0f, paint); //Draw the final image
canvas.drawBitmap(memoryBitmap, 0, 0.0f, paint); //And finally draw on real canvas
Any help, tips, suggestions are very welcome.
Cheers
Wilhelm
When there is only a simple transform set on Canvas (a simple transform = translate only, no rotate, no scale), Skia, Android's 2D rendering library, aligns bitmaps to the pixel grid. This means that a move by less than 1 pixel might not be visible at all. A silly workaround is to set a very, very small scale or rotate transform on Canvas before drawing your bitmap. This has the side effect of not snapping bitmaps to the pixel grid.
I think I should just add a new API on Paint to let apps do subpixel positioning of bitmaps no matter what transform is set.

Categories

Resources