I build a action bar.Which contain a item say setting (image).After clicking on the image the another activity is being started.
But I want the starting activity to show sliding transition effect.current activity slide left and hide and starting activity should slide from right to left.
here is code
code for selecting the item
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.setting:
goToSetting();
}
return super.onOptionsItemSelected(item);
}
gotoSetting function :
public void goToSetting() {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageSetting.class);
startActivity(intent);
}
which is calling activity named "DisplayMessageSetting"
please help
Edit the onCreate method in your DisplayMessageSetting activity like this :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//specify the animation
overridePendingTransition(R.anim.slide_left_in, 0);
//...
}
Create a slide_left_in.xml file in your res/anim folder (create the folder if it doesn't exist) and paste the following :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="400"/>
</set>
This way you just specified an animation set with one translate animation in it.
You have a lot more options to tweak that animation, you can learn a bit more about that here.
Related
i'm making a program on android : the user has to push on an animated button to make the button invisible and increase score. But, while testing, the animated button is disabled during animation : the animation is perfectly done but we can't use animated button during animation. I've searched answers but i haven't found.
MainActivity :
Public int score = 0;
Public Button button1 = (Button) findViewById(R.id.button1);
Public Animation anim1 = AnimationUtils.loadAnimation(this, R.anim.translate_up);
button1.startAnimation(anim1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score++;
button1.setVisibility(View.INVISIBLE);
}});
Translate_up, XML animation :
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"android:shareInterpolator="false">
<translate
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="#android:anim/decelerate_interpolator"
android:startOffset="0"
android:toXDelta="0"
android:toYDelta="-900"
/>
Sorry for my bad English, i hope you understand me.
Thanks !
The button itself is actually working on it's original position. So although it looks like your button is sliding to the top, you can still click on the empty space in it's original position and trigger the onClick event.
I am afraid an animation won't work here. If it is a game then you should create a gameloop and update the position of your views (e.g. your button) in the gameloop method.
a few days ago, I had a problem with updating an Action Bar menu icon based on the result of an AsyncTask: Updating Action Bar Menu Item due to AsyncTask result without delay
Now I realized that this problem is not at all related to an AsyncTask or an Action Bar. The problem occurs as well if I want to change a TextView or any other View.
After some time of trial and error I realized that the problem is caused by overridePendingTransition which I use in the Activity which calls the second Activity (where I want to change the icon):
First Activity (in an onItemClick method):
#Override
public void onItemClick(View view, int position) {
...
Intent intent = new Intent(getActivity(), SecondActivity.class);
...
startActivityForResult(intent, anyValue);
getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.no_animation);
}
The animation looks like that:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="#android:integer/config_shortAnimTime"/>
</set>
And this is how the AsyncTask (onPostExecute) of the second Activity looks like:
#Override
public void onPostExecute(String result) {
super.onPostExecute(result);
if (mMenu != null) {
if (result.equals("no favorite")) {
...
mMenu.findItem(R.id.action_favorite).setVisible(false);
mMenu.findItem(R.id.action_no_favorite).setVisible(true);
} else {
...
mMenu.findItem(R.id.action_favorite).setVisible(true);
mMenu.findItem(R.id.action_no_favorite).setVisible(false);
}
}
As explained, instead of switching between two menu icons, you could also use one TextView and setting the text based on the result - the problem remains.
Do you know how I can change the animation without having this weird delay in my second Activity?
Check your integer.xml file
Make the value of config_shortAnimTime smaller according to your needs.
Its in milliseconds.
Prefer making it 1000 (1 sec).
I am working on android. I want to create a rotating image and select parts on it. For example if the image is a human head then while its rotating, the ears can be selected. Now I did the image part selection, but I don't know how to rotate it? Can somebody show me an easy way?
You have 2 ways to rotate a image, i put the simple with xml.
First inside directory res create a directory with the name anim
inside the directory anim create a file xml with this code:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
>
</rotate>
this code is the instruction to make a image rotate.
finally your main activity must look like that:
public class MainActivity extends Activity implements OnClickListener{
ImageView imagen; //declare the image will use
Button boton; // this button will activie de rotation
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagen = (ImageView) findViewById(R.id.iv);
boton = (Button)findViewById(R.id.bt);
boton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt:
Animation rotacion; // declare an animation
rotacion = AnimationUtils.loadAnimation(this,R.anim.rotar);
rotacion.reset();
imagen.startAnimation(rotacion);
break;
default:
break;
}
}
You should use http://developer.android.com/reference/android/view/animation/RotateAnimation.html class to make animation and animate your view
In my android application I have four imageviews when I click any one of that all(4) images are animating. I set the zoomout animation for all the images. once the animation is finished I am starting a new activity. The problem is once i click a back button The images(all the 4) images are hide. If I start a application from home screen the Images are showing. please anyone help me how to show the images once i click the back button
synopsis.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//synopsis.setBackgroundResource(R.drawable.aa);
synopsis.startAnimation(animZoomOut);
team.startAnimation(animZoomOut);
music.startAnimation(animZoomOut);
gallery.startAnimation(animZoomOut);
//synopsis.startAnimation(android.R.anim.slide_in_left);
animZoomOut.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
intent = new Intent(HomeActivity.this, Activity_Synopisis.class);
startActivity(intent);
}
});
}
});
<?xml version="1.0" encoding="utf-8"?>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0"
android:toYScale="0" >
</scale>
The activity is being recreated thus the animation result is gone, you either need to have some sort of preference or pass a variable in the bundle, when the activity after the animation loads, set the variable 'animation_finished' to true and then simply check in the oncreate of the imageview activity: if true: show/hide your images appropriately
When the new activity is started, the old activity with the 4 images will remain in the state (i.e. with all images zoomed-out), so when you resume the old activity, you won't see that. One solution is to set the state of the 4 images to "normal" (i.e. show them) in the onResume() method of your old activity.
As per your short note and my understanding,
intent = new Intent(HomeActivity.this, Activity_Synopisis.class);
startActivity(intent);
synopsis.cancel();
team.cancel();
music.cancel();
gallery.cancel();
Try this one,It may help you out...
Thanks for the answer all.
I resolved this with the follo
synopsis.clearAnimation();
team.clearAnimation();
gallery.clearAnimation();
music.clearAnimation();
Added these above lines of code in Resume
How can I trigger animations on individual views when switching activities?
I.e. if the user clicks a button to go to the next page, I'd like some of my views to fly off screen, and have the background crossfade into the next screen, instead of having the whole screen be animated as one piece.
Is this possible? And if so, how should it be done? (I'm using the most recent API, 4.1, and it doesn't have to be backwards compatible)
EDIT: Currently, doing the transition-in animation is working fine by calling it in onResume(), but when I press back, the activity switches faster than any animations started in onPause() so that makes me think there's a better way/place to do this.
Overriding onResume() works fine, but onPause/onStop don't wait for
the animation to complete before moving to the next screen.
What ever starts the event ex. button click would need to start the animations before start activity is called.
button.setOnClickListener(new ViewOnClickListener() {
#Override
void onClick(... {
// start animations
// wait till they are finished
// start activity
}
});
Since every event that starts a new activity is going to have animation code I would also recommend moving it into some sort of helper class to avoid having duplicate code all over the place. ex.
button1.setOnClickListener(new ViewOnClickListener() {
#Override
void onClick(... {
helper.AnimateViews(/* probably pass activity or context */);
// start activity
}
});
button2.setOnClickListener(new ViewOnClickListener() {
#Override
void onClick(... {
helper.animateViews(/* probably pass activity or context */);
// start activity
}
});
public class ViewAnimiationHelper {
public void animateViews(Activity activity) {
// find all views if not found then don't animate them
View view1 = activity.findViewById(R.id.view1);
if(view1 != null) {
// animate view
}
View view2 = activity.findViewById(R.id.view1);
if(view2 != null) {
// animate view
}
}
}
This is all sudo java code but hopefully enough for you to get the idea. Good luck!
You can set up animations (like slide) when you switch between activities like this :
In the res folder, create an anim folder
For example, put two xml files for slide.
slide_in.xml
<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="200"/>
</set>
slie_out.xml
<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="200" />
</set>
Then on your java code just write this :
Intent i = new Intent(YourActivity.this, OtherActivity.class);
this.startActivity(i);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
If you are testing that on a real device, don't forget to allow it to play animations (Settings -> Display -> Animations -> All Animation)
Hope it helps !:)