Hi am doing one application here when my activity starts that time i need to rotate image first then i need to move image from top to center there i have to scale my image after some time i have to in visable my imageview,i tried using below code first i applied rotate animation then i applied translate animation.after 2 seconds i applied scale animation but image is not scaling in center its taking imagview original postion(top) there its scaling but i need to scale imageview after move animation where is there at that postion i need scale image view...any one suggest how to apply different animations to single imageview.
public class A extends Activity{
TranslateAnimation moveLefttoRight1;
Animation logoMoveAnimation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
final ImageView myImage = (ImageView)findViewById(R.id.imageView1);
//rotate animation
final Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotator);
myImage.startAnimation(myRotation);
//translate animation
moveLefttoRight1 = new TranslateAnimation(0, 0, 0, 200);
moveLefttoRight1.setDuration(2000);
moveLefttoRight1.setFillAfter(true);
myImage.startAnimation(moveLefttoRight1);
//scale animation
logoMoveAnimation = AnimationUtils.loadAnimation(this, R.anim.sacle);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
myImage.startAnimation(logoMoveAnimation);
}
}, 2000);
}
}
Use Animation listener.
when get animation end start another animation.
for better reference
http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
http://www.techrepublic.com/blog/android-app-builder/how-to-use-android-animation-listeners/
Related
I have an imageview with 5 backgrounds to choose from. I want to fade image2 out and set image5 as background with fade in effect. This should keep changing randomly. The problem is, how do i do this efficiently?
this is how i give fade in and fade out effects using system animations-
fade out
Animation out = AnimationUtils.makeOutAnimation(this, true);
viewToAnimate.startAnimation(out);
viewToAnimate.setVisibility(View.INVISIBLE);
fade in
Animation in = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
viewToAnimate.startAnimation(in);
viewToAnimate.setVisibility(View.VISIBLE);
this is how i change my background-
search_engine_identifier.setImageResource(R.drawable.ic_yahoo);
Create two in xml you can get from here then load them like this.
ImageView myImageView = (ImageView) findViewById(R.id.imageView2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Splash.this, R.anim.fadein)
Animation myFadeOutAnimation = AnimationUtils.loadAnimation(Splash.this, R.anim.fadeout);
int value = 0; // Global Type
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(value%2 == 0){
doFadeOutAnimation();
}else{
doFadeInAnimation();
}
value ++;
}
});
Another way to do this is by using one of Android's View Animator classes, such as ObjectAnimator. Here's what I'm doing:
1) Add the drawable ID's of the images you want to use into an int array.
2) Create ObjectAnimators for the fadeIn and fadeOut animations. You can set the duration of the fade in and fade out to whatever you want.
3) Add an AnimatorListener to the fadeOut ObjectAnimator so that when it finishes, it will set the new image (which is chosen randomly by selecting a random number from the images array) and then it will fade back in with the new image, using the fadeIn ObjectAnimator.
4) Create a runnable and in it's run method, start the fadeOut animation.
5) Call handler.postDelayed on the runnable and use that to decide how long you want each image to stay before fading out.
6) At the end of your Runnable's run method, call handler.postDelayed again so the images will continue to fade in and out, but you should make sure that you have some kind of conditional statement so that you can stop the animation when you need to, which is why I used the boolean "running" so I can set it to false when I need to stop the handler from looping.
ImageView mImageView = (ImageView) findViewById(R.id.imageView);
boolean running = true;
final int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5};
final Random random = new Random();
final Handler handler = new Handler();
final ObjectAnimator fadeIn = ObjectAnimator.ofFloat(mImageView, "alpha", 0f, 1f);
fadeIn.setDuration(1000);
final ObjectAnimator fadeOut = ObjectAnimator.ofFloat(mImageView, "alpha", 1f, 0f);
fadeOut.setDuration(1000);
fadeOut.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
int rand = random.nextInt(images.length);
mImageView.setImageResource(images[rand]);
fadeIn.start();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
Runnable runnable = new Runnable() {
#Override
public void run() {
fadeOut.start();
if (running) {
handler.postDelayed(this, 5000);
}
}
};
handler.postDelayed(runnable, 5000);
}
This will continuously loop through your selected images (randomly) with fade in/ fade out animations. You can add a few checks to make sure the same image doesn't appear twice in a row, etc.
I am developing a game where a user needs to tap on the image in ImageView to rotate it. On each tap image rotates by 90 degrees in clockwise direction.
But image is taking time to rotate from old to new position. This is hampering the gameplay experience. I have used the following:
protected void onCreate(Bundle savedInstanceState)
{
...
...
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap myImg = getBitmapFromDrawable(imgview.getDrawable());
Bitmap rotated = Bitmap.createBitmap(myImg,0,0,myImg.getWidth(),myImg.getHeight(),matrix,true);
imgview.setImageBitmap(rotated);
}
});
I want to know is there any other way to rotate the image
without creating any delay in rotation.
I also tried to do it once and couldn't find any other solution but using animation. Here how I'd do.
private void rotate(float degree) {
final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(0);
rotateAnim.setFillAfter(true);
imgview.startAnimation(rotateAnim);
}
You don't need to rotate the object, rotating the view should be enough.
If you are aiming API>=11 you can always do this.
mImageView.setRotation(angle);
Cheers.
Short method
View.animate().rotation(90).setDuration(0);
did you try to use a custom view extending imageview and rotating the image in a background thread?
I want to rotate an imageview from 30 degrees on each click on a button.
On the first clic, i can set the animation properly but i can't succeed to update the imageview position after the animation. When i clicked again on the button, the animation start from the original position of the imageview and not from the final position after the first animation.
Here is my code :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
turnImg = (ImageView) findViewById(R.id.imageViewturnImg );
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.imgTurn);
// Getting width & height of the given image.
int w = bmp.getWidth();
int h = bmp.getHeight();
turnImg .setImageBitmap(bmp);
Button buttonok = (Button) findViewById(R.id.buttonok);
buttonok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
turn();
}
});
}
public void turn()
{
float degrees = 30;
RotateAnimation anim = new RotateAnimation(0, degrees,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(300);
anim.setFillEnabled(true);
anim.setFillAfter(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
Matrix mat = turnImg.getImageMatrix();
mat.postRotate(30,turnImg.getWidth()/2, turnImg.getHeight()/2);
turnImg.setScaleType(ScaleType.MATRIX);
turnImg.setImageMatrix(mat);
}
#Override
public void onAnimationRepeat(Animation animation) {}
#Override
public void onAnimationStart(Animation animation) {}
});
turnImg.startAnimation(anim);
}
I think the problem came from the way i actualize the position in the onAnimationEnd().
ps : sorry for my bad english...
The problem is that your animation is not affecting the same object as your matrix rotation. That is, you are animating the rotation of the ImageView object, but then you are setting the rotation of the image that is within that ImageView object. So, for example, the first time you rotate it, the ImageView rotates from 0 to 30 degrees, and then remains at a rotation of 30 degrees due to the call to setFillAfter(true). Then the onAnimationEnd() handler runs, which rotates the internal image by 30 degrees. This effectively jumps the image to a rotation of 60 degrees, as seen by the user (30 for the View, 30 for the bitmap). Then the next time the animation runs, it rotates the view from 0 to 30 degrees, with the internal image still at its rotation of 30 degrees. This makes it look to the user like it's rotating from 30 to 60 degrees. Then you apply another rotation on the internal image when that animation finishes, ending up with the image rotated to 60 degrees and the view rotated (again) to 30 degrees, so the image is now visually rotated to 90 degrees.
And so on.
There are different ways to handle this problem, but one simple way is to avoid affecting the two different objects - just work with the ImageView. Instead of rotating from 0 to 30 each time, rotate from whatever the current rotation is to that value plus 30 degrees. You can remove the onAnimationEnd() handler, because you will no longer need to rotate the image inside the view.
The resulting code for turn() is much simpler:
public void turn()
{
RotateAnimation anim = new RotateAnimation(currentRotation, currentRotation + 30,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);
currentRotation = (currentRotation + 30) % 360;
anim.setInterpolator(new LinearInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
turnImg.startAnimation(anim);
}
This code assumes an instance variable of currentRotation, which is used to track the last degrees the view was rotated to.
Its a bit more involved if you allow the user to click mid-animation, but not too difficult.
By the way, this is much simpler in the animation system in 3.0; there is now a 'rotation' property on View, and you can run an animation on that property and it can track its own current value. But the approach above should work for older releases.
Here is my scenario.
At first I do an animation set to my view (View.startAnimation(animationSet)) where animationSet consists of Translate + Rotate + Scale all at the same time. It works fine.
on that animationSet I have fillAfter(true).
After some time user click on a button and onClick I must start new ScaleAnimation on that same View. So if I do something like:
#Override
public void onClick(View v) {
ScaleAnimation scaleAnimation = new ScaleAnimation(mOldScaleFactor, mScaleFactor, mOldScaleFactor, mScaleFactor, mPivotX, mPivotY);
scaleAnimation.setDuration(ANIMATION_DURATION_MILIS);
scaleAnimation.setStartOffset(ANIMATION_OFFSET_MILIS);
scaleAnimation.setFillAfter(true);
v.startAnimation(scaleAnimation);
}
All the previous animation (Translate + Rotete + Scale) is forgotten.
How to start new animation from where old animation ends?
You could try to implement a listener and possible capture the information from when the previous animation left off, however I'm not sure you'll be able to query the animation for its state. Another option is to make sure the animation stops into a known state. That way you can know how you need to start the next animation.
public float param1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//load text view to add animation
ImageView image1 = (ImageView) findViewById(R.id.splash_imageView1);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_anim);
image1.startAnimation(fade1);
fade1.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//interrogate animation here
}
hi i want to give animation using only one image in drawable folder means in imageview tag of xml file i increase width and height of same image now i want to animate it in one place
i had the code which uses series of images seved in drawale folder and animate it but i have only 1 image ..pls give any suitable example which uses only 1 image and animate that image using imageview height and width increasing..thanks in adv
below is my code..
final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
splashImageView.setBackgroundResource(R.drawable.flag);
final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getBackground();
splashImageView.post(new Runnable(){
#Override
public void run() {
frameAnimation.start();
}
});
final SplashScreen sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(5000);
}
}
catch(InterruptedException ex){
}
}
};
mSplashThread.start();
}
Try this:
ScaleAnimation scaler = new ScaleAnimation((float) 0.7, (float) 1.0, (float) 0.7, (float) 1.0);
scaler.setDuration(40);
imageView1.startAnimation(scaler);
"become big and small in one place" usually is called scaling.
I didn't really read it (just googled "android scaling images"), but this forum post might help you out. It seems to have a code tutorial:
http://www.anddev.org/resize_and_rotate_image_-_example-t621.html