我创建了一个名为ParallaxController的类,用于以不同的速度在三个不同的图像视图上创建视差效果。本课程使用加速计传感器,并在“x'”中以不同的速度移动图像。轴。我的问题在于性能,我通过AsyncTask类在图像视图中加载图像 - Android Developers Guide - 当我在图像视图中使用3个大图像时,当我移动设备时,图像视图移动速度非常慢。
我的代码:
-ParallaxController.java:
public class ParallaxController {
private Activity context;
private View viewBack, viewTop, viewFront;
private float backSpeed, topSpeed, frontSpeed;
private float maxMove;
private SensorManager sensorManager;
private ParallaxSensorEventListener eventListener;
public ParallaxController(Activity context, View viewBack, View viewTop, View viewFront, float backSpeed, float topSpeed, float frontSpeed, float maxMove) {
this.context = context;
this.viewBack = viewBack;
this.viewTop = viewTop;
this.viewFront = viewFront;
this.backSpeed = backSpeed;
this.topSpeed = topSpeed;
this.frontSpeed = frontSpeed;
this.maxMove = maxMove;
sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
eventListener = new ParallaxSensorEventListener();
}
public void startControl() {
sensorManager.registerListener(eventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
SensorManager.SENSOR_DELAY_GAME);
}
public void stopControl() {
sensorManager.unregisterListener(eventListener);
}
private class ParallaxSensorEventListener implements SensorEventListener {
float lastPitch = 0;
public boolean isTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float pitch;
if (isTablet(context)) {
pitch = sensorEvent.values[2];
} else {
pitch = sensorEvent.values[1];
}
if (pitch > 45 || pitch < -45)
return;
float dp = pitch - lastPitch;
if (dp == 0)
return;
float move = (dp * maxMove) / -45.0f;
float maxSpeed = Math.max(backSpeed, Math.max(frontSpeed, topSpeed));
Display display=context.getWindowManager().getDefaultDisplay();
Point size=new Point();
display.getSize(size);
float backX = (move / maxSpeed) * backSpeed;
float topX = (move / maxSpeed) * topSpeed;
float frontX = (move / maxSpeed) * frontSpeed;
if (viewBack != null) {
float backSpace=(viewBack.getWidth()*(viewBack.getScaleX()-1))/2;
if (viewBack.getX() + backX-backSpace > 0 || viewBack.getX() + backX +viewBack.getWidth()+backSpace < size.x)
return;
} else backX=0;
if (viewFront != null) {
float frontSpace=(viewFront.getWidth()*(viewFront.getScaleX()-1))/2;
if (viewFront.getX() + frontX - frontSpace> 0 || viewFront.getX() + frontX +viewFront.getWidth()+frontSpace < size.x)
return;
} else frontX=0;
if (viewTop != null) {
float topSpace=(viewTop.getWidth()*(viewTop.getScaleX()-1))/2;
if (viewTop.getX() + topX - topSpace > 0 || viewTop.getX() + topX +viewTop.getWidth()+topSpace < size.x)
return;
} else topX=0;
if (backX!=0) viewBack.setX(viewBack.getX() + backX);
if (frontX!=0) viewFront.setX(viewFront.getX() + frontX);
if (topX!=0) viewTop.setX(viewTop.getX() + topX);
lastPitch = pitch;
Log.d("TAG",viewBack.getX()+"");
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
}
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ParallaxController pc;
private ImageView iv_0;
private ImageView iv_1;
private ImageView iv_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_0 = (ImageView) findViewById(R.id.iv_0);
iv_1 = (ImageView) findViewById(R.id.iv_1);
iv_2 = (ImageView) findViewById(R.id.iv_2);
setImage(iv_0, R.drawable.a);
setImage(iv_1, R.drawable.b);
setImage(iv_2, R.drawable.c);
scale(iv_0, 1.4f);
scale(iv_1, 1.4f);
scale(iv_2, 1.4f);
pc = new ParallaxController(this,iv_0,iv_1,iv_2, 1 , 3 , 5 , 250);
pc.startControl();
}
private void setImage(ImageView iv, int res){
BitmapWorkerTask task = new BitmapWorkerTask(iv);
task.execute(res);
}
private void scale(View view,float scale){
view.setScaleX(scale);
view.setScaleY(scale);
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
...
}
}
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_0"
android:layout_gravity="center"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_1"
android:layout_gravity="center" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_2"
android:layout_gravity="center"/>
注意:活动始终是LandScape。
谢谢...:)