Java 我的android应用程序没有';即使没有';这不是一个错误

Java 我的android应用程序没有';即使没有';这不是一个错误,java,android,xml,Java,Android,Xml,我只想随机画100个彩色圆圈,但我看到一个圆圈闪烁了大约半秒,它就消失了。我试着自己解决了3个小时,但找不到任何线索。尝试约束布局等。。 请注意,没有错误。 有什么问题?我以为是因为布局的关系,我现在还不完全明白。帮助一个小绵羊编码员 Android java代码: package com.example.seido.bubbledraw0408; import android.content.Context; import android.graphics.Canvas; import an

我只想随机画100个彩色圆圈,但我看到一个圆圈闪烁了大约半秒,它就消失了。我试着自己解决了3个小时,但找不到任何线索。尝试约束布局等。。 请注意,没有错误。 有什么问题?我以为是因为布局的关系,我现在还不完全明白。帮助一个小绵羊编码员

Android java代码:

package com.example.seido.bubbledraw0408;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.support.v7.widget.AppCompatImageView;
import android.widget.ImageView;

import java.util.ArrayList;

/**
 * Created by seido on 2018-04-08.
 */

 public class BubbleView extends AppCompatImageView {

    private ArrayList<Bubble> bubbleList;
    private final int DELAY = 16;
    private Paint myPaint = new Paint(); //allow us to draw
    private Handler h; // like Timer in eclipse
    private int size = 30;

    public BubbleView(Context context, AttributeSet attr){
        super(context, attr);

        bubbleList = new ArrayList<Bubble>();
        myPaint.setColor(Color.WHITE);
        h = new Handler();

    }


    protected void onDraw(Canvas c){

        // just to test the ability to draw
        for(int x = 0; x < 100; x++)
            bubbleList.add(new Bubble((int)(Math.random() * 300),
                    (int)(Math.random() * 400), 50));


        // draw all the bubbles on the screen
        for(Bubble bubble : bubbleList) {
            myPaint.setColor(bubble.color);
            c.drawOval(bubble.x, bubble.y,
                    bubble.x, bubble.y, myPaint);
        }
    }

    public class Bubble {

        public int x;
        public int y;
        public int size;
        public int MAX = 5;
        public int xSpeed;
        public int ySpeed;
        public int color;

        Bubble(int newX, int newY, int newSize){

            x = newX;
            y = newY;
            size = newSize;
            color = Color.argb((int)(Math.random() * 256), (int)(Math.random() * 256) ,
                    (int)(Math.random() * 256), (int)(Math.random() * 256));

            xSpeed = (int)(Math.random() * 2 * MAX - MAX);
            ySpeed = (int)(Math.random() * 2 * MAX - MAX);

            if(xSpeed == 0 || ySpeed == 0) {
                xSpeed = 1;
                ySpeed = 1;
            }

        }

        public void randomMotion() {

            x += xSpeed;
            y += ySpeed;

            if(x <= size/2 || x >= getWidth() - size/2) {
                xSpeed = -1 * xSpeed;
            }
            if(y <= size/2 || y >= getHeight() - size/2) {
                ySpeed = -1 * ySpeed;
            }

        }


    }

}
package com.example.seido.bubbledraw0408;
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Paint;
导入android.os.Handler;
导入android.util.AttributeSet;
导入android.support.v7.widget.AppCompatImageView;
导入android.widget.ImageView;
导入java.util.ArrayList;
/**
*由seido于2018年4月8日创建。
*/
公共类BubbleView扩展了AppCompatImageView{
私有数组列表泡泡列表;
专用最终整数延迟=16;
private Paint myPaint=new Paint();//允许我们绘制
私有处理程序h;//类似于eclipse中的计时器
私有整数大小=30;
公共BubbleView(上下文、属性集属性){
超级(上下文,attr);
bubbleList=新的ArrayList();
myPaint.setColor(颜色:白色);
h=新处理程序();
}
受保护的void onDraw(画布c){
//只是为了测试画画的能力
对于(int x=0;x<100;x++)
添加(新的气泡((int)(Math.random()*300),
(int)(Math.random()*400),50);
//在屏幕上画出所有的气泡
for(气泡:气泡列表){
myPaint.setColor(bubble.color);
c、 drawOval(bubble.x,bubble.y,
气泡.x,气泡.y,myPaint);
}
}
公共阶级泡沫{
公共int x;
公共智力;
公共整数大小;
公共int MAX=5;
公共int xSpeed;
公共信息速度;
公共int颜色;
气泡(int-newX、int-newY、int-newSize){
x=newX;
y=新的;
大小=新闻大小;
color=color.argb((int)(Math.random()*256),(int)(Math.random()*256),
(int)(Math.random()*256),(int)(Math.random()*256));
xSpeed=(int)(Math.random()*2*MAX-MAX);
ySpeed=(int)(Math.random()*2*MAX-MAX);
如果(xSpeed==0 | | ySpeed==0){
xSpeed=1;
y速度=1;
}
}
公开动议({
x+=x速度;
y+=y速度;
如果(x=getWidth()-size/2){
xSpeed=-1*xSpeed;
}
如果(y=getHeight()-size/2){
ySpeed=-1*ySpeed;
}
}
}
}
XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom = "16dp"
android:background="#000000"
tools:context="com.example.seido.bubbledraw0408.BubbleView">

<com.example.seido.bubbledraw0408.BubbleView
    android:layout_width = "match_parent"
    android:layout_height = "match_parent" />

</LinearLayout>

好的,我已经尝试了提供的代码,它按预期工作。尽管如此,我认为以下改进可能会为您的情况提供解决方案

首先,在onDraw方法中填充气泡列表是不好的-此方法只能进行绘图。气泡视图的初始化必须如下所示。另外,请注意,我已经覆盖了视图中所有可能的构造函数,这是一个更好的实践

public BubbleView(Context context) {
    super(context);

    Initialize();
}

public BubbleView(Context context, AttributeSet attr){
    super(context, attr);

    Initialize();
}

public BubbleView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    Initialize();
}

private void Initialize() {
    bubbleList = new ArrayList<Bubble>();
    myPaint.setColor(Color.RED);
    h = new Handler();

    for(int x = 0; x < 100; x++)
        bubbleList.add(new Bubble((int)(Math.random() * 300),
                (int)(Math.random() * 400), 50));
}

这至少会让你的代码变得更好。还有,你给onDraw打过多次电话吗?如果是这样的话,这段代码很可能会解决您的问题,因为您不会向数组中添加100个气泡并不断地绘制它们。

那么您在logcat中没有错误吗?是的,我在logcat中没有错误。请帮助我,大师。您的评论让我深受感动。非常感谢你!下班后,我将尝试你提供的代码。不管怎样,我真的很喜欢作为一个业余爱好者编写代码,因为像你这样愿意帮助别人的人。哦,我刚刚尝试了你的代码,但它也不起作用,我只看到空白的黑屏。你能给我看看你的XML代码吗?
protected void onDraw(Canvas c){
    Log.d("Let me", "draw");

    // draw all the bubbles on the screen
    for(Bubble bubble : bubbleList) {
        myPaint.setColor(bubble.color);
        c.drawOval(bubble.x, bubble.y,
                bubble.x + bubble.size, bubble.y + bubble.size, myPaint);
    }
}