本文介绍了在Android的移动圈子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个任务。这是画一些(不止一个)圆在屏幕上移动。他们必须开始点击它们后移动。我有code只为一个圆。给我的路该怎么做这个任务,例如,5圈。在此先感谢!
公共类MainActivity延伸活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(新MyView的(这个));
}
一流的MyView的扩展视图{
//公共油漆℃;
公共涂料P;
私有静态最终诠释半径= 46;
私人诠释的centerX;
私人诠释centerY;
私人诠释speedX = 50;
私人诠释SPEEDY = 40;
//专用涂料粉刷; //Создайегогде-нибудьтамвконструкторе
公共MyView的(上下文的背景下){
超(上下文);
P =新的油漆();
p.setColor(Color.GREEN);
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldW,诠释oldH){
的centerX =瓦特/ 2;
centerY = H / 2;
}
保护无效的OnDraw(帆布C){
INT W =的getWidth();
INT H =的getHeight();
的centerX + = speedX;
centerY + =快捷;
INT rightLimit = W - RADIUS;
INT bottomLimit = H - RADIUS;
如果(的centerX> = rightLimit){
的centerX = rightLimit;
speedX * = -1;
}
如果(的centerX< = RADIUS){
的centerX = RADIUS;
speedX * = -1;
}
如果(centerY> = bottomLimit){
centerY = bottomLimit;
快速* = -1;
}
如果(centerY< = RADIUS){
centerY = RADIUS;
快速* = -1;
}
c.drawCircle(的centerX,centerY,RADIUS,P);
postInvalidateDelayed(200);
}
}
}
解决方案
您必须改变这一部分:
私人诠释的centerX;
私人诠释centerY;
私人诠释speedX = 50;
私人诠释SPEEDY = 40;
和其转换到
类圆{
私人诠释的centerX;
私人诠释centerY;
私人诠释speedX = 50;
私人诠释SPEEDY = 40;
//添加构造函数在这里和其他的东西
};
然后让你的圈子集合:的ArrayList<圈>圈子然后,而不是
和
的centerX + = speedX;
centerY + =快捷;
INT rightLimit = W - RADIUS;
INT bottomLimit = H - RADIUS;
如果(的centerX> = rightLimit){
的centerX = rightLimit;
speedX * = -1;
}
如果(的centerX< = RADIUS){
的centerX = RADIUS;
speedX * = -1;
}
如果(centerY> = bottomLimit){
centerY = bottomLimit;
快速* = -1;
}
如果(centerY< = RADIUS){
centerY = RADIUS;
快速* = -1;
}
您需要做的是为每一个圈,像
为(i = 0; I< circles.size();我++){
圈子[I] .centerX + =圈子[I] .speedX;
圈子[I] .centerY + =圈子[I] .speedY;
//итакдалее...
}
I have a task. It's to draw some (more than one) circles moving around the screen. They must start moving after click on them. I have the code only for one circle. Give me the way how to do this task, for example, 5 circles. Thanks in advance!
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View {
//public Paint c;
public Paint p;
private static final int RADIUS = 46;
private int centerX;
private int centerY;
private int speedX = 50;
private int speedY = 40;
//private Paint paint; // Создай его где-нибудь там в конструкторе
public MyView(Context context) {
super(context);
p = new Paint();
p.setColor(Color.GREEN);
}
@Override
protected void onSizeChanged(int w, int h, int oldW, int oldH) {
centerX = w / 2;
centerY = h / 2;
}
protected void onDraw(Canvas c) {
int w = getWidth();
int h = getHeight();
centerX += speedX;
centerY += speedY;
int rightLimit = w - RADIUS;
int bottomLimit = h - RADIUS;
if (centerX >= rightLimit) {
centerX = rightLimit;
speedX *= -1;
}
if (centerX <= RADIUS) {
centerX = RADIUS;
speedX *= -1;
}
if (centerY >= bottomLimit) {
centerY = bottomLimit;
speedY *= -1;
}
if (centerY <= RADIUS) {
centerY = RADIUS;
speedY *= -1;
}
c.drawCircle(centerX, centerY, RADIUS, p);
postInvalidateDelayed(200);
}
}
}
解决方案
you have to change this part:
private int centerX;
private int centerY;
private int speedX = 50;
private int speedY = 40;
and convert it to the
class Circle {
private int centerX;
private int centerY;
private int speedX = 50;
private int speedY = 40;
// add constructor here and other things
};
then make a collection of your circles: ArrayList<Circle> circles
, and then instead of
centerX += speedX;
centerY += speedY;
int rightLimit = w - RADIUS;
int bottomLimit = h - RADIUS;
if (centerX >= rightLimit) {
centerX = rightLimit;
speedX *= -1;
}
if (centerX <= RADIUS) {
centerX = RADIUS;
speedX *= -1;
}
if (centerY >= bottomLimit) {
centerY = bottomLimit;
speedY *= -1;
}
if (centerY <= RADIUS) {
centerY = RADIUS;
speedY *= -1;
}
you have to do it for every circle, like
for( i=0; i<circles.size(); i++) {
circles[i].centerX += circles[i].speedX;
circles[i].centerY += circles[i].speedY;
// и так далее...
}
这篇关于在Android的移动圈子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!