android - 添加一个新的 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;
}
}
...
解决方案
以与在 xml 文件中使用 ImageView 相同的方式使用 BubbleView,添加如下内容:
<BubbleView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/icon"
android:id="@+id/bubble_view"
/>
推荐阅读
- php - php - 比较一个数组的索引和另一个数组的值
- javascript - 如何允许用户点击行
- java - Apache httpclient HTTPGET 解码
- r - 在 R 中创建求和公式
- java - 双向链表,在Java中的给定节点之前插入
- java - 如何以 N 个计数作为参数选择前 N 个记录
- xamarin.forms - 在 Xamarin 表单中调整活动指示器的大小
- ldap - LDAP 身份验证过滤器以验证哪个员工属于哪个组
- javascript - 如何从时钟选择器中获取价值并将其传递给我的表单 f.date_field?
- javascript - 有没有更简单的方法来更新复选框价格?