如何在CustomView中代替陀螺仪使用陀螺仪传感器

时间:2020-09-15 08:41:23

标签: java android

我正在尝试开发像Doodle Jump这样的游戏。在该游戏中,您将手机移至另一侧,而玩家则移至手机移至的一侧。我使用陀螺仪传感器,因为好像应该这样。 我还使用android图形,并在“自定义”视图而不是“活动”中编写所有代码,因为我无法在“活动”中使用图形,因此我只在视图中编写代码并替换“活动”内容

所以基本上我创建了一个名为Gyroscope的类,它可以满足我所需的所有功能:

public class Gyroscope {

public interface Listener{
    void onRotation(float rx, float ry, float rz);
}
private Listener listener;

public void setListener(Listener l){
    listener = l;
}

private SensorManager sensorManager;
private Sensor sensor;
private SensorEventListener sensorEventListener;

Gyroscope(Context context){
    sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
    sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            if (listener != null){
                listener.onRotation(sensorEvent.values[0],sensorEvent.values[1],sensorEvent.values[2]);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {

        }
    };
}

public void register(){
    sensorManager.registerListener(sensorEventListener,sensor,sensorManager.SENSOR_DELAY_NORMAL);
}

public void unregister(){
    sensorManager.unregisterListener(sensorEventListener);
}

现在要使用此功能,我需要使用“暂停”和“恢复”功能,但它仅适用于活动,而且我不知道如何将其用于视图,因此我将所有类都附加到了项目中,以便您可以更清晰地理解它< / p>

public class  CustomView extends View {
//Screen
Context context;
float screenWidth, screenHeight;

//Player
Sprite spritePlayer;
Bitmap player_Image;

//Floor
Sprite spriteFloor;

//Others
Intent intent;

public CustomView(Context context, int w, int h) { super(context);
    //Screen
    this.context = context;
    screenWidth = w;
    screenHeight = h;

    //Player
    player_Image = BitmapFactory.decodeResource(context.getResources(), R.drawable.player);
    spritePlayer = new Sprite(screenWidth/2-150,screenHeight-900,300,600,player_Image);

    //Floor
    spriteFloor = new Sprite(0,screenHeight-300,screenWidth,200,null);

    //Others
    intent.putExtra("spritePlayer",spritePlayer);
};





protected void onDraw(Canvas canvas) {
    spritePlayer.draw(canvas);
    spriteFloor.draw(canvas);
}

}

public class MainActivity extends AppCompatActivity {
CustomView cv;
public Gyroscope gyroscope;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    cv = new CustomView(this, size.x, size.y);
    setContentView(cv);

    gyroscope = new Gyroscope(this);

    gyroscope.setListener(new Gyroscope.Listener() {
        @Override
        public void onRotation(float rx, float ry, float rz) {
            if(ry > 1){


            }
            else if(ry < -1){

            }

        }
    });
}


@Override
protected void onResume() {
    super.onResume();
    gyroscope.register();
}

@Override
protected void onPause() {
    super.onPause();
    gyroscope.unregister();
}

}

class Sprite {
protected float x, y;
protected float w, h;
protected float dx,dy;
Bitmap pic;
Sprite(){
    this.x = 0;
    this.y = 0;
    this.w = 0;
    this.h = 0;
}
Sprite(float x, float y, float w, float h, Bitmap bmp) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
    if(bmp != null){
        this.pic = Bitmap.createScaledBitmap(bmp, (int)w,(int)h, true);
    }
}

protected Sprite(Parcel in) {
    x = in.readFloat();
    y = in.readFloat();
    w = in.readFloat();
    h = in.readFloat();
    dx = in.readFloat();
    dy = in.readFloat();
    pic = in.readParcelable(Bitmap.class.getClassLoader());
}

public void setX(float x) {
    this.x = x;
}

public void setY(float y) {
    this.y = y;
}

public void setW(float w) {
    this.w = w;
}

public void setH(float h) {
    this.h = h;
}

public float getX() {
    return x;
}

public float getY() {
    return y;
}

public float getW() {
    return w;
}

public float getH() {
    return h;
}

public void draw(Canvas canvas) {
    Paint p = new Paint();
    p.setColor(Color.GREEN);
    if ( pic != null)
        canvas.drawBitmap(pic, x,y, p);
    else
        canvas.drawRect ( x, y, x + w, y + h, p);
}

public void move(float dx, float dy) {
    x += dx;
    y += dy;
}

public boolean contains(float dx, float dy){
    if (dx >= this.x && dx <= this.x + this.w && dy >= this.y && dy <= this.y + this.h){
        return true;
    }
    return false;
}

public float getCenter(){
    return this.getX()+this.getW()/2;
}

public void aa(){

}

}

0 个答案:

没有答案