电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

Android實現微信搖一搖功能

瀏覽:140日期:2022-06-06 10:37:14

本文實例為大家分享了Android實現微信搖一搖功能的具體代碼,供大家參考,具體內容如下

1、初始化界面

設置搖一搖界面的背景圖片和搖動時的上下兩半張圖片

Android實現微信搖一搖功能

<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android: android:layout_width='match_parent' android:layout_height='match_parent' tools:context='com.example.yyy.MainActivity' android:background='@mipmap/shakehideimg_man2' > <LinearLayout android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' > <ImageView android: android:layout_width='match_parent' android:layout_height='0dp' android:layout_weight='1' android:background='@mipmap/shake_logo_up' /> <ImageView android: android:layout_width='match_parent' android:layout_height='0dp' android:layout_weight='1' android:background='@mipmap/shake_logo_down' /> </LinearLayout></RelativeLayout>

2、Mainactivity - onCreate()

private ImageView imgDown; private ImageView imgUp; private SensorManager sensorManager; private SensorEventListener sensorEventListener; private Sensor accSensor; private AnimationSet upAnimationSet; private AnimationSet downAnimationSet; private SoundPool soundPool; private int soundId; private Vibrator vibrator; private boolean isYYY = false; /*1.初始化頁面 2.初始化數據 * 3.監聽加速度變化(觸發條件) * 1.圖片執行動畫 * ***2.到服務器查詢同一時間搖一搖的異性用戶 * 2.播放音樂 * 3.振動 * **4.當你正在搖的時候(不能再搖動) * */ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initEvent(); //注冊監聽 sensorManager.registerListener(sensorEventListener,accSensor,SENSOR_DELAY_NORMAL);}

3、初始化數據

private void initData() { //先獲得傳感器管理器 sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //獲得加速度傳感器 accSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //獲得振動器 vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); //初始化聲音池 soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0); //初始化聲音資源 soundId = soundPool.load(this,R.raw.awe,1); //初始化動畫 upAnimationSet = new AnimationSet(true); TranslateAnimation upUpAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -0.5f); upUpAnimation.setDuration(500); TranslateAnimation upDownAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 0); upDownAnimation.setDuration(500); //down動畫在up動畫之后執行 upUpAnimation.setStartOffset(500); upAnimationSet.addAnimation(upUpAnimation); upAnimationSet.addAnimation(upDownAnimation); upAnimationSet.setDuration(1000); upAnimationSet.setStartOffset(200); //初始化動畫 downAnimationSet = new AnimationSet(true); TranslateAnimation downUpAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0); downUpAnimation.setDuration(500); TranslateAnimation downDownAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f); downDownAnimation.setDuration(500); //down動畫在up動畫之后執行 downDownAnimation.setStartOffset(500); downAnimationSet.addAnimation(downDownAnimation); downAnimationSet.addAnimation(downUpAnimation); downAnimationSet.setDuration(1000); downAnimationSet.setStartOffset(200); }

4、初始化事件 - 搖一搖

給加速度感應器設置監聽① 設置搖一搖的觸發條件② 播放動畫③ 播放音樂④ 開啟震動

private void initEvent() { sensorEventListener = new SensorEventListener() { /* * 當傳感器的值發生變化時的回調 * */ @Override public void onSensorChanged(SensorEvent event) { //Log.i('AAA', 'onSensorChanged: '); //設置觸發搖一搖的條件 //獲得x,y,z方向的變化 float[] values = event.values; float valueX = values[0]; //空間中X的變化 float valueY = values[1]; //空間中Y的變化 float valueZ = values[2]; //空間中Z的變化 if(valueX > 15 || valueY > 15 || valueZ >15){//觸發條件 if(!isYYY){ imgUp.startAnimation(upAnimationSet); imgDown.startAnimation(downAnimationSet); //播放音樂 soundPool.play(soundId,1,1,1,0,1); //振動 vibrator.vibrate(new long[]{200,400,200,400,200,400,200,400},-1); } } } /* *當傳感器精度發生變化的回調 * */ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } }; upAnimationSet.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { isYYY = true; } @Override public void onAnimationEnd(Animation animation) { isYYY = false; } @Override public void onAnimationRepeat(Animation animation) { } });}

5、添加權限

<uses-permission android:name='android.permission.VIBRATE'></uses-permission>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: 微信
相關文章:
主站蜘蛛池模板: 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 西点培训学校_法式西点培训班_西点师培训_西点蛋糕培训-广州烘趣西点烘焙培训学院 | 东莞办公家具厂家直销-美鑫【免费3D效果图】全国办公桌/会议桌定制 | 焊接减速机箱体,减速机箱体加工-淄博博山泽坤机械厂 | 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | POS机官网 - 拉卡拉POS机免费办理|官网在线申请入口 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | 自动气象站_气象站监测设备_全自动气象站设备_雨量监测站-山东风途物联网 | 电子巡更系统-巡检管理系统-智能巡检【金万码】 | 郑州电线电缆厂家-防火|低压|低烟无卤电缆-河南明星电缆 | 会议会展活动拍摄_年会庆典演出跟拍_摄影摄像直播-艾木传媒 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 赛尔特智能移动阳光房-阳光房厂家-赛尔特建筑科技(广东)有限公司 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 沧州友城管业有限公司-内外涂塑钢管-大口径螺旋钢管-涂塑螺旋管-保温钢管生产厂家 | 北京四合院出租,北京四合院出售,北京平房买卖 - 顺益兴四合院 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | 防爆电机_ybx3系列电机_河南省南洋防爆电机有限公司 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | 气弹簧定制-气动杆-可控气弹簧-不锈钢阻尼器-工业气弹簧-可调节气弹簧厂家-常州巨腾气弹簧供应商 | 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 防火阀、排烟防火阀、电动防火阀产品生产销售商-德州凯亿空调设备有限公司 | 上海噪音治理公司-专业隔音降噪公司-中广通环保 | 展厅装修公司|企业展厅设计|展厅制作|展厅搭建—广州展厅装饰公司 | 全自动不干胶贴标机_套标机-上海今昂贴标机生产厂家 | 砍排机-锯骨机-冻肉切丁机-熟肉切片机-预制菜生产线一站式服务厂商 - 广州市祥九瑞盈机械设备有限公司 | 混合反应量热仪-高温高压量热仪-微机差热分析仪DTA|凯璞百科 | 环压强度试验机-拉链拉力试验机-上海倾技仪器仪表科技有限公司 | 余姚生活网_余姚论坛_余姚市综合门户网站 | 电动高压冲洗车_价格-江苏速利达机车有限公司 | 匀胶机旋涂仪-声扫显微镜-工业水浸超声-安赛斯(北京)科技有限公司 | 储能预警-储能消防系统-电池舱自动灭火装置-四川千页科技股份有限公司官网 | HDPE土工膜,复合土工膜,防渗膜价格,土工膜厂家-山东新路通工程材料有限公司 | wika威卡压力表-wika压力变送器-德国wika代理-威卡总代-北京博朗宁科技 | 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | 长春网站建设,五合一网站设计制作,免费优化推广-长春网站建设 | 电机修理_二手电机专家-河北豫通机电设备有限公司(原石家庄冀华高压电机维修中心) |