SurfaceView not animating - android

i have created a surfaceView and attached an alpha fade in and fade out animation but it doesnt seem to animate.
Here is my animation code:
fadeOut = new AlphaAnimation(1.0f, 0.0f);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(1000);
fadeIn = new AlphaAnimation(0.0f, 1.0f);
fadeIn.setInterpolator(new AccelerateDecelerateInterpolator());
fadeIn.setDuration(1000);
animation = new AnimationSet(false);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
//this.setAnimation(animation);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.REVERSE);
inside my onDraw method i simply call this
super.onDraw(canvas);
this.startAnimation(animation);
Any suggestions?
Thanks

A SurfaceView is a special kind of view. I'm not so familiar with the exact implementation of it, but what I understood is that it creates more or less a hole in your app, and renders the view outside of the normal framework. This causes that it's not possible to do things like animations on it (or at least limits it).
If you really want a fade in animation, I suggest you implement it yourself in your SurfaceView, or you consider extending from View instead of SurfaceView.

Android ICS (v4) added TextureView which supports animations
Because a SurfaceView’s content does not live in the application’s window, it cannot be transformed (moved, scaled, rotated) efficiently. This makes it difficult to use a SurfaceView inside a ListView or a ScrollView. SurfaceView also cannot interact properly with some features of the UI toolkit such as fading edges or View.setAlpha().
To solve these problems, Android 4.0 introduces a new widget called TextureView that relies on the hardware accelerated 2D rendering pipeline and SurfaceTexture. TextureView offers the same capabilities as SurfaceView but, unlike SurfaceView, behaves as a regular view. You can for instance use a TextureView to display an OpenGL scene or a video stream. The TextureView itself can be animated, scrolled, etc.

Related

Android elevated view animation not smooth

I want to rotate a simple imageview which has an elevation of 5dp.
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
animRotate.start();
The animation for the above code is smooth if the android:elevation value for the ImageView is not set in the layout file. But when i set the elevation, the animation becomes jerky.
Can someone please suggest a fix?
Maybe the reason is that you create and run animatuion at once. As docs say, it is better first to init your animation
//OnCreate
animRotate=ObjectAnimator.ofFloat(imgProgress, "rotationY", 0, 360);
animRotate.setDuration(ANIM_DURATION);
animRotate.setRepeatCount(5);
And then when it is time for animation to be fired run it
animRotate.start();
Also, consider reading about what PivotX and PivotY are, it may be useful.
Also, using default interpolator will give strange result for rotating 5 times - i think using simple linear interpolator is much better choice.

flipping a tile in android

In my layout, I am having a set of tiles(Something similar to windows phone home screen) .I am trying to flip the tile to show more content. I have taken a look at card flipping, but then it flips the entire fragment.I want it to flip a part of the whole layout.I have tried rotating it using Object animator .Here is my code..
View v = findViewById(R.id.iotd_relative);
ObjectAnimator animation = ObjectAnimator.ofFloat(v, "rotationY", 0.0f, 360f);
animation.setDuration(3600);
animation.setRepeatCount(ObjectAnimator.INFINITE);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.start();
Is there any possibility of calling another layout when it flips?
I would recommend using a ScaleAnimation, heres a good example that simulates a Card Flip, you should be able to get what you want from there.
Use Android's scale animation to simulate a 3D flip...

Multiple views and animations lagging

I have two RelativeLayout views. The first one has about three children. The second one has only the first one view as its child. I have two animations, fadeIn and translateIn, which must be applied to the views firstView and secondView respectively. The code below is working. They start and end at the same time, but it's lagging. It's not so smooth. Is there any thing I can do to run them simultaneously and smooth?
Here's the code:
private static final int duration = 500;
protected static boolean ANIMATION_FINISHED;
protected static void onShowAnimation() {
int width;
ANIMATION_FINISHED = false;
width = getScreenWidth();
final AlphaAnimation fadeIn = new AlphaAnimation(0, 1);
final TranslateAnimation translateIn = new TranslateAnimation(-width, 0, 1, 1);
fadeIn.setDuration(duration);
translateIn.setDuration(duration);
firstView.startAnimation(translateIn);
secondView.startAnimation(fadeIn);
secondView.postDelayed(new Runnable() {
#Override
public void run() {
ANIMATION_FINISHED = true;
}
}, duration);
}
animations up to gingerbread always been pretty laggy in Android. In Honeycomb and forward they fixed it with the new framework, but then the developers (that's you) must know which framework to use.
to have a nice smooth hardware accelerated animation you should use the android.animation not the android.view.animation
Here you have the complete guide for the property animation framework: https://developer.android.com/guide/topics/graphics/prop-animation.html
and here is the base class for it:
https://developer.android.com/reference/android/animation/package-summary.html
if you need to target devices on gingerbread or below, I suggest you to use the NineOldAndroid library that automatically handles the best possible framework for the device. (Remembering it will still be laggy on 2.3, but at least 3.0+ will be smooth).
ps.: You shouldn't use a postDelayed to know when the animation is over, instead use an Animation Listener
After searching a lot finally i successfully remove lagging during animation.
you need to set,
yourAnimatedView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
before starting animation, and after completion of animation you need to set,
yourAnimatedView.setLayerType(View.LAYER_TYPE_NONE, null);
During animations your views may be redrawn each frame. If you use view layers, instead of having to redraw each frame, views render once into an off-screen buffer which can be reused.
In addition, hardware layers are cached on the GPU, which makes certain operations during animation much faster. Simple transformations (translation, rotation, scaling and alpha) can be rendered quickly with layers. Since many animations are just a combination of these transformations, layers can supercharge animation performance

Android fadeAnimation works partially

I'm trying to make an animation on a layout that has been previously rotated using RotateAnimation. The animation i want to do are fadeIn and FadeOut depending of the situation
aLayout = (LinearLayout) _context.findViewById(R.layout.layoutId);
AlphaAnimation fadeIn = new AlphaAnimation(0, 1.0f);
AlphaAnimation fadeOut = new AlphaAnimation(1.0f, 0);
fadeIn.setDuration(500);
fadeOut.setDuration(500);
fadeIn.setFillAfter(true);
fadeOut.setFillAfter(true);
Depending of the situation i apply :
aLayout.startAnimation(fadeIn);
or
aLayout.startAnimation(fadeOut);
I've check and the animations aren't trying to start at the same time. The behaviour is that my layout is partially fadeIn.
Instead of having 'invisible part' and then 'visible part'
i only got part of the layout 'invisible part' to 'in le rt'.
It seems totally random that's why i'm asking you in case you have any idea of where it can come from. Before the rotation this alpha stuff works well but once i do it i start having this unexpected behaviour
I'm working from 2.2 to 4.1 Any help would be appreciated. Thanks
Edit : Can't figure this out. Anyone ?
I use a fadeout animation using this:
public void fadeout(final View view) {
// Start Fade Animation
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(fadeOut);
view.startAnimation(animation);
}
and I call it in the onCreate after setting the Content view of the activity that I want to fade out after grabbing the layout root:
View layoutRoot = findViewById(R.id.splashscreen);
fadeout(layoutRoot);
As for the rotation part you can call the animation in the onConfigurationChanged
Seems it's a bug on android. Couldn't figure how to solve it.
Anyway, i found another way to do this. Instead of making the Layout rotating i rotate only the UIElement i needed to be rotate and create the element i want to animate twice. One for the vertical position the other for the horizontal one.
This is the only way i found

Leaving an Android Animation at it's end state

I'm trying to do an animation that takes a button (with a custom background image and system text) and fades it out. That works fine, actually. The issue is after the animation it goes back to it's initial state. I want it to animate and stay that way.
Thanks!
AlphaAnimation anim = new AlphaAnimation(1, 0.2f);
anim.setDuration (5000);
textView.startAnimation (anim);
anim.setFillAfter(true);
That should work.
My answer is 'stolen' form android.View transparency

Categories

Resources