deSelby
deSelby

Reputation: 1407

Controlling Animation Speed

My app has a small web view that I want to animate into place once the page has finished loading. My code looks like this:

view.setVisibility(View.VISIBLE);
TranslateAnimation anim=new TranslateAnimation(0.0f, 0.0f,
                                  view.getLayoutParams().height, 0.0f);
anim.setDuration(5);
anim.setInterpolator(new LinearInterpolator());
view.startAnimation(anim);

It animates all right, but I can't seem to control the speed. I've set the value for setDuration() to 5, 500, 5000, 5000000 - all to no discernible effect.

Is there something else I need to do to control animation duration?

BTW my base SDK is 1.6.

Upvotes: 1

Views: 11760

Answers (2)

deSelby
deSelby

Reputation: 1407

Here's what I've found in my testing.

  1. This may or may not be specific to WebViews. My original code was copied from an example that animated a LinearLayout.
  2. Setting an animation's duration has no impact if you're trying to use setVisibility(View.GONE) and setVisibility(View.Visible) on the WebView. The only way I could make the animation work properly was to disable those methods on the WebView.
  3. Setting an animation's duration has no impact if you create the animation in Java, regardless of what you do with setVisibility. Again, this may only apply to WebViews.
  4. The only way I could get the animation to work on a WebView was eliminate the calls to setVisibility and to use an XML-based animation as suggested by primalpop in the previous answer.

Again, YMMV and this may only apply to WebViews.

Upvotes: 0

Primal Pappachan
Primal Pappachan

Reputation: 26525

I use XML approach for setting animation for a layout.

translate.xml

<?xml version="1.0" encoding="utf-8"?>
<set
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >  
      <translate 
        android:fromYDelta="-100%" 
        android:toYDelta="0"
        android:duration="500" />"
       />
</set>

To control the animation duration, you could try changing android:duration ="500" to values you require.

layout_anim_controller.xml

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
        android:animationOrder="reverse"
        android:animation="@anim/translate" />

Setting the Animation for the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
   > 

    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layoutAnimation="@anim/layout_anim_controller"
    />

</LinearLayout>

Maybe this helps a little bit in the right direction.

Upvotes: 1

Related Questions