I want to make a one shot animation but be able to play it as many times as I want. Right now it plays only the first time.
.xml:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/p1_ch1" android:oneshot="true">
<item
android:drawable="#drawable/p1_ch1_5"
android:duration="500"/>
<item
android:drawable="#drawable/p1_ch1_4"
android:duration="1000"/>
<item
android:drawable="#drawable/p1_ch1_5"
android:duration="500"/>
</animation-list>
.java:
public void handler_p1_ch1_5 (View target){
ImageView iv = (ImageView)findViewById(R.id.p1_ch1_5);
AnimationDrawable aw = (AnimationDrawable)iv.getBackground();
aw.start();
}
Just call aw.stop() before aw.start()
Use this:
frameAnimation.setOneShot(true);
Change
android:oneShot="true" to
android:oneShot="false"
If you want to replay a one shot animation after previous animation finish you can do like
final AnimationDrawable animationDrawable = (AnimationDrawable) image.getDrawable();
if (!animationDrawable.isRunning()) {
int totalFrameDuration = 0;
for (int i = 0; i < animationDrawable.getNumberOfFrames(); i++) {
totalFrameDuration += animationDrawable.getDuration(i);
}
animationDrawable.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
animationDrawable.stop();
}
}, duration);
}
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 have two images which I want to fade between causing a glowing on and off effect. This should run all the time like an animation, not just when the button is pressed.
Here are the two images:
This was working well before but after some hardware/android OS updates my animation is really jumpy. Here is the Animation XML I was using:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/bottom_bar_add_dark"/>
<item android:drawable="#drawable/bottom_bar_add" android:duration="500" />
</animation-list>
I have looked high and low and cannot find an answer to this.
Edit
Here is the code that creates the image view and sets all its resources:
public ImageView findDevicesButton(){
bottomButton = new ImageView(this);
int id = bottomButton.generateViewId();
bottomButton.setId(id);
if(currentapiVersion >= 11){
bottomButton.setImageResource(R.drawable.animationxmladddevice);
//Background image
bottomButton.setBackgroundResource(R.drawable.bottom_bar);
saveButtonAnimation = (AnimationDrawable)bottomButton.getDrawable();
saveButtonAnimation.setEnterFadeDuration(1000);
saveButtonAnimation.setExitFadeDuration(1000);
bottomButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
findDevicesAlertBuilder();
}
});
}else{
bottomButton.setImageResource(R.drawable.bottom_bar_add);
bottomButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
bottomButton.setBackgroundResource(R.drawable.bottom_bar);
bottomButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
findDevicesAlertBuilder();
}
});
}
return bottomButton;
}
This is the back background image:
All together it should look like this with the center button glowing:
So far I learned how animations work and how to set the background of a button according to its state as described here.
Well, I defined an animation:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:interpolator="#android:anim/accelerate_interpolator"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="50"
android:repeatMode="reverse"
android:repeatCount="6"/>
I start the animation in the onClick(View v) method. The problem now is, that the actual click action gets processed before the animation finishes. I know I could use an AnimationListener, but this would not look very nice to me since I'd have to call the actual click processes within the AnimationListener then.
Does anyone know a more skilful way to let a button blink after it gets clicked?
You can use the Selector tag like Follows: Create a New xml file and place it in your drawable folder and name it as shadow_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/ask_footer"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/askfooter_hover" />
<item android:drawable="#drawable/ask_footer" />
</selector>
And then go to that xml in which your Button is declared: And write one attribute in Button
android:background="#drawable/shadow_color"
and you are done.
Mark the answer if you find it usefull..
Define the onclick for the button
button1.setOnClickListener(
new Button.OnClickListener() {
public void onClick (View v){ calcular(1,v); }
}
);
Make your button blink
This makes the images defined in the XML alternate between each other.
public void calcular(final int p,final View v){
MediaPlayer mp = MediaPlayer.create(this, R.raw.click);
mp.start();
//v.setBackgroundResource(R.drawable.dia1btn_stl2);
final TransitionDrawable transition1 =
(TransitionDrawable) v.getBackground();
Handler blinkHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
transition1.startTransition(70);
break;
case 1:
transition1.resetTransition();
break;
}
super.handleMessage(msg);
}
};
for (int i=0; i<6; i++)
{
Message msg = new Message();
if(i % 2 == 0){
msg.what = 0;
}
else{
msg.what=1;
}
blinkHandler.sendMessageDelayed(msg, i*100);
}
/*mCurrentSeries.clear();
if(calcularctrl == 0){
calcularctrl = 1;
dtdodo = new DownloadImageTask(this , p , codacaovalue);
dtdodo.execute("wwwkjhdijdh");
}*/
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
public void run() {
//v.setBackgroundResource(R.drawable.dia1btn_stl2);
mCurrentSeries.clear();
if(calcularctrl == 0){
calcularctrl = 1;
dtdodo = new DownloadImageTask(outer() , p , codacaovalue);
dtdodo.execute("wwwkjhdijdh");
}
try {
this.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public acoesdetalhes outer(){
return acoesdetalhes.this;
}
}, 1000);
}
The XML of the button background
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="#drawable/mes1btn_stl2" />
<item android:drawable="#drawable/mes1btn_prssd2" />
</transition>
This code provided in part by user Alin.
How can I spin a image wheel in an activity on android with the help of touch event? I need some guideline or link of any tutorial.
This is typically done with a couple pieces. This is how I do it in one of my apps. *Note: This is not a smooth wheel, so much as it starts and stops at the top (which was intentional). You can lookup more about Animation on the dev site.
Main XML that has an image:
<ImageView
android:id="#+id/anim_example"
android:src="#drawable/loading_circle"
android:layout_width="30sp"
android:layout_height="30sp"
android:onClick="RunAnimation" />
Here are the parts in code that run the animation
public void RunAnimation(View v)
{
//The onClick method has to be present and must take the above parameter.
StartLoading();
//This will delay the stop for 5 seconds
//Normally you would want to actually have this run based on some other input/data.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
StopLoading();
}
}, 5000);
}
public void StartLoading() {
ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
Animation rotateLoading = AnimationUtils.loadAnimation(this, R.anim.rotate);
refreshImage.clearAnimation();
refreshImage.setAnimation(rotateLoading);
}
public void StopLoading() {
ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
if (refreshImage.getAnimation() != null)
{
refreshImage.clearAnimation();
refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
}
}
anim.rotate:
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="359"
android:duration="2000"
android:repeatMode="restart"
android:repeatCount="-1"
android:pivotX="50%"
android:pivotY="50%">
</rotate>
I use the animation drawable functionality in my class.
I need to generate the sound while each frame of animation is loaded.Whether it is possible or not. I use the following code for animation:
<?xml version="1.0" encoding="utf-8"?>
<animation-list android:id="#+id/handimation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/count3" android:duration="650" />
<item android:drawable="#drawable/count2" android:duration="650" />
<item android:drawable="#drawable/count1" android:duration="650" />
<item android:drawable="#drawable/go" android:duration="650" />
</animation-list>
My Java:
AnimationDrawable frameAnimation;
ImageView animatedimage ;
animatedimage = (ImageView)findViewById(R.id.rcount);
final MyAnimationRoutineStart animationStart =
new MyAnimationRoutineStart();
final MyAnimationRoutineStop animationStop =
new MyAnimationRoutineStop();
animatedimage.setBackgroundResource(R.drawable.spin);
frameAnimation = (AnimationDrawable) animatedimage.getBackground();
Timer animateTimerStart = new Timer(false);
animateTimerStart.schedule(animationStart, 100);
Timer animateTimerStop = new Timer(false);
animateTimerStop.schedule(animationStop, 2500);
class MyAnimationRoutineStart extends TimerTask
{
MyAnimationRoutineStart()
{
}
public void run()
{
// Start the animation (looped playback by default).
frameAnimation.start();
}
}
class MyAnimationRoutineStop extends TimerTask
{
MyAnimationRoutineStop()
{
}
public void run()
{
frameAnimation.stop();
// stop the animation (looped playback by default).
videoName=camcorderView.startRecording();
counter = 0;
counterFlag = true;
counterThread.start();
}
}
I don't know where to generate the sound.
Any one suggest some idea.
Thanks.
I achieved that using a handler. Inside the handler, I start and play the sound and delay the handler message for the duration of each animation frame. It is working fine.