I have a button which basically changes the TextView every time it is clicked. Is it possible to associate an animation with this? I have looked at things such as a 3D flip but they seem a little too advanced for me. Any simpler suggestions?
final Button button1 = (Button) findViewById(R.id.button1);
final TextView textView = (TextView) findViewById(R.id.text_random_text);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
textView.setText(getNextRandomCuisine());
}
});
I suggest you to take a look at the Android webPage. http://developer.android.com/reference/android/view/animation/AnimationUtils.html
the class AnimationUtils helps you to load and Animation Pattern from your xml folder, you can for example make a "fadein" animation, you should dafür create an xml datei, name it for example "fadein.xml" it should be like this
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" >
</alpha>
with this you are just telling your android device, that this animation should variates the alpha value (also the transparence) of your view, from 0 to 1, making your view visible, the "duration" parameter is just how any milliseconds will take to execute this animation.
Related
I have a button which need to fade in. But it works only the first time. It doesn't work the second time.
Here is my code.
final TextView doctorInfoView = (TextView) rowView.findViewById(R.id.doctorInfo);
final TextView specialtyView = (TextView) rowView.findViewById(R.id.specialty);
final ImageButton deleteDoctor = (ImageButton)rowView.findViewById(R.id.deleteDoctor);
final Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fade_in_animate);
final ImageButton editDoctor = (ImageButton)rowView.findViewById(R.id.editDoctor);
final RelativeLayout mainRowLayout = (RelativeLayout)rowView.findViewById(R.id.doctorListInfoView);
final LinearLayout rowLayout = (LinearLayout)rowView.findViewById(R.id.doctorInfoLayout);
final LinearLayout editButtonLayout = (LinearLayout)rowView.findViewById(R.id.editButtonLayout);
final LinearLayout deleteButtonLayout = (LinearLayout)rowView.findViewById(R.id.deleteButtonLayout);
rowLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (isClicked) {
editDoctor.setAnimation(fadeInAnimation);
editDoctor.setVisibility(View.VISIBLE);
deleteDoctor.setAnimation(fadeInAnimation);
deleteDoctor.setVisibility(View.VISIBLE);
mainRowLayout.setBackgroundColor(Color.parseColor("#ffffff"));
doctorInfoView.setTextColor(Color.parseColor("#eeeeee"));
specialtyView.setTextColor(Color.parseColor("#eeeeee"));
editButtonLayout.setBackgroundColor(Color.parseColor("#16aea3"));
deleteButtonLayout.setBackgroundColor(Color.parseColor("#16aea3"));
isClicked = false;
} else {
editDoctor.setVisibility(View.GONE);
deleteDoctor.setVisibility(View.GONE);
mainRowLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
doctorInfoView.setTextColor(Color.parseColor("#000000"));
specialtyView.setTextColor(Color.parseColor("#0d9e9f"));
editButtonLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
deleteButtonLayout.setBackgroundColor(Color.parseColor("#f2f2f4"));
isClicked = true;
}
}
});
Here is fade_in_animate.xml
<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="#android:anim/accelerate_interpolator"
android:duration="500"/>
</set>
I'd appreciated about any feedback.
One approach to solve this would be to set the animation to null
editDoctor.setVisibility(View.GONE);
editDoctor.setAnimation(null);
EDIT: You forgot to set it to infinite
animation.setRepeatCount(Animation.INFINITE);
Here is the xml
android:repeatCount="-1"
android:repeatMode="repeat"
Here is the full documentation
EDIT 2: I didn't see that you are setting the alpha. My bad. This should work! You don't need to repeat it. This will work with the method of setting the animation to null.
editDoctor.setVisibility(View.GONE);
editDoctor.setAnimation(null);
editDoctor.setAlpha(.0f);
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.
I am developing android application and i am trying to apply some roll effect from top of screen for popup window but don't know how to achieve but currently i am adding some other animation effect
this is my popup window function code
private void loadingPopup() {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.profile_popup, null);
final PopupWindow windows = new PopupWindow(layout , 450,650,true);
windows.setFocusable(false);
windows.setTouchable(true);
windows.setOutsideTouchable(true);
windows.setAnimationStyle(R.style.AnimationPopup);
layout.post(new Runnable() {
public void run() {
windows.showAtLocation(layout, Gravity.TOP, 0, 0);
}
});
name = (TextView)layout.findViewById(R.id.name);
profilepicture =(ImageView)layout.findViewById(R.id.profileimage);
String sname = profilelistdb.get(0).get("pname");
name.setText(sname);
String imagename = profilelistdb.get(0).get("pimage");
String totalurl = imageurl+imagename;
imageLoader1.DisplayImage(totalurl, profilepicture);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
windows.dismiss();
}
});
}
This is Style.xml
<style name="AnimationPopup">
<item name="#android:windowEnterAnimation">#anim/appear</item>
</style>
appear.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="50%p" android:toYDelta="0%p"
android:duration="#android:integer/config_longAnimTime"/>
</set>
My Requirement:-
My Popup window should come from top with roll kind of thing and whenever user click close button it should hide popup with reverse roll effect could you please help me how to achieve this.
If you don't understand about what i am trying to say about roll
just check this link and see 30 seconds
https://www.youtube.com/watch?v=KSHVSswMUng this is what exactly i need.
You could use this library. It's for View Animations in android, and works very well. And then use this:
YoYo.with(Techniques.RollIn)
.duration(700)
.playOn(findViewById(R.id.edit_area));
YoYo.with(Techniques.RollOut)
.duration(700)
.playOn(findViewById(R.id.edit_area));
Or experiment with it yourself, to see what looks the best.
Edit: Not sure how to achieve that particular roll-effect that you have in mind now that I see a video of it, but if you can't find it, I'm sure you'll find something in this library that will look good. Good luck.
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
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 !:)