How to put Text after Animation is running? - java

I want to make some text appear after an animation like rotating for 2min.
I tried using method like isRunning() in Animator but didn't work.
ObjectAnimator object = ObjectAnimator.ofFloat(ima, "rotation", 1080);
object.setInterpolator(new AccelerateInterpolator());
object.setDuration(1000);
object.setRepeatCount(0);
object.start();
boolean bo = object.isRunning();
while(!bo) {
Random ran = new Random();
int count = ran.nextInt(10);
String str = Integer.toString(count);
text.setText(str);
bo = true ;
}

Take your animation object and add animation listener to it
rotatationAnimation.setAnimationListener(new 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
**// set your text visisbilty **
}
});

To listen to an ObjectAnimator, just add the following code :
object.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
/*make your text appear*/
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
I hope I could have helped you!

Related

Stack Runnables in a queue with ThreadFactory

I have a method that takes 4 second to execute, if the user calls it again before it ended than it will refresh, I want to only start the new one after the last one has finished, putting them in a queue.
I've read the documentation, but I still don't know how to implement it, can someone provide an example please?
Code
my method:
public void popClassAdded(int amount){
classAddedText.setText(getString(R.string.you_earned) + amount + " ");
setIn = new AnimatorSet();
setIn.playTogether(
Glider.glide(Skill.QuintEaseOut, 200, ObjectAnimator.ofFloat(classAddedLayout, "translationY", -50, 165))
);
setIn.setDuration(1200);
classAddedLayout.setVisibility(View.VISIBLE);
setIn.start();
classAddedLayout.postDelayed(new Runnable(){
public void run(){
AnimatorSet set = new AnimatorSet();
set.playTogether(Glider.glide(Skill.CubicEaseInOut, 200, ObjectAnimator.ofFloat(classAddedLayout, "translationY", 165, -50)));
set.setDuration(1200);
set.start();
set.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) { }
#Override
public void onAnimationEnd(Animator animation) {
classAddedLayout.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) { }
#Override
public void onAnimationRepeat(Animator animation) { }
});
}
},2000);
shopPrefs.edit().putInt("coins", amount + shopPrefs.getInt("coins", 0)).apply();
}
Calling it:
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//may get clicked more than once in an interval or 4 seconds
((Activity_Main)getActivity()).popClassAdded(100);
}
});

Android: Method looper

In my Activity I've a Method which must repeat until the user clicks on a button
private void AnimateItem(int i){
((AnimationDrawable) myList.getChildAt(i).getBackground()).start();
}
private void CheckItems(){
int[] items = new int[]{1,3,5};
for(int i = 0; i<items.length(); i++){
AnimateItem(i);
}
}
So, here I'll have:
AnimateItem(1);
AnimateItem(3);
AnimateItem(5);
How to execute methods one by one (when 1st was ended, start 2nd... and repeating cycle after last method end) until button was clicked.
Instead on AnimateItem(i) method of yours, if your activity implements AnimationListener we can over ride following methods
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
if (animation == your desired Animation) {
//start your new Animation
}
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}

Put Animation delay on ImageViews and TextViews that belongs to 1 Animation set

I have already a rough idea about how to do this and I think it is by making 2 or more animation set per Image View or Text View but the code would be to long. Is there a way in which I could minimize the codes? Here is the code for every Image View and Text View:
AnimationSet setA = new AnimationSet(true);
fadeIn1.setDuration(1000);
setA.addAnimation(fadeIn1);
TranslateAnimation Trans1 = new TranslateAnimation(270, 0, 0, 0);
Trans1.setDuration(1000);
setA.addAnimation(Trans1);
ImageView1.startAnimation(setA);
//how do i place delay here??
ImageView2.startAnimation(setA);
//how do i place delay here??
TextView1.startAnimation(setA);
//how do i place delay here??
TextView2.startAnimation(setA);
//how do i place delay here??
setA.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
ImageView1.clearAnimation();
ImageView2.clearAnimation();
TextView1.clearAnimation();
TextView2.clearAnimation();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
You can make delay by using handlers..
eg:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ImageView1.startAnimation(setA);
}
}, 2000);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ImageView2.startAnimation(setA);
}
}, 4000);

Android App StartApp Ads Error

I made a game and used an ads company on it (StartApp). I wanna use Interstitial Ads on it. I followed the step they show but can't achieve it. I'm getting error. Here is the LogCat:
http://i.stack.imgur.com/SaIXE.png
Here is the codes:
GameActivity.java
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
StartAppAd.init(this, "10254544", "20454573");
}
MenuScene.java
private StartAppAd startAppAd = new StartAppAd(activity);
#Override
public void createScene() {
startAppAd.showAd();
startAppAd.loadAd();
}
Check the Manifest.xml file. Be sure you wrote the right package name in there.
Try This code and u should update manifest file also
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StartAppSearch.showSearchBox(this);
StartAppAd.init(this, "107181003", "211487617");
StartAppSearch.init(this, "107181003", "211487617");
btnnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); //load next add
Intent intent=new Intent(getApplicationContext(),Second.class);
startActivity(intent);
}
});
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
boolean _active = true;
}
return true;
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
startAppAd.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
startAppAd.onResume();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
private StartAppAd startAppAd = new StartAppAd(this);
#Override
public void onBackPressed() {
startAppAd.onBackPressed();
super.onBackPressed();
}
and update in manifest file also
You can not use the
startAppAd.showAd();
startAppAd.loadAd();
inside the Scene class (MenuScene). Instead you can try below method.
In GameActivity class you must be having KeyEvent method. put the code to show the startApp ads in there as below,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if(SceneManager.getInstance().getCurrentSceneType().toString() == "SCENE_MENU"){
try{
startAppAd.showAd(); // show the ad
startAppAd.loadAd(); // load the next ad
}catch(Exception e){
}
}
try{
SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
}catch(Exception e){
}
}
return false;
}
Regards,
Deepak

AlphaAnimation: Fade out works, but not fade in

I have the following code:
public boolean stopped = false;
public void fadeOut(final ImageView obj, final int time, int delay)
{
if(stopped)
{
stopped = false;
anim = new AlphaAnimation(1.00f, 0.00f);
anim.setDuration(time);
if(delay > 0)
{
anim.setStartOffset(delay);
}
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading out. Duration: " + time + "ms.");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading out finished.");
//obj.setAlpha(255);
stopped = true;
}
});
obj.startAnimation(anim);
}
}
and this code works fine. My object (an ImageView) fades out beautifully. But when I run this:
public void fadeIn(final ImageView obj, final int time, int delay)
{
if(stopped)
{
stopped = false;
anim = new AlphaAnimation(0, 1);
anim.setDuration(time);
if(delay > 0)
{
anim.setStartOffset(delay);
}
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading in. Duration: " + time + "ms.");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
Log.v("FADER", "Fading in finished.");
//obj.setAlpha(255);
stopped = true;
}
});
obj.startAnimation(anim);
}
}
It doesn't work - there is no fading in. The ImageView stays completely transparent.
Why is this, any ideas?
And for those who are wondering, yes, I have declared and set the boolean stopped, so it's not the issue, because when I look at LogCat, it's printing the text as it runs the fadeIn() function.
Solved!
Turns out, if you use [ImageView Object].setAlpha, and set it to 0 for instance, then when you run AlphaAnimation, it works between the boundaries of 0 and the current alpha of the image.
So, if you want to keep the image invisible after fadeout, the solution is to use [ImageView Object].setVisibility(View.INVISIBLE), and just set it back to View.VISIBLE before you run your animation.
Mission Accomplished.
Is your view's visibility set to View.VISIBLE?

Categories