I'm trying to scale a View equivalent to the right and left from center of a View using ScaleAnimation . Whatever values I set for pivotX and PivotY it always scale in the same way (like right edge appears to be scaling keeping left edge constant). Below is the code I used to initialize the ScaleAnimation. Can anyone please let me know, if Im doing anything wrong?. Thanks.
final ScaleAnimation scaleAnim = new ScaleAnimation(1.0f, 2.0f, 1.0f, 1.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
Initialing the anim solved the problem.
I just used the below to code to initialise it.
anim.initialize(/* animate view */child.getWidth(),
child.getHeight(),
/* parents view */ this.getWidth(),
this.getHeight());
Related
I need to Animate Splash Screen in Android and I'm having a circle in the center of the Screen and I want that to Stretch to fill the entire Screen in an Animated way.How do I make it?
Its call Scale Animation you can do it like that
ScaleAnimation scal=new ScaleAnimation(0, 1f, 0, 1f, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scal.setDuration(1000);
scal.setFillAfter(true);
YOUR_VIEW.setAnimation(scal);
YOUR_VIEW is the view of your xml(like ImageView) which you want to perform animation.
I have a parent container called mContainer and a child view called mChildImageView. The ImageView is positioned in the middle of the container and I am trying to translate the ImageView to the top left without cutting off the ImageView.
What I would like is to translate the top most point of the ImageView to the top left part of mContainer, this way it would make the ImageView cut off.
This is the following code I am using, as you can see I am using Absolute coordinates, I would like to make it more dynamic how can I accomplish this?
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE, // Specifies X-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
0.0f, // Change in x coordinate at start of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE,
-30, // Change in x coordinate at end of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE, // Specifies Y-Type interpretation, ABSOLUTE, RELATIVE_TO_SELF, RELATIVE_TO_PARENT
0.0f, // Change in y coordinate at start of animation, can be absolute or percentage if RELATIVE
Animation.ABSOLUTE,
30 // Change in y coordinate at end of animation, can be absolute or percentage if RELATIVE
);
mChildImageView.setAnimation(a);
mChildImageView.animate();
If you want to change TranslationX or/and TranslationY parameters of a view with relation to its parent, I highly recommend you to use Animators instead of Animations as they have some drawbacks (for example: Android - Animation Issue).
Possible variant:
mChildImageView.animate().translationX(newTranslationX).translationY(newTranslationY).setDuration(duration).start();
I figured it out so to get it to move to the top left we can do the following
TranslateAnimation a = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
-20.0f, // Translate to left - This is arbitrary enter your own value
Animation.RELATIVE_TO_PARENT,
0.0f,
Animation.RELATIVE_TO_PARENT,
-15.0f // Translate up - This is arbitrary enter your own value
);
mChildImageView.setAnimation(a);
mChildImageView.animate();
RELATIVE_TO_PARENT uses percentages so 0.0f to 1.0f
I have some buttons in my app that i want to scale up when they are clicked
for that i created a selector
selector_sms_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_sms_big" android:state_pressed="true"></item>
<item android:drawable="#drawable/ic_sms" android:state_pressed="false"></item>
</selector>
and declared my button as follows
<Button
android:id="#+id/sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector_sms_button" />
the problem is - the button does not grow on click at all
the size of ic_sms_big is 300x300dp and the size of ic_sms is 72x72dp
I don't want to create an animation file for them if i absolutely don't have to.
the problem is the button should grow to at least 3 times its size when clicked, and it doesn't.
What can I do to fix it ?
EDIT :
my request "I don't want to create an animation file" means not that i don't want to create an XML file for it, but i don't want to create a reference for it in code at all.
I have 6 buttons that are supposed to do that
if you don't want to add a file, you could do it programmatically
Animation anim = new ScaleAnimation(
1f, 1f, // Start and end values for the X axis scaling
startScale, endScale, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
anim.setFillAfter(true); // Needed to keep the result of the animation
v.startAnimation(anim);
The solution is to add padding to the smaller version of the button
have it take the exact same amount of pixels as the bigger image, but have lots of padding, and then on click the image changes and the icon appears to grow
To implement it programmatically you could you the following code:
private void animateView() {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation growAnimation = new ScaleAnimation(1.0f, 3.0f, 1.0f, 3.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
ScaleAnimation shrinkAnimation = new ScaleAnimation(3.0f, 1.0f, 3.0f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animationSet.addAnimation(growAnimation);
animationSet.addAnimation(shrinkAnimation);
animationSet.setDuration(200);
viewToAnimate.startAnimation(animationSet);
}
You should change the size of your button in onClick method:
button.setLayoutParams (new LayoutParams(width, height));
Try removing android:state_pressed="false" on the smaller button:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_sms_big" android:state_pressed="true"></item>
<item android:drawable="#drawable/ic_sms" ></item>
</selector>
And also you should consider this:
state_selected is used when an item is selected using a
keyboard/dpad/trackball/etc.
state_activated is used when View.setActivated(true) is called. This is used for "persistent selection" (see Settings on tablet for instance)
state_pressed is used when the user is pressing the item either through touch or a
keyboard or a mouse
state_focused is used if the item is marked focusable and it receives focus either through the user of a keyboard/dpad/trackball/etc. or if the item is focusable in touch mode
Try something like this on your button
button.animate()
.setDuration(2000)
.scaleX(3.0f)
.scaleY(3.0f);
Animation anim = new ScaleAnimation(
1f, 0.7f, // Start and end values for the X axis scaling
1f, 0.7f, // Start and end values for the Y axis scaling
Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling
Animation.RELATIVE_TO_SELF, 0.5f); // Pivot point of Y scaling
anim.setFillAfter(false); // Needed to keep the result of the animation
anim.setDuration(300);
iv_logo.startAnimation(anim);
I have achieved the desired result using this.
The zoomed out will be strictly in the center of the view.
I am rotating a rectangle area view and applying the transformation after in this manner
AnimationSet snowMov1 = new AnimationSet(true);
RotateAnimation rotate1 = new RotateAnimation(0,-90, Animation.RELATIVE_TO_SELF,0.0f , Animation.RELATIVE_TO_SELF,0.0f );
rotate1.setDuration(000);
snowMov1.addAnimation(rotate1);
TranslateAnimation trans1 = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f);
trans1.setDuration(000);
snowMov1.addAnimation(trans1);
snowMov1.setFillAfter(true); //this will apply the animation and keep the transformation
This however does not rotate the focus area of that view. The focus area remains the same.
Can someone please help me how the focus area can be rotated as well ??
Thank you.
Unfortunately, there is no way to do this automatically. Animations only update the drawing position of views, and not their actual position. Your best bet here would be to set a listener for the animation and to actually change the position in the onAnimationEnd method.
My custom ScaleAnimation seems to ignore its pivot point. Here is my super-call:
super(1.0f, widthFactor, 1.0f, heightFactor, pivotX, pivotY);
When I set pivotX to 1.0f and pivotY to 0.0f the top-left corner is fixed although it should be the top-right corner that is fixed.
Can you tell me what I am doing wrong?
Thank you!
maybe try with view.getLeft() and view.getTop() where view is the view on which you're trying to apply the animation instead of 1.0f and 0.0f? I'm a bit of android newbie myself, but using these has worked for me.