Android的循环问题问题、Android

2023-09-07 12:57:14 作者:卑微的戏子。

我是新到Android而不是编程。并低于code使的icon.png是默认提供在Android中,走在屏幕上,或至少它应该是。这是行不通的。谁能帮助?

 >包com.android.test;进口> android.app.Activity;进口> android.content.Context;进口> android.graphics.Bitmap;进口> android.graphics.BitmapFactory;进口> android.graphics.Canvas;进口> android.graphics.Color;进口> android.os.Bundle;>>>进口android.view.View;进口> android.view.Window;>>公共类Android的扩展活动> {INT X,Y = 10;>> @覆盖公共无效的onCreate(捆绑> savedInstanceState){> super.onCreate(savedInstanceState);> requestWindowFeature(Window.FEATURE_NO_TITLE);>的setContentView(新面板(本)); }>>类面板扩展视图{市民>面板(上下文的背景下){>超级(上下文); }>> @覆盖公共无效的onDraw(帆布>帆布){位图_scratch => BitmapFactory.de codeResource(getResources()> R.drawable.icon);> canvas.drawColor(Color.BLACK); INT> N = 1;而(N == 1){{尝试>视频下载(30); }赶上> (InterruptedException的E){}> canvas.drawBitmap(_scratch,X,Y,>空值); X + = 2; Y + = 2;>> canvas.drawBitmap(_scratch,X,Y,>空值); }}}} 

解决方案

您的onDraw()方法永远不会返回。它需要。请问您的应用程序组,几秒钟后关闭?你将不得不发布消息到UI线程后30毫秒,而不是内的onDraw循环()重绘。关闭我的头顶,试试这个来代替:

 类面板扩展视图{  公共面板(上下文的背景下){    超级(上下文);  }  INT X = 0;  INT Y = 0;  位图_scratch = BitmapFactory.de codeResource(getResources(),R.drawable.icon);  @覆盖公共无效的onDraw(帆布油画){    canvas.drawColor(Color.BLACK);    canvas.drawBitmap(_scratch,X,Y,NULL);    X + = 2; Y + = 2;    this.postInvalidateDelayed(30);  }} 
Android Studio常见问题以及解决方式

当然,你需要添加一些code停止循环,一旦图标已经关闭的边缘。

I am new to android but not to programming. And the code below makes the icon.png that is available by default in android, go across the screen or at least it is supposed to. This doesn't work. Can anyone help?

> package com.android.test; import
> android.app.Activity; import
> android.content.Context; import
> android.graphics.Bitmap; import
> android.graphics.BitmapFactory; import
> android.graphics.Canvas; import
> android.graphics.Color; import
> android.os.Bundle;
> 
> 
> import android.view.View; import
> android.view.Window;
> 
> public class Android extends Activity
> { int x,y=10;
> 
> @Override public void onCreate(Bundle
> savedInstanceState) {
> super.onCreate(savedInstanceState);
> requestWindowFeature(Window.FEATURE_NO_TITLE);
> setContentView(new Panel(this)); }
> 
> class Panel extends View { public
> Panel(Context context) {
> super(context); }
> 
> @Override public void onDraw(Canvas
> canvas) { Bitmap _scratch =
> BitmapFactory.decodeResource(getResources(),
> R.drawable.icon);
> canvas.drawColor(Color.BLACK); int
> n=1;  while (n==1){ try {
> Thread.sleep(30);  } catch
> (InterruptedException e) { }
> canvas.drawBitmap(_scratch, x, y,
> null);  x+=2; y+=2;
> 
> canvas.drawBitmap(_scratch, x, y,
> null);  } } } }

解决方案

Your onDraw() method never returns. It needs to. Does your app Force Close after a few seconds? You're going to have to post a message to the UI thread to redraw after 30 milliseconds instead of looping within onDraw(). Off the top of my head, try this instead:

class Panel extends View {
  public Panel(Context context) {
    super(context);
  }

  int x = 0;
  int y = 0;
  Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

  @Override public void onDraw(Canvas canvas) {
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(_scratch, x, y, null);
    x+=2; y+=2;
    this.postInvalidateDelayed(30);
  }
}

Of course you need to add some code to stop looping once the icon has gone off the edge.