我想做像视差这样的事情,所以我使用G传感器 并希望用它移动图像。
我的问题是我猜的只有旧的教程 谷歌改变了什么?
public class ShowWallpaper extends AppCompatActivity implements SensorListener {
ImageView imageViewWallpaper;
ImageButton ib_back;
Button setWallpaper;
TextView tv_text;
RelativeLayout.LayoutParams lp;
private Sensor mySensor;
private SensorManager SM;
public static float x = 0;
public static float y = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_show_wallpaper);
imageViewWallpaper = findViewById(R.id.showCustom);
ib_back = findViewById(R.id.ib_back);
setWallpaper = findViewById(R.id.btnSetWallpaper);
tv_text = findViewById(R.id.tv_test);
SM = (SensorManager) getSystemService(SENSOR_SERVICE);
mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
SM.registerListener(this,SensorManager.SENSOR_DELAY_GAME);
Bundle extras = getIntent().getExtras();
Picasso.with(getApplicationContext()).load(extras.getString("SRC")).into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
imageViewWallpaper.setImageBitmap(bitmap);
setWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WallpaperManager wpm = (WallpaperManager) getSystemService(WALLPAPER_SERVICE);
try {
wpm.setBitmap(bitmap);
Toast.makeText(ShowWallpaper.this, "Hintergrund wurde gesetzt.", Toast.LENGTH_SHORT).show();
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
ib_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void onSensorChanged(int i, float[] floats) {
x -= floats[0];
y += floats[1];
imageViewWallpaper.setX(x);
imageViewWallpaper.setY(y);
/*
//imageViewWallpaper.setX(Math.round(floats[0]) /2);
//imageViewWallpaper.setY(Math.round(floats[1]) /2);
imageViewWallpaper.setX(imageViewWallpaper.getHeight()/2);
imageViewWallpaper.setY(imageViewWallpaper.getWidth()/2);
if(Math.round(floats[0]) < 0){
lp.setMargins(0,0,Math.round(floats[0]*10),Math.round(floats[1]*10));
}else {
lp.setMargins(Math.round(floats[0]*10),Math.round(floats[1]*10),0,0);
}
imageViewWallpaper.setLayoutParams(lp);*/
tv_text.setText("X - " + String.valueOf(floats[0]) + " Y - " + floats[1]);
}
@Override
public void onAccuracyChanged(int i, int i1) {
}
}
使用此代码时,图像会移开。我尝试了这个网站的大部分答案,但没有工作。
我的G传感器可能无法正常工作吗?
由于