I am a real newbie when it comes to android programming. I am trying to figure out how to change view on the fly or draw circles from the main activity.
I have tried to change view but i failed. So now i am trying to figure out how i should paint a circle from my main activity after the client clicks on a button
private void InitiateGame(String name, String password){
Log.d("InitiatingGame", "Initiating Game");
NetworkHandler networkHandler = new NetworkHandler(HOST, PORT);
PlayerHandler playerHandler = new PlayerHandler();
MessageHandler messageHandler = new MessageHandler(networkHandler, playerHandler);
networkHandler.connect(name, password);
final GameHandler zombieView = new GameHandler(networkHandler, messageHandler, playerHandler);
nameField.post(new Runnable()
{
@Override
public void run()
{
setMainScreenVisibility(View.INVISIBLE);
}
});
initiated = true;
}
This is the code that is called after the client have clicked on the "Connect" Button. So he will instanciate some classes that you guys dont need to know what they are doing. Then he connects to the server. So i do not want to make new intent's.
What is better. Create a new view class that extends view that i set as contentview?(if so. how?) Or should i just try to draw these circles from this main activity? (also how?)
To draw a circle using paint
, you need to create a custom view like this
private class CircleView extends View{
Paint paint = new Paint();
public CircleView(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
// set your own position and radius
canvas.drawCircle(100,200,100,paint);
}
}
And, then add an Parent Layout to the Activity
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
setContentView(relativeLayout);
Finally you need to add the circle view
to the Parent Layout
relativeLayout.addView(new CircleView(this));
Another way is to create the circle as drawable xml
and set it to an ImageView
Create a circle.xml in drawable/
And then , set the drawable in ImageView
in your layout
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/circle"/>
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments