I have a problem with android:duration attribute in my xml file.
That is my code:
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item
android:drawable="#drawable/eye_1"
android:duration="150" />
<item
.
.
.
</animation-list>
There are 8 images.
Here is my Activity code:
public class MyActivity extends Activity {
AnimationDrawable frameAnimation;
private MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sauron);
mp = MediaPlayer.create(this, R.raw.sound);
mp.setLooping(true);
mp.start();
ImageView imgView = (ImageView) findViewById(R.id.animationImage);
// imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.drawable.animation);
frameAnimation = (AnimationDrawable) imgView
.getBackground();
// Get instance of Vibrator from current Context
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(10000);
}
#Override public void onWindowFocusChanged(boolean hasFocus) {
frameAnimation.start();
super.onWindowFocusChanged(hasFocus); }
#Override
protected void onStop()
{
// Stop play
super.onStop();
mp.stop();
}
}
Everything works fine but there is a problem with duration between images. No matter what number i put in android:duration attribute, animation runs very fast. Does anyone know where is a problem?
Thank you
the duration is in "milliseconds" meaning 150 is is VERY VERY fast
when 1500 is 1.5 seconds and 150 means 0.15 seconds which is just over 1 tenth of a second
try putting a number in thousands, like 1500 or 3000 and see if that works
Related
Im very new to java and android programming.
I am trying to create a button that when pressed, a text will appear at the location of the click and immediately fade out.
At the moment, the problem is that when clicked - the text does appear, but the animation doesn't play, and the text disappears after a few seconds (just like I want).
I have edited this question a few times down bellow, following some progress on solving this problem with great help from the guys replying. Thanks!
The code I tried is this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mClickMe = (Button) findViewById(R.id.button1);
mRelativeLayout = (RelativeLayout) findViewById(R.id.rl);
mClickMe.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int xClickPos = (int) event.getRawX();
int yClickPos = (int) event.getRawY();
final TextView scorePop = new TextView(getApplicationContext());
scorePop.setText("+10");
scorePop.setX(xClickPos);
scorePop.setY(yClickPos);
scorePop.setVisibility(TextView.INVISIBLE);
// Create the score pop animation
Animation scoreAnimation = new AlphaAnimation(1, 0);
scoreAnimation.setDuration(1000);
scoreAnimation.setAnimationListener (new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
scorePop.setVisibility(TextView.VISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
mRelativeLayout.removeView(scorePop);
}
});
mRelativeLayout.addView(scorePop);
scorePop.setAnimation(scoreAnimation);
scoreAnimation.start();
}
return false;
}
});
}
EDIT: I have also noticed that if the button is clicked quickly on different spots, the text appears at those spots and disappears with a "choppy" fade. Yet still, if clicked once - the text appears and disappears but no animation.
Thanks for the help!
EDIT 2: With the help I got here it seems the problem has something to do with the device. I am using Nexus 4, and running an emulator to test my apps as Nexus 4 as well. Oddly, running emulator for another device such as Galaxy Nexus seemed to solve the issue.
EDIT:
I was able to recreate your original bug and it had something to do with setX(),setY(). After executing either of those methods the TextView gets "numb" and won't animate. The workarround I figured was to set the layout's margins not the TextView's coordinates:
// Set the margins instead of the actual position
ViewGroup.MarginLayoutParams params=(ViewGroup.MarginLayoutParams)scorePop.getLayoutParams();
params.leftMargin=xClickPos;
params.topMargin=yClickPos;
On my device Galaxy Nexus it is showing animation fine on first time also. May be your device takes time here "mRelativeLayout.addView(scorePop);" Add a little delay after that and then start animation It should run fine.
try it...
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fadein);
txtMessage = (TextView) findViewById(R.id.txtMessage);
btnStart = (Button) findViewById(R.id.btnStart);
// load the animation
animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_in);
// set animation listener
animFadein.setAnimationListener(this);
// button click event
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtMessage.setVisibility(View.VISIBLE);
// start the animation
txtMessage.startAnimation(animFadein);
}
});
Is it possible to change the picture of the button with a sequence of images from the drawable continuously for a specific time on the click of the button , I know little about frame animation , is it possible to apply frame animation for changing the pictures of the button ? If not is there any other way of doing it?
please consider frame animation:
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.image1), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.image2), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.image3), 1000);
animation.setOneShot(false);
Button btnAnimation = (Button) findViewById(R.id.myBtn);
btnAnimation.setBackgroundDrawable(animation);
//In OnCreate or button_click event you can fire animation
animation.start()
I stole the answer from the this.
If you want to change the background for every click You can call the following :
private int pics[]= {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5};
private Random rand = new Random();
public int set_rand_pic() {
int pos = rand.nextInt(pics.length-1);
mycard.setBackgroundResource(pics[pos]);
return pos;
}
Hope it helps.
you can use Countdown Timer.
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
//Change the button Bachground here!
}
public void onFinish() {
mTextField.setText("done!");
//set the button background that you want to show on end
}
}.start();
thats all :) enjoy
i am enable to load animation in Emulater..Its working fine with any real device..
public class MainActivity extends Activity {
private ImageView imgView;
private Animation animation;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.grow);
animation.setRepeatCount(50); // Repeat animation infinitely
animation.setRepeatMode(Animation.REVERSE);
imgView = (ImageView) findViewById(R.id.imgView);
imgView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgView.startAnimation(animation);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent it = new Intent(getApplicationContext(), MyWebView.class);
startActivity(it);
}
}, 5000);
}
});
}
and my anim xml file is following
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="50" />
I think you Might have Disabled Animations in Emulator:
Check that:
Settings>Display>Animation..
Hope that helps your Problem
For anyone running into similar issues when using ObjectAnimator, in my case I had to enable the developer settings on the emulator and then go into "Settings" > "Developer options" > "Drawing" section
In this "Drawing" section you'll find different options for each animation type, in my case the "Animator duration scale" was off, after setting it to "1x" I started seeing the animations on the emulator.
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);
}
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.