3994人阅读
android必知必会(83)
一、原理介绍:
Android手机中摇一摇的功能已经很常见了,最近接触到了这个功能,原理很简单:使用加速度传感器,在晃动手机时,***加速度在各个方向的变化,当加速度值超过设定的灵敏度时,则触发摇一摇功能。
二、使用到的类:
三、功能实现:
1、摇一摇功能:我将摇一摇功能封装成了一个类ShakeUtils,在使用时按照注释中的说明使用即可,ShakeUtils.java如下(差的包请CTRL+SHIFT+O导入):
* 摇一摇工具类
* 使用说明:
* private ShakeUtils mShakeUtils =
* 1、在需要使用摇一摇功能的Activity实例化该工具类并设置摇一摇***:
* mShakeUtils = new ShakeUtils( this );
* mShakeUtils.setOnShakeListener(new OnShakeListener{
public void onShake(){
// 此处为摇一摇触发后的操作
* 2、分别在Activity的onResume和onPause方法中调用该工具类的onResume和onPause方法:
* mShakeUtils.onResume();
* mShakeUtils.onPause();
public class ShakeUtils implements SensorEventListener {
public ShakeUtils( Context context ){
mSensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
public void setOnShakeListener( OnShakeListener onShakeListener ){
mOnShakeListener = onShakeL
public void onResume(){
mSensorManager.registerListener(this,
mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
public void onPause(){
mSensorManager.unregisterListener(this);
public void onAccuracyChanged(Sensor sensor, int accuracy) {
public void onSensorChanged(SensorEvent event) {
int sensorType = event.sensor.getType();
//values[0]:X轴,values[1]:Y轴,values[2]:Z轴
float[] values = event.
if (sensorType == Sensor.TYPE_ACCELEROMETER){
//这里可以调节摇一摇的灵敏度
if ((Math.abs(values[0]) & SENSOR_VALUE || Math.abs(values[1]) & SENSOR_VALUE || Math.abs(values[2]) & SENSOR_VALUE)){
System.out.println(&sensor value == & + & & + values[ 0 ] + & & + values[ 1 ] + & & +
values[ 2 ] );
if( null != mOnShakeListener ){
mOnShakeListener.onShake( );
public interface OnShakeListener{
public void onShake();
private SensorManager mSensorManager =
private OnShakeListener mOnShakeListener =
private static final int SENSOR_VALUE = 14;
2、BaseActivity:为了方便程序扩展,为所有的Activity写了一个基类:
public abstract class BaseActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
private void init(){
setContentView( );
findViews( );
getData( );
showContent( );
public abstract void setContentView();
public abstract void findViews();
public abstract void getData();
public abstract void showContent();
3、ShakeBeautyActivity:ShakeUtils的具体使用:
public class ShakeBeautyActivity extends BaseActivity {
public void setContentView() {
setContentView( R.layout.activity_shake_beauty_layout );
public void findViews() {
mShakeImageView = ( ImageView )findViewById( R.id.shakeImgId );
initShakeUtils( );
public void getData() {
public void showContent() {
protected void onResume() {
super.onResume();
mShakeUtils.onResume( );
protected void onPause() {
super.onPause();
mShakeUtils.onPause( );
private void initShakeUtils(){
mShakeUtils = new ShakeUtils( this );
mShakeUtils.setOnShakeListener( new OnShakeListener( ) {
public void onShake() {
setShakeImage( );
private void setShakeImage( ){
Random random = new Random( );
mShakeImageView.setBackgroundResource( mBeautys[ ( Math.abs(random.nextInt( ) ) )%mBeautys.length ] );
private ImageView mShakeImageView =
private ShakeUtils mShakeUtils =
private static final int[] mBeautys = new int[]{
R.drawable.beauty_01
,R.drawable.beauty_02
,R.drawable.beauty_03
,R.drawable.beauty_04
,R.drawable.beauty_05
,R.drawable.beauty_06
,R.drawable.beauty_07
,R.drawable.beauty_08
,R.drawable.beauty_09
四、demo下载:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:359908次
积分:4513
积分:4513
排名:第6865名
原创:83篇
转载:29篇
译文:10篇
评论:79条
阅读:16138
(2)(2)(2)(7)(2)(6)(4)(7)(4)(17)(7)(9)(3)(3)(3)(2)(8)(2)(9)(15)(5)(3)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
新浪微博:/sincerecorner
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(1335)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Android 摇一摇功能实现',
blogAbstract:'比较简单,就是一个重力感应器的***问题。package com.example.import android.hardware.Simport android.hardware.SensorEimport android.hardware.SensorEventLimport android.hardware.SensorMimport android.os.Bimport android.os.Himport android.os.Mimport android.os.Vimport android.app.A',
blogTag:'',
blogUrl:'blog/static/',
isPublished:1,
istop:false,
modifyTime:3,
publishTime:8,
permalink:'blog/static/',
commentCount:1,
mainCommentCount:1,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:true,
hostIntro:'新浪微博:/sincerecorner',
hmcon:'0',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}