添加一个新的Class文件,并且不再涉及其他任何内容

时间:2019-06-16 07:01:16

标签: android class imageview

我尝试了一个教程:像学习儿童一样有趣地学习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;
        }
    }

   ...

1 个答案:

答案 0 :(得分:1)

完全像在xml文件中使用ImageView一样使用BubbleView 添加类似这样的内容:

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