Android 如何在此代码中使用球图像

Android 如何在此代码中使用球图像,android,Android,我是android游戏开发的新手。 请帮助我找到如何在此代码中创建球图像。 在web上发布的教程的帮助下,我开发了一个示例应用程序。我的目标是访问加速计并根据手机方向移动球。 我已经提到一个问题 public class Accelerometer extends Activity implements SensorEventListener { /** Called when the activity is first created. */ CustomDrawableVie

我是android游戏开发的新手。 请帮助我找到如何在此代码中创建球图像。 在web上发布的教程的帮助下,我开发了一个示例应用程序。我的目标是访问加速计并根据手机方向移动球。 我已经提到一个问题

public class Accelerometer extends Activity implements SensorEventListener
{
    /** Called when the activity is first created. */
    CustomDrawableView mCustomDrawableView = null;
    ShapeDrawable mDrawable = new ShapeDrawable();
    public static int x;
    public static int y;

    private SensorManager sensorManager = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        // Get a reference to a SensorManager
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        mCustomDrawableView = new CustomDrawableView(this);
        setContentView(mCustomDrawableView);
        // setContentView(R.layout.main);

    }

    // This method will update the UI on new sensor events
    public void onSensorChanged(SensorEvent sensorEvent)
    {
        {
            if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
                // the values you were calculating originally here were over 10000!
                x = (int) Math.pow(sensorEvent.values[1], 2); 
                y = (int) Math.pow(sensorEvent.values[2], 2);

            }

            if (sensorEvent.sensor.getType() == Sensor.TYPE_ORIENTATION) {

            }
        }
    }

    // I've chosen to not implement this method
    public void onAccuracyChanged(Sensor arg0, int arg1)
    {
        // TODO Auto-generated method stub

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        // Register this class as a listener for the accelerometer sensor
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);
        // ...and the orientation sensor
        sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
                SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onStop()
    {
        // Unregister the listener
        sensorManager.unregisterListener(this);
        super.onStop();
    }

    public class CustomDrawableView extends View
    {
        static final int width = 50;
        static final int height = 50;

        public CustomDrawableView(Context context)
        {
            super(context);

            mDrawable = new ShapeDrawable(new OvalShape());
            mDrawable.getPaint().setColor(0xff74AC23);
            mDrawable.setBounds(x, y, x + width, y + height);
        }

        protected void onDraw(Canvas canvas)
        {
            RectF oval = new RectF(Accelerometer.x, Accelerometer.y, Accelerometer.x + width, Accelerometer.y
                    + height); // set bounds of rectangle
            Paint p = new Paint(); // set some paint options
            p.setColor(Color.BLUE);
            canvas.drawOval(oval, p);
            invalidate();
        }
    }
}
试试这个

最好在Oncreate中创建位图

Bitmap bitmap =  BitmapFactory.decodeResource(getResources(), R.drawable.ball_image);
在OnDraw函数中使用该位图,并将x、y设置为加速计值

 protected void onDraw(Canvas canvas)
    {
         canvas.drawBitmap(bitmap,Accelerometer.x, Accelerometer.y,null);

    }

为什么在onDraw中调用
invalidate
?这是一个糟糕的做法。@Simon,在
onDraw
方法中创建位图是错误的worse@Simon,@vmironov我刚才在这里指出了如何显示图像。。。我不能写完整的代码。。不管怎样,谢谢