android flyin imageView after a certain time
-
18-06-2021 - |
Question
The purpose I aming for is, that 2 seconds after my activity was started an ImageView (with an image) should flyIn to the view. But with the following code nothing happens.
Am I on the right way or can I solve this completely different? What do I have to change that this works?
Here is how my code looks:
public class Main extends Activity {
private ImageView _truck;
private Handler _flyInTruckHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_truck = (ImageView) findViewById(R.id.main_garbageTruck);
_truck.setVisibility(View.INVISIBLE);
_flyInTruckHandler = new Handler() {
public void handleMessage(Message msg) {
_truck.setVisibility(View.VISIBLE);
}
};
_flyInTruckHandler.postDelayed(new Runnable() {
public void run() {
new flyInTruck();
}
}, 2000);
}
class flyInTruck extends TimerTask {
@Override
public void run() {
Animation anim = AnimationUtils.loadAnimation( getApplicationContext(), R.anim.flyin);
findViewById(R.id.main_garbageTruck).setAnimation(anim);
_flyInTruckHandler.sendEmptyMessage(0);
anim.start();
}
}
}
The animation I use is from here : Fly In Animation for a GridView But slightly changed:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true" >
<scale
android:duration="2500"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
Solution
SOLUTION
Ok, I got it (so simple and I didn't thought about that before). I do not need that extra class "flyInTruck". I just can write it like this:
_flyInTruckHandler.postDelayed(new Runnable() {
public void run() {
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.flyin);
findViewById(R.id.main_garbageTruck).setAnimation(anim);
_flyInTruckHandler.sendEmptyMessage(0);
anim.start();
}
}, 2000);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow