How to apply a animation to an imageview in android? - android

what i am trying to do here is,i have downloaded some images in my application and i want to show these images one by one to the user in full screen after some times say every 30 seconds.I also applied an animation to the imageview when image changes here is my code:-
protected Bitmap doInBackground(String... params) {
// TODO Auto-generated method stub
if (!isCancelled())
{
filename=params[0];
if(ShowImages.vn>d2)
{
ShowImages.vn=0;
}
bmp = BitmapFactory.decodeFile(filename);
}
return bmp;
}
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
ShowImages.tt1.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
r1 = AnimationUtils.loadAnimation(ShowImages.context, R.anim.lefttoright);
ShowImages.tt1.setImageBitmap(result);
ShowImages.tt1.startAnimation(r1);
}
}
this is my code for my animation file:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="700"/>
</set>
i have no problem with this code it is executing fine,but when the image change takes place the whole imageview goes blank for some time and then the next image comes.I do not want this,i want the previous image to be there untill the next image change has taken place,can someone suggest me a method for this.

In your case it would be good to use frame-by-frame animation as well define on developer site Frame- by- frame: An object used to create frame-by-frame animations, defined by a series of Drawable objects, which can be used as a View object's background.
Check below tutorial even you will get more example, just try to Google:
Displaying a frame-by-frame animation in Android

Related

How to create Animated Splash Screen such this

I need to make such animated splash screen. Can you help me?
One way is to use Tween Animation like the following. In your case needs more than one anim file
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);
Say like if you have to animate white circle image then do the following
ImageView image = (ImageView)findViewById(R.id.imageView);
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move);
image.startAnimation(animation1);
Now you need to create anim file in res/anim/move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="75%p"
android:duration="800" />
</set>
This is an example. You need to find ways to modify these basic animations for your requirement . For more refer this link
To use a gif for your splash screen.
In your build.gradle file:
compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'
In your activities layout:
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gifView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/gif"
/>
And finally in your class file:
private static int SPLASH_TIME_OUT = 1500;
private boolean isInFront;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_gif);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
if(isInFront)
{
// Start your app main activity
Intent i = new Intent(SplashScreen_Gif.this, MainMenuActivity.class);
startActivity(i);
}
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
You can use a gif image like this and use it on your splash screen instead of a normal jpg or png image

rotate a refresh button in ListView

recently I wrote a function.it's about a refresh button in each list item. what I want is click the button or List Item, the refresh button starts rotate. stops when the request finished.
I use animation as follows:
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="358" />
and some source code are here:
public void refresh(View v) {
Animation rotation = AnimationUtils.loadAnimation(mContext,
R.anim.rotate);
rotation.setFillAfter(true);
v.startAnimation(rotation);
}
public void completeRefresh(View v) {
v.clearAnimation();
}
when the request finished, I call notifyDataSetChanged() to refresh the LiseView.
the problem is that the button was indeed rotating. but when I click it the second time. it's rotating but a little fuzzy. just like this:
any suggestions? thanks a lot.
What is most likely happening on your second (and subsequent clicks) is that the animation is running on it again.
In your current implementation, try using setTag(...) like this:
public void refresh(View v) {
if(v.getTag() != null && (boolean)v.getTag()) {
//do nothing, since we are setting the tag to be true once pressed
} else {
//this view hasn't been clicked yet, show animation and set tag
v.setTag(true);
Animation rotation = AnimationUtils.loadAnimation(mContext, R.anim.rotate);
rotation.setFillAfter(true);
v.startAnimation(rotation);
}
}
In your Adapter, you should keep track of which list items are being animated (I'm assuming multiple items can be clicked at the same time). Once you know that the 'request' is finished, you can update the adapter with the correct items and then call notifyDataSetChanged()

Using FrameAnimations and AnimationDrawable to display images not working

I have a simple .xml layout file that has a simple ImageView, that I want to switch images on. I decided to go ahead and do it with FrameAnimations, using the AnimationDrawable API.
I have managed to create my animation file in "drawable/file.xml", here is the code:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/image_010_1" android:duration="1000" />
<item android:drawable="#drawable/image_010_2" android:duration="1000" />
</animation-list>
Now, Here is the code that should start my animation:
Image1.setBackgroundResource(R.drawable.image_010);
AnimationDrawable animation = (AnimationDrawable) Image1.getBackground();
animation.start();
For some reason, When i run the app I just see the first frame, and the next image does not come after 1000 milis, or at all! What am I doing wrong? Thanks!
so I managed to solve this problem by adding a Runnable to my ImageView:
image.post(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
animation.start();
}
});

How to set animation when I click on button at that time move image from left to right and right to left in android?

I wanna translate 2 images image When I click on Button, At that time from left image move left to right and right image move right to left(both image come in center at that time stop animation) on emulator using android animation.I'm new to android animation.How could i do that.?
coding is much appreciated.Thanks..
i can't explain everything here but i help you to do that. first you must create two new XML file (Animation in old versions or Tween Animation in new versions) for each image , then use translate. for example:
<translate android:fromXDelta="10"
android:toXDelta="100"
android:duration="2000"
/>
for left image, and for right one use your numbers.
then in java file, set your Button and images. then define animation and use them. i think this example is comprehensible:
final ImageView iv1 = (ImageView) findViewById(R.id.imageView1);
final ImageView iv2 = (ImageView) findViewById(R.id.imageView2);
final Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Animation anim = AnimationUtils.loadAnimation(main.this, R.anim.animation);
iv1.startAnimation(anim);
iv2.startAnimation(anim);
}
});
in this example, i use one animation for both of them.
and remember in this site, you can get help from other, not absolute code.
add two xml files to res/anim folder of your project like below:
lefttoright.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate android:fromXDelta="-600%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%" android:duration="300"
android:zAdjustment="bottom">
</translate>
righttoleft.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator">
<translate android:fromXDelta="0%" android:toXDelta="400%"
android:fromYDelta="0%" android:toYDelta="0%" android:duration="300"
android:zAdjustment="bottom">
</translate>
In your layout file keep one ImageView at the left of the screen and other one at the right of the screen
Use the following code snippet in your button's onClick event:
lefttoright = AnimationUtils.loadAnimation(context,
R.anim.lefttoright);
righttoleft = AnimationUtils.loadAnimation(context,
R.anim.righttoleft);
imageView1.startAnimation(lefttoright);
imageView2.startAnimation(righttoleft);
then implement your animation listener:
lefttoright.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
//update imageView's position (e.g. center)
}
});
Do same thing for righttoleft.
Hope it helps.

Android Animation

I trying to move a image from one place to other using animation,but after moving it coming back to original position, how to stop it to the moved position itself.
to let the image in the last place of the animation , try this :
TranslationAnimation ta = new TranslateAnimation(fromX, toX, 0, 0);
ta.setDuration(1000);
ta.setFillAfter(true); // this will let the image in the last place of the Animation
imageView.startAnimation(ta);
After an Animation is done, use the method setFillAfter(true), to make the last animation state persist:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.
Animation.setFillAfter/Before - Do they work/What are they for?
If you need to do something more specific, you could also set an animation listener and move your object at the end of the animation:
animation1.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//change according to your needs
myView.setX(0);
}
#Override
public void onAnimationRepeat(Animation animation) { }
});
Finally got a way to work around,the right way to do this is setFillAfter(true),
if you want to define your animation in xml then you should do some thing like this
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%"
android:toXDelta="-100%"
android:duration="1000"/>
</set>
you can see that i have defined filterAfter="true" in the set tag,if you try to
define it in translate tag it won't work,might be a bug in the framework!!
and then in the Code
Animation anim = AnimationUtils.loadAnimation(this, R.anim.slide_out);
someView.startAnimation(anim);
See this blog post for a solution:
// first set the view's location to the end position
view.setLayoutParams(...); // set to (x, y)
// then animate the view translating from (0, 0)
TranslationAnimation ta = new TranslateAnimation(-x, -y, 0, 0);
ta.setDuration(1000);
view.startAnimation(ta);
I'm sure you would have found the answer by now (I just did... so I am posting for others). Android seems to have officially shifted to a new animation framework called "Property Animation". This has been available since Honeycomb (3.0). This will essentially solve your problem as it animates the actual property values.
DEV GUIDE:
http://developer.android.com/guide/topics/graphics/prop-animation.html

Categories

Resources