gpt4 book ai didi

java - 在随机位置以随机大小绘制随机圆 Android

转载 作者:行者123 更新时间:2023-11-30 09:02:02 25 4
gpt4 key购买 nike

我在这个作业上取得了很大的进步。最终产品需要在每次点击按钮(圆圈)后添加一个随机颜色/随机大小/随机位置的圆圈。我有另一个按钮(清除)需要清除 Canvas 。这是我现在面临的两个问题。我的圈子没有显示在随机位置。它们都从屏幕的左上角开始。第二个问题是我不知道如何让我的清除按钮工作。其他一切正常,随机圆圈颜色随机圆圈大小。

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>

最佳答案

您的随机函数无法在当前 View 上绘制圆圈(很可能是因为您拥有高分辨率设备),您必须获得 View 的高度才能在其上随机绘制圆圈。

    int minRadius = 100;
Random random = new Random();//define this outside you onDraw fucntion
int w = getWidth();
int h = getHeight();

int randX = random.nextInt(w);
int randY = random.nextInt(h);
int randR = minRadius + random.nextInt(100);
...
c.drawCircle(randX, randY, randR, p);

还可以通过在整个 Canvas 上绘制颜色来重置 View

canvas.drawColor(Color.WHITLE);

关于java - 在随机位置以随机大小绘制随机圆 Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26167972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com