I've patterned my initial code from another tutorial to create classes to handle drawing to a SurfaceView. This tutorial (and others I've found) associate an xml-defined SurfaceView to a class which intiates upon creation of the activity. My goal is to initiate animation in an xml-defined surface view upon pushing a button in my layout and terminating it when I push it again.
Furthermore, the animation must run inside the region I've specified in my xml (rather than the entire display).
For simplicity, I'm currently only drawing a circle in the surfaceview until I figure out how to get my layout working.
So, when I click the Go button (BattleActivity::onClick), my surface view is created and drawn, but it fills the entire display covering the other controls in my layout, including the button. I want the animation to take place only in the surface view defined as part of my layout (id=battleView).
Here's a sub-section of my XML layout code (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>
Here's my main activity class. This is created when I navigate to the tab view where its layout (tabBattle) resides. The onClick is called when I push the Go button. My intention in onClick is to get the surface view, create a new BattleView instance, and and pass the surface view context to the BattleView constructor. This clearly is not correct but I have no idea how else to do this.
public class BattleActivity extends Activity implements View.OnClickListener { @Override public void onCreate(Bundle 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 subclass:
public class BattleView extends SurfaceView implements SurfaceHolder.Callback { private int circleRadius; private Paint circlePaint; UpdateThreadBattle updateThread;
private int width;
private int height;
private int xPos;
private int yPos;
private int xVel;
private int yVel;
pu开发者_JAVA百科blic 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
}
}
As you probably have suspected, I amm new to android and java in general. Any help is greatly appreciated!
Greg
The eventual solution I chose was to utilize a timer rather than implement a thread-based surface animation which was overkill for the simple animation I required in a view. At the time I wrote the question I didn't know about the timer. It works great, brw!
精彩评论