2011-04-07 42 views
0

我已經從另一個教程中構建了我的初始代碼,以創建類來處理繪圖到SurfaceView。本教程(以及我發現的其他人)將xml定義的SurfaceView關聯到創建活動時引發的類。我的目標是在我的佈局中按下一個按鈕時,在xml定義的表面視圖中啓動動畫,並在我再次按下時終止它。如何通過按鈕啓動xml定義的表面視圖?

此外,動畫必須在我的xml(而不是整個顯示器)中指定的區域內運行。

爲了簡單起見,我目前只是在surfaceview中繪製一個圓,直到我弄清楚如何讓我的佈局工作。所以,當我點擊Go按鈕(BattleActivity :: onClick)時,我的表面視圖被創建並繪製,但它填充了覆蓋我的佈局中其他控件(包括按鈕)的整個顯示。我只想在我的佈局(id = battleView)的一部分定義的表面視圖中進行動畫。

這是我的XML佈局代碼(tabBattle.xml)的一個小節。

<LinearLayout 
    android:orientation="vertical" 
    android:gravity="center" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <SurfaceView 
     android:id="@+id/battleView" 
     android:layout_weight="70" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal"/>   

    <Button 
     android:onClick="onClick" 
     android:text=" Go " 
     android:gravity="center" 
     android:layout_weight="30" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 

</LinearLayout> 

這是我的主要活動課。這是在我導航到其佈局(tabBattle)所在的選項卡視圖時創建的。當我按下Go按鈕時,onClick被調用。我在onClick的意圖是獲取表面視圖,創建一個新的BattleView實例,並將表面視圖上下文傳遞給BattleView構造函數。這顯然是不正確的,但我不知道如何做到這一點。

公共類BattleActivity延伸活動實現View.OnClickListener { @Override 公共無效的onCreate(捆綁savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.tabbattle); }

@Override 
public void onClick(View v) 
{ 
    SurfaceView battleSurface = (SurfaceView)findViewById(R.id.battleView); 
    setContentView(new BattleView(battleSurface.getContext())); 
} 

}

SurfaceView亞類:

公共類BattleView延伸SurfaceView實現SurfaceHolder.Callback { 私人詮釋circleRadius; 私人畫圈circlePaint; UpdateThreadBattle updateThread;

private int width; 
private int height; 
private int xPos; 
private int yPos; 
private int xVel; 
private int yVel; 

public BattleView(Context context) 
{ 
    super(context); 
    getHolder().addCallback(this); 

    circleRadius = 10; 
    circlePaint = new Paint(); 
    circlePaint.setColor(Color.BLUE); 

    xVel = 2; 
    yVel = 2;  
} 

@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawColor(Color.WHITE); 
    canvas.drawCircle(xPos, yPos, circleRadius, circlePaint); 
} 


@Override 
public void surfaceCreated(SurfaceHolder holder) 
{ 
    Rect surfaceFrame = holder.getSurfaceFrame(); 
    width = surfaceFrame.width(); 
    height = surfaceFrame.height(); 

    xPos = width/2; 
    yPos = circleRadius; 

    updateThread = new UpdateThreadBattle(this); 
    updateThread.setRunning(true); 
    updateThread.start(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder arg0) 
{ 
    boolean retry = true; 

    updateThread.setRunning(false); 

    while (retry) { 
     try { 
      updateThread.join(); 
      retry = false; 
     } catch (InterruptedException e) { 

     } 
    } 
} 

@Override 
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
    // TODO Auto-generated method stub 

} 

}

正如你可能已經懷疑,我一般AMM新到Android和Java。任何幫助是極大的讚賞!

格雷格

回答

0

最終的溶液我選擇是利用一個計時器,而不是實現基於線程的表面動畫這是矯枉過正的簡單動畫我在視圖中必需的。當時我寫了一個我不知道計時器的問題。它很好,brw!

相關問題