![](/img/trans.png)
[英]How to make a simple VR video player by Google CardBoard Android SDK and Rajawali
[英]Move camera around using touch event in CardBoard and Rajawali VR Android
目前,我正在開發適用於Android的VR應用程序,該應用程序使用Google CardBoard和Rajawali播放360視頻。 傳感器工作正常,但是我無法使用觸摸來正確拖動場景或相機。 有什么方法可以在此應用程序中啟用觸摸模式嗎?
任何幫助是極大的贊賞! 謝謝。
我從事過同一件事,這就是我所使用的:
首先,看一下Rajawali的ArcballCamera類。 您可以在此處看到如何處理觸摸事件以通過觸摸事件旋轉攝像機。
問題是,當用戶在屏幕上移動時,我不喜歡旋轉的默認行為,因此我根據前一個獨立完成了另一種實現,並直接旋轉了我想要的球體而不是相機,這樣就可以了(所有這些都在我的Renderer類中,順便說一句):
首先,聲明:
private GestureDetector detector; //gesture detector
private ScaleGestureDetector scaleDetector; //scale detector (for zooming)
private GestureListener gListener; //gesture listener
private ScaleListener sListener; //scale listener
private View.OnTouchListener touchListener; //touch events listener
private boolean isRotating; //true if the sphere is rotating
private boolean isScaling; //true if the sphere is scaling
private float xInicial,yInicial; //inicial touch point
//sphere's yaw and pitch, used for rotation
private double yaw,pitch, yawAcumulado=0, pitchAcumulado=0, yawAcumuladoR=0, pitchAcumuladoR=0;
//physical to logical (in 3D world) conversion: screen scroll to sphere rotation
private final double gradosPorBarridoX=120, gradosPorBarridoY=90;
private final double gradosPorPixelYaw, gradosPorPixelPitch;
在渲染器的構造函數中,我開始了inizalization(定時器和控件用於視頻控件視圖,因此請不要注意這些):
DisplayMetrics outMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
gradosPorPixelPitch = gradosPorBarridoY / outMetrics.heightPixels;
gradosPorPixelYaw = gradosPorBarridoX / outMetrics.widthPixels;
addListeners();
...
//from Rajawali ArcballCamera class
private void addListeners(){
((Activity)context).runOnUiThread(new Runnable() {
@Override
public void run() {
gListener = new GestureListener();
sListener = new ScaleListener();
detector = new GestureDetector(context, gListener);
scaleDetector = new ScaleGestureDetector(context, sListener);
touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
scaleDetector.onTouchEvent(event); //see if it is a scale event
//if not, check whether it is a scroll
if (!isScaling) {
detector.onTouchEvent(event);
//or an up motion
if (event.getAction() == MotionEvent.ACTION_UP) {
if (!isRotating) {
//change video control view's visibility
TouchActivity.timer.cancel();
if (TouchActivity.control.getVisibility() == View.INVISIBLE) {
TouchActivity.control.setVisibility(View.VISIBLE);
TouchActivity.timer.start(); //timer is restarted
} else {
TouchActivity.control.setVisibility(View.INVISIBLE);
}
} else {
isRotating = false; //cancel rotation
}
}
}
return true;
}
};
TouchActivity.principal.setOnTouchListener(touchListener);
}
});
}
最后但並非最不重要的一點是,事件偵聽(用於縮放和旋轉):
/**
* called when the rotation starts
* @param x
* @param y
*/
private void startRotation(float x, float y){
xInicial = x;
yInicial = y;
}
/**
* called during the consecutive events of a rotation movement
* @param x
* @param y
*/
private void updateRotation(float x, float y){
float difX = xInicial - x;
float difY = yInicial - y;
yaw= difX * gradosPorPixelYaw;
pitch = difY * gradosPorPixelPitch;
yawAcumulado+=yaw;
pitchAcumulado+=pitch;
}
/**
* event listener. if the user scrolls his finger through the screen, it sends the
* touch event to calculate the sphere's rotation
*/
private class GestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onScroll(MotionEvent event1, MotionEvent event2, float distanceX, float distanceY) {
//starts or updates the rotation with the upcoming event x and y screen values
if(!isRotating) {
startRotation(event2.getX(), event2.getY());
isRotating=true;
return false;
}else{
isRotating = true;
updateRotation(event2.getX(), event2.getY());
return false;
}
}
}
/**
* event listener. Zooms in or out depending on the user's action
*/
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
//zooms in or out according to the scale detector value
@Override
public boolean onScale(ScaleGestureDetector detector) {
if(detector.getScaleFactor()>1){
if(earthSphere.getScaleX()*1.1<120){
earthSphere.setScaleX(earthSphere.getScaleX()*1.1);
earthSphere.setScaleY(earthSphere.getScaleY() * 1.1);
earthSphere.setScaleZ(earthSphere.getScaleZ() * 1.1);
}
}else{
if(earthSphere.getScaleX()*0.9>0.95) {
earthSphere.setScaleX(earthSphere.getScaleX() * 0.9);
earthSphere.setScaleY(earthSphere.getScaleY() * 0.9);
earthSphere.setScaleZ(earthSphere.getScaleZ() * 0.9);
}
}
return true;
}
//the zoom begins
@Override
public boolean onScaleBegin (ScaleGestureDetector detector) {
isScaling = true;
isRotating = false;
return super.onScaleBegin(detector);
}
//the zoom ends
@Override
public void onScaleEnd (ScaleGestureDetector detector) {
isRotating = false;
isScaling = false;
}
}
解決了所有這些問題后,只需在每個渲染器上設置方向,如下所示:
yawAcumuladoR = (yawAcumulado) * 0.04;
pitchAcumuladoR = (pitchAcumulado) * 0.04;
Quaternion q = new Quaternion();
q.fromEuler(yawAcumuladoR, pitchAcumuladoR, 0);
earthSphere.setOrientation(q);
正如我所說的,這對我有用,但是我只是在旋轉球體。 將其添加到您的需求中應該並不難,除了您擁有Arcball類的相機外,它可能更適合您的需求。 無論如何,我希望這對您有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.