使用加速度计移动对象

时间:2014-07-06 12:41:57

标签: android android-canvas

你好:)我创建了一个带球的黄色帆布,现在我正在尝试移动球,但我不知道如何,这是我第一次使用加速度计,这是我第一次使用帆布和画画..你能帮助我搬家吗?

到目前为止,这是我的代码:

     package com.example.theball;

import android.support.v4.widget.DrawerLayout.LayoutParams;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.SensorEvent;
import android.os.Bundle;
import android.view.Display;
import android.widget.ImageView;
import android.widget.RelativeLayout;

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.fragment_main);
    draw();
    }
@SuppressWarnings("deprecation")
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); 
    ImageView imageView = new ImageView(this);
    imageView.setImageBitmap(bitmap);
    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);
    setContentView(layout);   
}
public void onSensorChanged(SensorEvent event)
{

}

}

1 个答案:

答案 0 :(得分:3)

已解决:这是新代码,感谢stackoverflow中的某人:

    package com.example.theball;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;

@SuppressWarnings("deprecation") public class MainActivity extends Activity implements SensorEventListener {
     private SensorManager sensorManager;
        private Sensor accelerometer;
        private long lastUpdate;

        AnimatedView animatedView = null;
        ShapeDrawable mDrawable = new ShapeDrawable();
        public static int x;
        public static int y;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // setContentView(R.layout.activity_main);

            sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            accelerometer = sensorManager
                    .getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            lastUpdate = System.currentTimeMillis();

            animatedView = new AnimatedView(this);
            setContentView(animatedView);
        }

        @Override
        protected void onResume() {
            super.onResume();
            sensorManager.registerListener(this, accelerometer,
                    SensorManager.SENSOR_DELAY_GAME);
        }

        @Override
        protected void onPause() {
            super.onPause();
            sensorManager.unregisterListener(this);
        }


        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            // TODO Auto-generated method stub
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

                x -= (int) event.values[0];
                y += (int) event.values[1];

            }
        }

        public class AnimatedView extends ImageView {

            static final int width = 50;
            static final int height = 50;

            public AnimatedView(Context context) {
                super(context);
                // TODO Auto-generated constructor stub

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

            }

            @Override
            protected void onDraw(Canvas canvas) {

                mDrawable.setBounds(x, y, x + width, y + height);
                mDrawable.draw(canvas);
                invalidate();
            }
        }

    }