Java 按下时将图像移动到随机的X、Y坐标。安卓工作室
我在学校开发了一个应用程序作为一个项目,我想要一个图像在按下时将位置更改为随机位置。这就是我到目前为止所做的Java 按下时将图像移动到随机的X、Y坐标。安卓工作室,java,android-studio,Java,Android Studio,我在学校开发了一个应用程序作为一个项目,我想要一个图像在按下时将位置更改为随机位置。这就是我到目前为止所做的 Random rand = new Random(); int nX = rand.nextInt(20); int nY = rand.nextInt(20); private Drawable sprite; public DrawView(Context context, int xRes, int yRes){ sprite = context.getResourc
Random rand = new Random();
int nX = rand.nextInt(20);
int nY = rand.nextInt(20);
private Drawable sprite;
public DrawView(Context context, int xRes, int yRes){
sprite = context.getResources().getDrawable(R.drawable.andre8bit);
}
public OnClickListener imageViewListener = new OnClickListener() {
}
我不知道在监听器中要做什么。在监听器中,您必须覆盖onClick方法,再次生成一个随机的x,y,并将它们设置为您想要移动的图像的位置 例子:
您将需要另一种方法来删除绘制的精灵,然后在新位置再次绘制它。因此,首先调用RemoveDrawn(),然后调用DrawView。或者,在这种情况下,您可以恢复顺序,这对人类来说太快了。在这里,您必须覆盖onclick方法,再次生成随机的x,y,并将它们设置为要移动的图像的位置。不需要创建这些方法:)尽管效果很好,谢谢!:)
buttonMoveImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nX = rand.nextInt(20);
nY = rand.nextInt(20);
// set the image coordinates...
}
});