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.
Related
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 need to translate and scale image view like on this picture from state 1 to state 2,
State 1 is center of screen, state 2 is any location on screen;
I have combined to animations
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX*5,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32)*5);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
Where "as" is AnimationSet. When I start the animation.
Everything works well, start and end position ok, but translation between them not going along "straight" line. It goes like an arc, on picture 2;
What kind of scale-animation and translate-animation need I perform and in which order to get straight transformation?
picture 1 pic1
picture 2 pic2
After changing order of animations and removing mult by 5 in translateanimation all works ok
a = new ScaleAnimation(1f, 0.2f, 1f, 0.2f);
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
a= new TranslateAnimation(Animation.ABSOLUTE,startx,Animation.ABSOLUTE,destX,Animation.ABSOLUTE,starty,Animation.ABSOLUTE,(destY-32));
a.setDuration(1000);
a.setStartOffset(2000);
as.addAnimation(a);
I am animating an ImageView from the center of the screen to the top-left using an Animation Set.I am Translating and Scaling the View.
Heres the code snippet.
AnimationSet tempAnimation=new AnimationSet(true);
TranslateAnimation anim = new TranslateAnimation( 0,xPos, 0, yPos );
anim.setFillAfter( true );
anim.setFillEnabled(true);
ScaleAnimation scaleanimation = new ScaleAnimation(1, (float)0.22, 1, (float)0.22, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);
scaleanimation.setDuration(1000);
scaleanimation.setFillEnabled(true);
scaleanimation.setFillAfter(true);
tempAnimation.addAnimation(scaleanimation);
tempAnimation.addAnimation(anim);
tempAnimation.setDuration(1000);
// This takes care that the ImageViews stay in their final positions after animating .
tempAnimation.setFillEnabled(true);
tempAnimation.setFillAfter(true);
mimageView.startAnimation(tempAnimation);
So,the problem is that it is leaving behind a trail /previous positions for just a brief moment.But it doesnt look good or smooth.I have noticed that if I use only 1 animation it is fine but using the animation set causes the trail.Are there any tips to avoid this?
Cheers
Found the answer eventually at https://code.google.com/p/android/issues/detail?id=22151.
Scale and Translation Animations have a problem on Android 2.3 .The simplest way to fix this is to add a small transparent padding around the image.In the code shown above,Just add
mimageView.setPadding(1,1,1,1);
Works like a charm!
I have an xml with 12 image. for example 3*4 table, where each cell contains an image.
In the OnCreate I write animation every image:
AnimationSet set = new AnimationSet(true);
Animation animation = new TranslateAnimation(
Animation.ABSOLUTE, -300.0f, Animation.START_ON_FIRST_FRAME, 0.0f,
Animation.ABSOLUTE, -800.0f, Animation.START_ON_FIRST_FRAME, 0.0f);
animation.setFillBefore(true);
animation.setFillAfter(true);
animation.setFillEnabled(true);
animation.setDuration(5000);
I want to start the animation from the top left of the screen and to end the animation to real position. But the animation is only use the image or cell region. Is there any solution to the animation, use the whole screen?
If they're all under the same parent view, you can set the parent's clipChildren attribute to false, which would allow the animation to extend beyond its own constraints (as long as it still remains within the boundaries of its parent):
android:clipChildren="false"
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());