2011-09-19 32 views
0

我按照本教程(http://stuffthathappens.com/blog/2008/11/13/android-animation-101/)繪製了畫布上的美麗旋轉文字。一切都完美的作品時,我的主要活動採用如何在Android上的現有XML佈局中包含自定義視圖?

setContentView(new SplashScreenAnimation(this)); 

這裏是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); 
    } 
} 

但是我想這個動畫文字相結合,有它的下面兩個按鈕,所以我有一個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> 

如何將此自定義視圖添加到上面的LinearLayout? (topLinearLayout - 這是最後一個)

我已經嘗試了很多不同的東西,並且我一直以強制關閉結束。

這種方法主要是我嘗試過的。我試過它膨脹等

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

我加了整個SplashScreenAnimation類代碼:

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

它的作品!

爲什麼手動添加它的其他方法不起作用?這與動畫需要通過onDraw開始的事實有關嗎?

+1

你嘗試過什麼?你有沒有嘗試過'((LinearLayout)findViewById(R.id.topLinearLayout))。addView(new SplashScreenAnimation(this))' –

+0

Sherif工作!!!!! – jducreux

+0

赫赫你歡迎..我應該添加它作爲答案?你接受嗎?大家都開心嗎? :P –

回答

1

試試這個行添加視圖:

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

您應該可以像佈局中的任何其他視圖一樣包含它。

請嘗試以下,然後設置您的內容查看整個XML佈局:

<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> 
+0

是的,我試過這個。我得到部隊關閉:( – jducreux

+0

如果它是一個SurfaceView而不是這個方法的工作嗎? – trans

1

這是很容易的,你只需要包括延長View類的類的完整包名:

<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> 
+0

是的我已經試過這個,我得到力關閉:( – jducreux

相關問題