I am following this link for my animation Requirement.
https://medium.com/#ayushpguptaapg/how-to-add-shine-glare-effect-on-an-imageview-ab3e9e660307
This has done on button click listener. Which is working fine. But i want to do it without button click as this animation starts as the activity load.
I have searched alot, but didn't find any simple solution.
Can anybody tell me the simple solution.
Thanks in advance.
#ismail, i am using your this code, like this:
public class MainActivity extends AppCompatActivity{
Button shinebtn;
ImageView img,shine;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
shine = findViewById(R.id.shine);
shinebtn=findViewById(R.id.button);
doAnimation();
}
public void doAnimation(){
new Thread(new Runnable() {
public void run() {
final Animation animation = new TranslateAnimation(0, img.getWidth()+shine.getWidth(),0, 0);
animation.setDuration(1000);
animation.setFillAfter(false);
animation.setRepeatCount(Animation.INFINITE);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
shine.post(new Runnable() {
public void run() {
shine.startAnimation(animation);
}
});
}
}).start();
}
}
just write the code after setting your elements in onCreate()
#Override
protected void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
setContentView(yor_layout_source);
....
....
}
#Override
public void onResume(){
super.onResume();
//do your animation code here
}
and here your animation function :
public void doAnimation(){
new Thread(new Runnable() {
public void run() {
Animation animation = new TranslateAnimation(0, img.getWidth()+shine.getWidth(),0, 0);
animation.setDuration(550);
animation.setFillAfter(false);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
shine.post(new Runnable() {
public void run() {
shine.startAnimation(animation);
}
});
}
}).start();
}
Related
i want to make a toss app ..it is spining all time and will not stop to show a head or tail..i want that when i click on the coin then it should spin for 2 3 seconds and then stops to show head or tail at random selection...but it is continuously spinning.
Here is the code...
public class MainActivity extends AppCompatActivity {
ImageView Coin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Coin=(ImageView) findViewById(R.id.ImgViewcoin);
Coin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Animation animation = new AlphaAnimation(1, 0);
animation.setInterpolator(new DecelerateInterpolator());
animation.setDuration(3000);
Coin.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
final int[] photos = {R.drawable.heads, R.drawable.tails};
final ImageView image = (ImageView) findViewById(R.id.ImgViewcoin);
final Random ran = new Random();
int i = ran.nextInt(photos.length);
image.setImageResource(photos[i]);
image.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int k = ran.nextInt(photos.length);
image.setImageResource(photos[k]);
}
}
);
}
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
Coin.startAnimation(animation);
}
});
}
}
Remove the findviewbyid from onclick and use with Coin instance you have.
I have an activity where an TextView show up, and after that it fades out. However, everytime it has fade out, it pops back in. I tried
wtext.setText("");
But then my text just disappeares without fading out(after fading in). Does anyone knows how to fix this? Thanks! Kind regards.
This is my code:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
final TextView wtext = (TextView) (findViewById(R.id.wtext));
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
#Override
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
wtext.setText("");
}
};
handler.postDelayed(task, 5000);
}
If you want something to happen after an animation is complete (in this case, hide a textview), you'll have to use an AnimationListener
Animation.AnimationListener myListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wtext.setVisibility(View.INVISIBLE);
}
};
animation1.setAnimationListener(myListener);
You could use a ViewPropertyAnimator, which will change the view's actual properties rather than just its appearance, so that it will not jump back to the original state after the animation is complete.
wtext.animate().alpha(0);
You could try setting fillAfter flag to true on the element: it will apply the final transformation to it after the animation is finished.
I am working o na Android app. This app has tutorial screens. In this tutorial user has to slide right and left to complete the tutorial. When it launches the app it shows first fragment called SwipeLeftFragment. When user swipes left it shows second fragment called SwipeRightFragment. When user swipes right it shows third fragment called CompletedTutorialFragment.
Currently It works fine but animation doesn't work.
Following is my code:
public class TutorialActivity extends FragmentActivity {
#Override
protected void onStart() {
super.onStart();
final swipeLeftFragment swipeLeftFragment = new SwipeLeftFragment();
addFragmentToBottom(swipeLeftFragment);
swipeLeftFragment.getView().setOnTouchListener(new OnSwipeListener(TutorialActivity.this) {
#Override
public void onSwipeRight() {
}
#Override
public void onSwipeLeft() {
TranslateAnimation animation = new TranslateAnimation(0, -swipeLeftFragment.getView().getWidth(), 0, 0);
animation.setDuration(5000);
swipeLeftFragment.getView().startAnimation(animation);
swipeLeftFragment.getView().setVisibility(View.GONE);
final SwipeRightFragment swipeRightFragment = new SwipeRightFragment();
addFragmentToBottom(swipeRightFragment);
swipeRightFragment.getView().setOnTouchListener(new OnSwipeListener(TutorialActivity.this) {
#Override
public void onSwipeRight() {
TranslateAnimation animation = new TranslateAnimation(0, +swipeRightFragment.getView().getWidth(), 0, 0);
animation.setDuration(5000);
swipeRightFragment.getView().startAnimation(animation);
swipeRightFragment.getView().setVisibility(View.GONE);
final CompletedTutorialFragment completedTutorialFragment = new CompletedTutorialFragment();
addFragmentToBottom(completedTutorialFragment);
startSignupActivity();
}
#Override
public void onSwipeLeft() {
}
});
}
});
}
private void startSignupActivity() {
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(TutorialActivity.this, SignUpActivity.class));
}
}, 2000L);
}
}
Can someone tell me whats wrong with my code?
this is the
OnTouchListener I am using
My animation is running on the button press, now i want the animation to start when the activity starts. Following is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animationStart();
Button onButton = (Button) findViewById(R.id.button1);
onButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
animationStart();
}
});
}
private void animationStart() {
ImageView imageanimate = (ImageView) findViewById(R.id.imageView1);
imageanimate.setBackgroundResource(R.drawable.ball_animation);
animation = (AnimationDrawable) imageanimate
.getDrawable();
if (animation.isRunning()) {
animation.stop();
}
animation.start();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
animationStart();
super.onStart();
}
}
I have also tried to initialize the animation in onstart method but it is now working.
I want that when the acitivity starts the animation should play. Anybody could please guide me the right way to do it as it is not working. The animation ONLY works with the button press.
If you want to start animation in onCreate or onStart, you should start animation in handler with 1000-2000 ms postdelay which will give enough time to initialize imageview.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//Start your animation here
}
},1000);
}
I do not test it, I hope it will help you.
protected void onCreate(Bundle savedInstanceState) {
//....
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
animationStart();
}
},0);
//...
}
private boolean mDoStartingAnim;
public void onCreate(Bundle savedInstanceState) {
// ...
mDoStartingAnim= true;
// ...
}
public void onResume() {
if (mDoStartingAnim) {
animationStart();
mDoStartingAnim = false;
}
}
I hope it will help you..
i have to make an application in which it starts with an animation and if we click the back button then it should return back to application manager.But what i have made in it if u click back button during that animation then it goes to application manager but after a second or two the first page(the one after this animation comes up).
Can anyone help??
This is the animation..
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load);
im = (ImageView) findViewById(R.id.load_icon);
rotate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.load_page);
rotate.setInterpolator(new LinearInterpolator());
im.startAnimation(rotate);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
}
The first page opens because you have added
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
P1.class);
startActivity(nextPageIntent);
}
}, 3000);
This launches the activity.For knowing if the animation has stopped use AnimationListener. More details here about animation listener
Android, How to set animation listener for view group?
You just added animation to one image view thats all, you do not doing anything with animation. The problem is, you started one thread to start activity P1 after 3 seconds. That thread only starting P1 activity. Try this and try to avoid killProcess(),
public class LauncherActivity extends Activity {
private Handler mHandler;
private Runnable mRunnable;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
Intent nextPageIntent = new Intent(getApplicationContext(),
XmlParserActivity.class);
startActivity(nextPageIntent);
}
};
mHandler.postDelayed(mRunnable, 3000);
}
/* #Override
public void onBackPressed() {
super.onBackPressed();
mHandler.removeCallbacks(mRunnable);
}*/
#Override
protected void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mRunnable);
}
}
public void onBackPressed() {
android.os.Process.killProcess(android.os.Process.myPid());
}
This is the answer