【问题标题】:Moving an image inside a view using accelerometer使用加速度计在视图内移动图像
【发布时间】:2021-02-20 12:24:53
【问题描述】:

要求是使用加速度传感器在视图内倾斜图像。我不知道如何实现这一目标。我可以使用加速度计在整个屏幕上移动图像,但是如何设置大量图像以在特定视图中移动。任何建议和想法或示例链接将不胜感激。

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    private SensorManager sensorManager;
    private Sensor accelerometer;

    ImageView mDrawable, ivImg;
    public static int x = 0;
    public static int y = 0;
    TextView textView;

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

        mDrawable = (ImageView) findViewById(R.id.ivImage);
        ivImg = (ImageView) findViewById(R.id.ivImg);
        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        textView = findViewById(R.id.textView);

       /* float centreX = (float) mDrawable.getX() + mDrawable.getWidth() / 2;
        float centreY = (float) mDrawable.getY() + mDrawable.getHeight() / 2;*/
        int mWidth = this.getResources().getDisplayMetrics().widthPixels;
        int mHeight = this.getResources().getDisplayMetrics().heightPixels;

        Log.e("centreX", "" + mWidth);
        Log.e("centreY", "" + mHeight);
        ivImg.setPivotX(100);
        ivImg.setPivotY(100);
    }


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

    }

    @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 onSensorChanged(SensorEvent event) {


        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {

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

           /* mDrawable.setY(y);
            mDrawable.setX(x);
            Log.e("X", "" + x);
            Log.e("y", "" + y);*/

            float x = event.values[0];
            float y = event.values[1];
            if (Math.abs(x) > Math.abs(y)) {
                if (x < 0) {
                    //image.setImageResource(R.drawable.right);
                    textView.setText("You tilt the device right");
                }
                if (x > 0) {
                    //image.setImageResource(R.drawable.left);
                    textView.setText("You tilt the device left");
                }
            } else {
                if (y < 0) {
                    //image.setImageResource(R.drawable.up);
                    textView.setText("You tilt the device up");
                }
                if (y > 0) {
                    //image.setImageResource(R.drawable.down);
                    textView.setText("You tilt the device down");
                }
            }
            if (x > (-2) && x < (2) && y > (-2) && y < (2)) {
                //image.setImageResource(R.drawable.center);
                textView.setText("Not tilt device");
            }

            Log.e("X", "" + x);
            Log.e("y", "" + y);
        }
    }
}

代码链接:https://github.com/avik1990/SensorImpl

【问题讨论】:

    标签: android accelerometer android-sensors


    【解决方案1】:

    我建议使用 Canvas.drawBitmap 和 bitmapFactory 并创建精灵,这将使您可以控制这些图像的坐标,并且您可以更改坐标

    //Create bitmap
    Bitmap  mDrawable;
    int mDrawable_x ,mDrawable_y;
    
    //initiate the position of mDrawable
    mDrawable_x = 320;
    mDrawable_y = 500;
    
    
    // initiate the bitmap  R.drawable.mDrawable is where your mDrawable image reside 
    mDrawable = BitmapFactory.decodeResource(getResources(),R.drawable.mDrawable)
    
    
    // mDrawable is ready for use and draw it on canvas
    
    Canvas.drawBitmap(mDrawable,mDrawable_x,mDrawable_y,null);
    
    
    // animate now you can add the accelerometer readings ( please reduce them with limits )
    // create two fields for x and y directions these will happen in onDraw method.
    
    int move_x , move_y ;
    move_x=1 ; move_y=1;
    
    // update the position  include the invalidate at the end to ensure it redraw 
    
    
    mDrawable_x = mDrawable_x + move_x ;
    mDrawable_y = mDrawable_y + move_y;
    
    Canvas.drawBitmap(mDrawable,mDrawable_x,mDrawable_y,null);
    
    invalidate();
    
    

    希望这很清楚,如果有任何问题,请告诉我。 另请参阅此代码以动画画布上的对象。您可以将加速度计读数混合到球移动 x、y 类/对象/值

    https://github.com/pintspin/ball_animation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多