我正在创建一个Android应用程序,我将向用户显示3D模型。我用rajawali框架来显示3D模型。我想要的是用户应该能够与模型进行交互。用户可以通过向左和向右滑动来旋转3D模型。我能用两根手指实现缩放。但我无法旋转模型。这是我的Activity和Renderer的代码。
public class MainActivity extends AppCompatActivity {
private ScaleGestureDetector mScaleDetector;
private GestureDetector gestureDetector;
MyRenderer renderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SurfaceView surface = new SurfaceView(this);
surface.setFrameRate(60.0);
surface.setRenderMode(ISurface.RENDERMODE_WHEN_DIRTY);
// Add mSurface to your root view
addContentView(surface, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT));
renderer = new MyRenderer(this);
mScaleDetector = new ScaleGestureDetector(this, renderer.scaleListener);
gestureDetector = new GestureDetector(this, renderer.gestureListener);
surface.setSurfaceRenderer(renderer);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mScaleDetector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.e("MotionEvent", "up");
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.e("MotionEvent", "down");
}
//Log.e("TouchEvent", event.getAction() + "");
// Assume all touch events are consumed
return true;
}
class MyRenderer extends Renderer implements OnFPSUpdateListener {
Context context;
private Sphere mEarthSphere;
private DirectionalLight mDirectionalLight;
public final ScaleListener scaleListener;
public final GestureListener gestureListener;
public boolean left, right;
public boolean up, down;
private final DecimalFormat mDFormat;
public MyRenderer(Context context) {
super(context);
this.context = context;
mDFormat = new DecimalFormat("#.##");
scaleListener = new ScaleListener();
gestureListener = new GestureListener();
setFrameRate(60);
setFPSUpdateListener(this);
}
@Override
protected void initScene() {
mDirectionalLight = new DirectionalLight(1f, .2f, -1.0f);
mDirectionalLight.setColor(1.0f, 1.0f, 1.0f);
mDirectionalLight.setPower(2);
getCurrentScene().addLight(mDirectionalLight);
Material material = new Material();
material.enableLighting(true);
material.setDiffuseMethod(new DiffuseMethod.Lambert());
material.setColorInfluence(0);
Texture earthTexture = new Texture("Earth", R.drawable.earthtruecolor_nasa_big);
try {
material.addTexture(earthTexture);
} catch (ATexture.TextureException error) {
Log.d("DEBUG", "TEXTURE ERROR");
}
mEarthSphere = new Sphere(1, 24, 24);
mEarthSphere.setMaterial(material);
getCurrentScene().addChild(mEarthSphere);
getCurrentCamera().setZ(4.2f);
}
@Override
public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
}
@Override
public void onTouchEvent(MotionEvent event) {
}
@Override
protected void onRender(long ellapsedRealtime, double deltaTime) {
super.onRender(ellapsedRealtime, deltaTime);
mEarthSphere.rotate(Vector3.Axis.Y, 1.0);
}
public class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = (float) (mEarthSphere.getScaleX() * detector
.getScaleFactor());
scale = Math.max(0.1f, Math.min(scale, 5.0f));
mEarthSphere.setScale(scale);
return true;
}
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
//Method to rotate the 3D model
return true;
}
}
@Override
public void onFPSUpdate(double fps) {
System.out.println("~fps: " + mDFormat.format(fps));
}
}
}
请告诉我如何通过手势旋转模型。