0

I have followed this tutorial (http://stuffthathappens.com/blog/2008/11/13/android-animation-101/) and have a beautiful rotating text being drawn onto a canvas. Everything works perfectly when my main activity uses

setContentView(new SplashScreenAnimation(this));

Here is SplashScreenAnimation:

public class SplashScreenAnimation extends View {
    private static final String QUOTE = "Nobody uses Java anymore. It's this big heavyweight ball and chain.";

    private Animation anim;

    public SplashScreenAnimation(Context context) {
        super(context);
    }

    private void createAnim(Canvas canvas) {
        anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
                .getHeight() / 2);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(10000L);
        anim.setInterpolator(new AccelerateDecelerateInterpolator());

        startAnimation(anim);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // Creates the animation the first time
        if (anim == null) {
            createAnim(canvas);
        }

        Path circle = new Path();

        int centerX = canvas.getWidth() / 2;
        int centerY = canvas.getHeight() / 2;
        int r = Math.min(centerX, centerY);

        circle.addCircle(centerX, centerY, r, Direction.CW);
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setTextSize(30);
        paint.setAntiAlias(true);

        canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
    }
}

However I want to combine this animated text and have two buttons underneath it so I have a RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:background="@drawable/splash_screen">
 <LinearLayout android:orientation="vertical"
  android:layout_height="wrap_content" android:layout_width="fill_parent"
  android:layout_alignParentBottom="true" android:id="@+id/bottomLinearLayout">
  <Button android:layout_gravity="center_horizontal" android:id="@+id/playButton"
   android:text="Button" android:layout_height="wrap_content"
   android:layout_width="wrap_content"></Button>
  <Button android:layout_height="wrap_content"
   android:layout_width="wrap_content" android:id="@+id/optionsButton"
   android:text="Button" android:layout_gravity="center_horizontal"
   android:layout_marginBottom="30sp"></Button>
 </LinearLayout>


 <LinearLayout android:id="@+id/topLinearLayout"
  android:orientation="vertical" android:layout_height="wrap_content"
  android:layout_alignParentTop="true" android:layout_width="fill_parent">
 </LinearLayout>

</RelativeLayout>

How can I add this custom view to the LinearLayout above? (topLinearLayout - it's the last one)

I've tried a lot of different things, and I keep ending up with force closes.

This sort of approach has been mainly what I've tried. I've tried inflating it, etc.

LinearLayout item = (LinearLayout)findViewById(R.id.topLinearLayout);
SplashScreenAnimation child = new SplashScreenAnimation(this);
item.addView(child);

I added entire SplashScreenAnimation class code:

((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))

It works!!

Why won't the other methods of adding it manually work? Has it something to do with the fact the animation needs to start via onDraw?

3
  • 1
    what have you tried? have you tried ((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this)) Commented Sep 19, 2011 at 14:41
  • hehe ur welcome .. should i add it as an answer? and you accept? and everyone is happy? :P Commented Sep 19, 2011 at 15:06
  • Yes, please add it as an answer. Oddly enough when I cleared a bunch of code and used the exact approach i specified above it worked.. So i guess something funny was going on. Thanks :) Commented Sep 20, 2011 at 3:39

3 Answers 3

1

Try this line to add the view:

((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))
Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to include it just like any other view in your layout.

Try the following and then set your content view to the entire XML layout:

<LinearLayout android:id="@+id/topLinearLayout"
  android:orientation="vertical" android:layout_height="wrap_content"
  android:layout_alignParentTop="true" android:layout_width="fill_parent">
      <com.YOURPACKAGENAME.SplashScreenAnimation
           android:id="@+id/animation"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"/>
 </LinearLayout>

1 Comment

Would this approach work if it was a SurfaceView instead?
0

It's pretty easy, and you just need to include the full package name of the class extending the View class:

<LinearLayout
    android:id="@+id/topLinearLayout"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_width="fill_parent">

       <mypackage.SplashScreenAnimation
           android:layout_height="wrap_content"
           android:layout_width="fill_parent"/>

</LinearLayout>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.