0
我正试图在屏幕上做一个简单的球移动。我将球作为位图,并且我为x和y值设置了字符串。我有一个小的代码说,如果x小于宽度加10,直到它更大,然后重置为0。但是球根本不在屏幕上移动。为什么这个位图不能在整个屏幕上动画? Android App
为什么不会我的球移动?
`package com.example.spader.gamer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
/**
* Created by Spader on 2/10/2016.
*/
public class drawingTheBall extends View {
Bitmap bBall;
int x, y;
public drawingTheBall(Context context) {
super(context);
bBall = BitmapFactory.decodeResource(getResources(), R.drawable.bball);
x = 0;
y = 0;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth(), canvas.getHeight()/2);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
canvas.drawRect(ourRect, blue);
if (x < canvas.getWidth()){
x += 10;
}else{
x = 0;
}
if (y < canvas.getHeight()){
y += 10;
}else{
y = 0;
}
Paint p = new Paint();
canvas.drawBitmap(bBall, x, y, p);
invalidate();
}
}
`
感谢您的答复。我对编程有点新,所以你能帮我理解你的回复吗?如何使我的位图一遍又一遍地发生抽取方法,因此图像被重新定位? – Zach
好吧,如果你是新的总体规划的,我会建议,而不是下面的,将让你与Java和Android熟悉的初学者的教程。你要做的事情需要一些现有的知识,这超出了StackOverflow问题的范围。 –