سؤال

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"/>

نصائح أخرى

You could just apply a circle.xml as a drawable to a view.

<!-- circle.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">

<solid 
   android:color="@color/my_fancy_color"/>
</shape>

<!-- a view in one of your layout files -->
<View 
     android:width="120dp"
     android:height="120dp"
     android:background="@drawable/circle" 
/>

The circle.xml can be named anything and show be place in the drawables folder. If you change the name of the xml then reference it with the changed name.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top