Я проделал большой прогресс в этом задании. Конечный продукт должен добавить круг в случайный цвет/случайный размер/случайное местоположение после каждого нажатия кнопки (круга). У меня есть еще одна кнопка (прозрачная), которая должна очистить холст. Вот две проблемы, с которыми я столкнулся сейчас. Мои круги не отображаются в случайных местах. Все они начинаются с верхнего левого угла экрана. Вторая проблема заключается в том, что я понятия не имею, как заставить работать ясную кнопку. Все остальное работает, круги случайных кругов в произвольных размерах круга.Рисование случайных кругов в случайных местах в случайных размерах Android
package com.example.randomcircles;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
public class DisplayRandomCircles extends Activity
{
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);
f1 = (FrameLayout) findViewById(R.id.frame);
}
@SuppressLint("WrongCall")
public void doit(View v)
{
int rand = (int) (Math.random() * 60);
switch (v.getId())
{
case (R.id.btn1):
DrawCircle c = new DrawCircle(getApplicationContext());
f1.addView(c);
break;
case (R.id.btn2):
DrawCircle d = new DrawCircle(getApplicationContext());
f1.addView(d);
break;
}
}
}
package com.example.randomcircles;
import java.util.Random;
import android.annotation.SuppressLint;
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
{
int red = (int) (Math.random() * 255);
int green = (int) (Math.random() * 255);
int blue = (int) (Math.random() * 255);
public DrawCircle(Context con)
{
super(con);
}
@SuppressLint("DrawAllocation")
@Override
public void onDraw(Canvas c)
{
super.onDraw(c);
int color = Color.rgb(red, green, blue);
int rand = (int)(Math.random() * 200);
Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setAntiAlias(true);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(100);
p.setColor(color);
p.setStyle(Paint.Style.FILL);
c.drawCircle(rand, rand, rand, p);
clear(c);
}
public void clear(Canvas c)
{
c.drawColor(Color.TRANSPARENT);
}
public void drawColor(int transparent) {
// TODO Auto-generated method stub
}
}
<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>
Мне нужно установить объект в GetWidth() метод делать. Также я устанавливаю drawColor.transparent в моей ясной кнопке? – user3242607
Я просто не могу получить кнопку, чтобы очистить холст от работы? – user3242607
Я добавил еще один класс под названием draw view и расширенный вид, подобный моему классу drawcircle, и переопределил метод ondraw, который нарисовал большой белый круг, действующий так, как будто он очищает холст. Добавлена еще одна addview для этого класса для моей кнопки очистки. Это сработало для меня – user3242607