下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
【移動(dòng)應(yīng)用開發(fā)技術(shù)】Android中如何使用重力傳感器實(shí)現(xiàn)橫豎屏放向切換功能
package
com.d1ev.touch.App.helper;
import
android.app.Activity;
import
android.content.pm.ActivityInfo;
import
android.util.Log;
import
android.view.OrientationEventListener;
import
java.lang.ref.WeakReference;
/**
*
Created
by
Administrator
on
2016/12/3
0003.
*
監(jiān)聽重力系統(tǒng)傳感器的變化,為Vr視頻播放器而定制
*/
public
class
MySensorHelper
{
private
static
final
String
TAG
=
MySensorHelper.class.getSimpleName();
private
OrientationEventListener
mLandOrientationListener;
private
OrientationEventListener
mPortOrientationListener;
private
WeakReference<Activity>
mActivityWeakRef;
private
boolean
isPortLock
=
false;
private
boolean
isLandLock=false;
public
MySensorHelper(final
Activity
activity)
{
this.mActivityWeakRef
=
new
WeakReference(activity);
this.mLandOrientationListener
=
new
OrientationEventListener(activity,
3)
{
public
void
onOrientationChanged(int
orientation)
{
Log.d(MySensorHelper.TAG,
"mLandOrientationListener");
if(orientation
<
100
&&
orientation
>
80
||
orientation
<
280
&&
orientation
>
260)
{
Log.e(MySensorHelper.TAG,
"轉(zhuǎn)到了橫屏");
if(!MySensorHelper.this.isLandLock)
{
Activity
mActivity
=
(Activity)MySensorHelper.this.mActivityWeakRef.get();
if(mActivity
!=
null)
{
Log.e(MySensorHelper.TAG,
"轉(zhuǎn)到了橫屏##################");
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
isLandLock=true;
isPortLock=false;
}
}
}
}
};
this.mPortOrientationListener
=
new
OrientationEventListener(activity,
3)
{
public
void
onOrientationChanged(int
orientation)
{
Log.w(MySensorHelper.TAG,
"mPortOrientationListener");
if(orientation
<
10
||
orientation
>
350
||
orientation
<
190
&&
orientation
>
170)
{
Log.e(MySensorHelper.TAG,
"轉(zhuǎn)到了豎屏");
if(!MySensorHelper.this.isPortLock)
{
Activity
mActivity
=
(Activity)MySensorHelper.this.mActivityWeakRef.get();
if(mActivity
!=
null)
{
Log.e(MySensorHelper.TAG,
"轉(zhuǎn)到了豎屏!!!!!!!!!!!!!!!!!!!!!!");
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
isPortLock=true;
isLandLock=false;
}
}
}
}
};
//this.disable();
}
//禁用切換屏幕的開關(guān)
public
void
disable()
{
Log.e(TAG,
"disable");
this.mPortOrientationListener.disable();
this.mLandOrientationListener.disable();
}
//開啟橫豎屏切換的開關(guān)
public
void
enable(){
this.mPortOrientationListener.enable();
this.mLandOrientationListener.enable();
}
//設(shè)置豎屏是否上鎖,true鎖定屏幕,fanle解鎖
public
void
setPortLock(boolean
lockFlag)
{
this.isPortLock
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025服務(wù)外包合同
- 2025房地產(chǎn)交割監(jiān)管合同
- 2025合同法討論案例
- 2024年神經(jīng)系統(tǒng)用藥項(xiàng)目資金籌措計(jì)劃書代可行性研究報(bào)告
- 智慧校園建設(shè)與學(xué)生學(xué)習(xí)方式的變革
- 現(xiàn)代農(nóng)業(yè)產(chǎn)業(yè)園區(qū)規(guī)劃與管理研究
- 高速公路服務(wù)區(qū)物業(yè)服務(wù)方案
- 二零二五年度出租房水電費(fèi)在線結(jié)算服務(wù)合同4篇
- 2025年新科版八年級生物上冊階段測試試卷含答案
- 2025年牛津譯林版九年級歷史上冊月考試卷含答案
- 不同茶葉的沖泡方法
- 光伏發(fā)電并網(wǎng)申辦具體流程
- 建筑勞務(wù)專業(yè)分包合同范本(2025年)
- 企業(yè)融資報(bào)告特斯拉成功案例分享
- 五年(2020-2024)高考地理真題分類匯編(全國版)專題12區(qū)域發(fā)展解析版
- 《阻燃材料與技術(shù)》課件 第8講 阻燃木質(zhì)材料
- 低空經(jīng)濟(jì)的社會(huì)接受度與倫理問題分析
- GB/T 4732.1-2024壓力容器分析設(shè)計(jì)第1部分:通用要求
- 河北省保定市競秀區(qū)2023-2024學(xué)年七年級下學(xué)期期末生物學(xué)試題(解析版)
- 運(yùn)動(dòng)技能學(xué)習(xí)與控制課件
- 六編元代文學(xué)
評論
0/150
提交評論