【问题标题】:How to create a ball in Android?如何在 Android 中创建一个球?
【发布时间】:2012-10-17 21:13:18
【问题描述】:

我一直在尝试在 Eclipse 中创建一个球。我试图找出在this post 中要做什么,但我似乎不知道在我的 Ball 课上放什么。有什么建议吗?

【问题讨论】:

    标签: android eclipse


    【解决方案1】:

    如果你决定改变主意,试试这个。

    public class MainActivity extends Activity {
    private int c = Color.YELLOW;
    private Canvas g;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        draw();
        }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    
    // draws the ball
    public void draw ()
    {
        Display display = getWindowManager().getDefaultDisplay();
    
        int width = display.getHeight();
        int height = display.getWidth();
    
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.RGB_565);
    
        g = new Canvas(bitmap);
        g.drawColor(c);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        g.drawCircle(50, 10, 25, paint); //Put your values
    
        // In order to display this image, we need to create a new ImageView that we can display.
        ImageView imageView = new ImageView(this);
    
        // Set this ImageView's bitmap to ours
        imageView.setImageBitmap(bitmap);
    
        // Create a simple layout and add imageview to it.
        RelativeLayout layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        layout.addView(imageView, params);
        layout.setBackgroundColor(Color.BLACK);
    
        // Show layout in activity.
        setContentView(layout);   
    }
    }
    

    【讨论】:

      【解决方案2】:

      这是一个简单的 java 类,您可以从中创建一个球对象:

      public class Ball {
      
          private int x, y, r;
          private Color c = Color.YELLOW;
      
          public Ball (int x, int y, int r)
          {
              this.x = x;
              this.y = y;
              this.r = r;
          }
      
          // draws the ball
          public void draw (Graphics g)
          {
              g.setColor(c);
              g.fillOval(x-r, y-r, 2*r, 2*r);
          }   
      
      }
      

      【讨论】:

      • 谢谢!不过,我遇到了一些问题: Color.YELLOW;得到一个错误,说“类型不匹配:不能从 int 转换为 Color”,并且 g.setColor 得到一个错误,说“Graphics 类型中的方法 setColor(Color) 不适用于参数 (Color)” Color.YELLOW要我将 c 更改为 int,而 setColor 依赖于变量 c。我应该将 c 更改为 int 吗?
      【解决方案3】:

      我知道渲染球体的唯一方法是在 openGl 中.. 看看here

      对于 2D 渲染,您可能会看到这个 tut .. 它非常简单 .. Link

      【讨论】:

      • 谢谢,但我正在使用 Marcin 给我的代码。不过,我有一些错误。你知道我该怎么做吗?
      • 您要画一个带效果的圆还是球体?
      • 我正在尝试画一个圆,并实现重力
      猜你喜欢
      • 2015-09-02
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      • 2017-09-24
      • 2020-07-19
      相关资源
      最近更新 更多