首页 > 解决方案 > 添加一个新的 Class 文件并且不再涉及任何其他内容

问题描述

我尝试了一个教程:像孩子一样学习 Java 构建有趣的桌面和移动应用程序我在使用 Android Studio 时遇到了问题。事实上,我所做的只是创建了一个 BubbleView 类:但我不知道我与 xml 文件有什么关系......谢谢

我试图修改一些 main_activity 但它不起作用!

import...

public class  BubbleView extends ImageView implements    View.OnTouchListener {
private ArrayList<Bubble> bubbleList;
private final int DELAY = 16;
private Paint myPaint = new Paint();
private Handler h;

public BubbleView(Context context, AttributeSet attrs) {
    super(context, attrs);
    bubbleList = new ArrayList<Bubble>();
    myPaint.setColor(BLACK);
    h = new Handler();
    this.setOnTouchListener(this);
}

private class Bubble {
    public int x;
    public int y;
    public int size;
    public int color;
    public int xspeed;
    public int yspeed;
    private final int MAX_SPEED = 5;

    public 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() * MAX_SPEED * 2 - MAX_SPEED);
        yspeed = (int) (Math.random() * MAX_SPEED * 2 - MAX_SPEED);
        if (xspeed == 0 && yspeed == 0) {
            xspeed = 1;
            yspeed = 1;
        }
    }

   ...

标签: androidclassimageview

解决方案


以与在 xml 文件中使用 ImageView 相同的方式使用 BubbleView,添加如下内容:

<BubbleView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/icon"
    android:id="@+id/bubble_view"
    />   

推荐阅读