I am doing some UT with Robolectric3.2.1. During onCreate() of my TestActivity, I start an anmation like this:
protected void onCreate(Bundle savedInstanceState){
...
mImageView.startAnimation(createAnim());
...
}
the method that creat an animationSet:
private AnimationSet createAnim(){
AnimationSet scaleAndAlphaAnim = (AnimationSet)AnimationUtils.loadAnimation(this, R.anim.radar_scale_alpha_anim);
scaleAndAlphaAnim.setInterpolator(new LinearInterpolator());
for (Animation anim : scaleAndAlphaAnim.getAnimations()) {
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(1500L);
}
return scaleAndAlphaAnim;
}
and the animation file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.75"
android:fromYScale="0.75"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
after I run code:
Robolectric.setupActivity(TestActivity.class);
The main thread gets stuck into endless loop at mImageView.startAnimation(createAnim()).But the code performs well on my real devices. Now I am confused. Can anybody tell me why?
Related
I have anim file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="60000"
android:fillAfter="true"
android:interpolator="#android:anim/linear_interpolator"
android:shareInterpolator="false" >
<rotate
android:startOffset="0"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</set>
and I want animation to start immediately. But is starts after 2-3 sec delay. What can be the reason?
Its working properly in my code. So there are no error in XML. Please check your Java. My Java code is:
void startAnim() {
view.clearAnimation();
Animation anim= AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim);
view.startAnimation(anim);
}
private ImageView logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logo=(ImageView)findViewById(R.id.logo);
Animation myanim = AnimationUtils.loadAnimation(this,R.anim.anim);
logo.startAnimation(myanim);
}
I used this animation blink.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000"
android:repeatCount="infinite"
android:interpolator="#android:anim/accelerate_interpolator"
/>
</set>
I called it like this to stop it after 5 seconds:
mainThreadhanlder.post(new Runnable() {
#Override
public void run() {
view.startAnimation(blinkAnim);
}
});
mainThreadhanlder.postDelayed(new Runnable() {
#Override
public void run() {
view.clearAnimation();
}
}, 5000);
but the problem is the animation is not working on the downside of the textView: The downside of the 00 can be shown that the animation is not working proporely !
so, what can be the source of the problem ?
Just add Padding In your TextView
<TextView
...
android:padding="5dp"/>
I have used animation on an activity in my game where user sees his score in form of stars(maximum score gets 3 stars). I have used animation on each of the stars(using startAnimation() method). The golden stars gets placed on the blank grey colored stars with translation, scale and alpha animation(I have used set animation).
But whenever that score activity starts the animation gets displayed with jerkiness/lag/unsmoothness.
Whats the solution to make that animation smooth and timely?
Following is the java code to dislplay 3 out of 3 stars
protected void onCreate(Bundle savedInstanceState)
{
............
Myanim1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_scale_left);
Myanim2= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.aplha_scale_mid);
Myanim3= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha_scale_right);
......}
if(pattern_Score>targetScore*90/100)
{
mCountDownTimer = new CountDownTimer(1000,100) {
#Override
public void onTick(long millisUntilFinished) {
starIv1.setImageResource(R.drawable.star_gray);
}
#Override
public void onFinish() {
starIv1.startAnimation(Myanim1);
starIv1.setImageResource(R.drawable.star);
Toast.makeText(getApplicationContext(), "timer1 stopped", Toast.LENGTH_SHORT).show();
}
};
mCountDownTimer.start();
starIv1.clearAnimation();
mCountDownTimer1=new CountDownTimer(2000,100) {
#Override
public void onTick(long millisUntilFinished) {
starIv2.setImageResource(R.drawable.star_gray);
}
#Override
public void onFinish() {
//starIv2.clearAnimation();
starIv2.startAnimation(Myanim2);
starIv2.setImageResource(R.drawable.star);
}
};
mCountDownTimer1.start();
starIv2.clearAnimation();
mCountDownTimer2=new CountDownTimer(3000,100) {
#Override
public void onTick(long millisUntilFinished) {
starIv3.setImageResource(R.drawable.star_gray);
}
#Override
public void onFinish() {
//starIv3.clearAnimation();
starIv3.startAnimation(Myanim3);
starIv3.setImageResource(R.drawable.star);
}
};
mCountDownTimer2.start();
starIv3.clearAnimation();....}
alpha_scale_left.xml(animation on left star)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%"
android:fromXDelta="-20%"
android:toYDelta="0"
android:toXDelta="0"
android:duration="600"/>
<scale
android:fromXScale="3000%"
android:fromYScale="3000%"
android:toXScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="900"
/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="600" />
</set>
alpha_scale_mid.xml(animation on middle star)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%"
android:fromXDelta="-5%"
android:toYDelta="0"
android:toXDelta="0"
android:duration="600"/>
<scale
android:fromXScale="3000%"
android:fromYScale="3000%"
android:toXScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="0%"
android:duration="600"
/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="600" />
</set>
alpha_scale_right.xml(animation on right star)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="-100%"
android:fromXDelta="5%"
android:toYDelta="0"
android:toXDelta="0"
android:duration="600"/>
<scale
android:fromXScale="3000%"
android:fromYScale="3000%"
android:toXScale="1"
android:toYScale="1"
android:pivotX="50%"
android:pivotY="0%"
android:duration="600"
/>
<alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="600" />
</set>
I don't understand the meaning of 3000% in your xmls try changing in the range of ~100%
Looking at the code I can suggest you to use some threads and not load all the animations on the main thread and you should rather use Asynctask and based on some progress you may use onprogressupdate method to make changes to the UI thread.
OR
Graphics and Animations are handled by the GPU or in more common
term the Processor of your device. If not rendered properly then it
could possibly be the result of a processor with low clock rate.
I have no idea for this animation.
How can I do it via XML like that? Or another solution?
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/accelerate_decelerate_interpolator"
android:fillAfter="true">
......
</set>
Thanks for your help
This code shakes a view in horizontal direction
shake.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:interpolator="#anim/cycle_5"
android:toXDelta="10" />
cycle_5.xml
<?xml version="1.0" encoding="utf-8"?>
<cycleInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:cycles="5" />
Method to shake ImageView
public void onShakeImage() {
Animation shake;
shake = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shake);
ImageView image;
image = (ImageView) findViewById(R.id.image_view);
image.startAnimation(shake); // starts animation
}
1) vibrate or
2) shake
(using property animation)the following code working for me.
ObjectAnimator rotate = ObjectAnimator.ofFloat(animateView, "rotation", 0f, 20f, 0f, -20f, 0f); // rotate o degree then 20 degree and so on for one loop of rotation.
// animateView (View object)
rotate.setRepeatCount(20); // repeat the loop 20 times
rotate.setDuration(100); // animation play time 100 ms
rotate.start();
Create shake.xml inside anim directory
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="70"
android:fromDegrees="0"
android:interpolator="#android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="5"
android:repeatMode="reverse"
android:toDegrees="0" />
<translate
android:duration="70"
android:fromXDelta="40"
android:interpolator="#android:anim/linear_interpolator"
android:repeatCount="5"
android:repeatMode="reverse"
android:toXDelta="-40" />
inside your java file add below method
public void animateView(View view){
Animation shake = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
view.startAnimation(shake);
}
and pass your view inside method for animation
animateView(yourView);
Create animation file in anim directory:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="200"
android:fromDegrees="-10"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="10" />
Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
your_view.startAnimation(shake);
This works for me smoothly as i Expected
private void Shake(View view)
{
RotateAnimation rotate = new RotateAnimation(-5, 5,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(250);
rotate.setStartOffset(50);
rotate.setRepeatMode(Animation.REVERSE);
rotate.setInterpolator(new CycleInterpolator(5));
view.startAnimation(rotate);
}
I did create the BUZZ like animation for imageView. Here I also have added delay to make it really like Buzzy effect
In your Activity:
Here, animShake is android.view.animation
animShake = AnimationUtils.loadAnimation(this, R.anim.shake)
imageView.startAnimation(animShake)
Don't forget to add AnimationListener for little delay of BUZZ effect
animShake.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationRepeat(animation: Animation?) {
}
override fun onAnimationEnd(animation: Animation?) {
Handler().postDelayed({
imageView.startAnimation(animShake)
}, 1000)
}
override fun onAnimationStart(animation: Animation?) {
}
})
anim shake XML file having only translate:
android:duration="75"
android:fromXDelta="-18%"
android:repeatCount="11"
android:repeatMode="reverse"
android:toXDelta="18%" />
I still try different basics with android, and now I'm stuck with animation. I'm trying to implement a simple animation. I've defined animation in xml file like this:
alpha android:interpolator="#android:anim/accelerate_interpolator" android:fromAlpha="0.0" android:toAlpha="1.0" duration="3000" repeatCount="infinite"
In my main view group I have an ImageView defined like this:
<ImageView android:id="#+id/someb" android:src="#drawable/earth_normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip"/>
And this is from my starting activity class:
public class Ohayou extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView earth = (ImageView)findViewById(R.id.someb);
Animation earthFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
earth.startAnimation(earthFadeInAnimation);
}
It finds ImageView successfuly and creates animation. but when I start emulator ImageView just shows the original src image, not an animation. What am I doing wrong?
Thanks
I see you have a correct code but I have doubt in your \anim\fade_in.xml, so try this
<?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="3000" android:repeatCount="infinite"/>
</set>