如何解决使用加速度计在视图内移动图像
要求是使用加速度计传感器在视图内倾斜图像。我不知道如何实现这一目标。我可以使用加速度计在整个屏幕上移动图像,但是如何设置大量图像以在特定视图内移动。任何建议和想法或示例链接将不胜感激。
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","" + y);
}
}
}
代码链接:https://github.com/avik1990/SensorImpl
解决方法
我建议使用 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,null);
invalidate();
希望这是澄清的,如果有任何问题,请告诉我。 另请参阅此代码以对画布上的对象进行动画处理。您可以将加速度计读数混合到球移动 x、y 类/对象/值
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。