如何创建Android的一球? [英] How to create a ball in Android?

查看:194
本文介绍了如何创建Android的一球?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在Eclipse中创建一个球。我试图找出什么在做<一个href=\"http://stackoverflow.com/questions/12904100/Android-app-force-closes-when-painting-a-ball\">this帖子,我似乎无法找出什么把我的球类。有什么建议?

I've been trying to create a ball in Eclipse. I tried to find out what to do in this post and I can't seem to figure out what to put in my Ball class. Any suggestions?

推荐答案

尝试这种情况下,你决定要改变你的想法。

try this in case you decide to change your mind.

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);   
}
}

这篇关于如何创建Android的一球?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆