Я пытаюсь добавить круг после каждого нажатия кнопки на том же экране. Я еще не реализовал другой код кнопки, но он должен очистить экран. Предполагается, что круги находятся в случайном положении/размере/цвете. У меня только первый круг напечатан, когда я нажимаю кнопку. Если я его снова нажму, мое приложение закрывается. Я еще не добавил случайное местоположение/размер, но я не знаю, как сделать каждую кнопку щелчком мыши показать другой круг на экране. Мой код ниже.Как добавить круг после каждой кнопки мыши?
package com.example.randomcircles;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class DisplayRandomCircles extends Activity
{
DrawCircle c;
Canvas d;
FrameLayout f1;
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.activity_display_random_circles);
Button btn1 = (Button) findViewById(R.id.btn1);
Button btn2 = (Button) findViewById(R.id.btn2);
c = new DrawCircle(getApplicationContext());
d = new Canvas();
f1 = (FrameLayout) findViewById(R.id.frame);
}
@SuppressLint("WrongCall")
public void doit(View v)
{
switch (v.getId())
{
case (R.id.btn1):
f1.addView(c);
break;
case (R.id.btn2):
break;
}
}
}
мой Жеребьевка круг код
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawCircle extends View
{
public DrawCircle(Context con)
{
super(con);
}
@Override
protected void onDraw(Canvas c)
{
int min = 0, max = 200;
int num = min + (int)(Math.random()*max);
super.onDraw(c);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(100);
p.setColor(Color.RED);
p.setStyle(Paint.Style.FILL);
c.drawCircle(num, num, num, p);
invalidate();
}
}
мой макет XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".75"
android:orientation="vertical" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:gravity="bottom|center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_weight=".50"
android:onClick="doit"
android:text="@string/Circle" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:layout_gravity="end|bottom"
android:onClick="doit"
android:text="@string/Clear" />
</LinearLayout>
</LinearLayout>
Использовать случайное, чтобы получить местоположение, и создать метод 'addCircle' – jyoon
вы хотите нарисовать ** один ** круг или вы хотите добавить ** новый ** круг при нажатии этой кнопки? –
Я хочу добавить новый круг каждый раз, когда нажимаю кнопку круга – user3242607