Android 는 센서 를 이용 하여 위 챗 을 모방 하여 흔 들 어 줍 니 다.

센서
센서 를 간단하게 소개 합 니 다.
바로 설비 가 주변 환경의 변 화 를 감지 하 는 하드웨어 다.
Android 의 센서 는 센서 프레임 에 포함 되 어 있 으 며 android.hardware.*(하드웨어 부분)에 속 합 니 다.
센서 프레임 워 크 는 주로 네 부분 을 포함 합 니 다.
① Sensor Manager:센서 의 입 구 를 가 져 오 는 데 사용 되 는 시스템 서비스 로 센서 등록 과 감청 취소 도 가능 합 니 다.
② 센서:센서 의 이름,유형,샘플링 율 을 포함 하 는 구체 적 인 센서
③ SensorEvent:센서 이벤트,센서 가 수집 한 데이터,센서 의 정밀도 포함
④ SensorEventListener:센서 의 감청,센서 데이터 변화,정밀도 변 화 를 주로 모니터링...
Android 오디 오 재생 시스템 은 두 가지 방식 을 제공 합 니 다.
① MediaPlayer 는 일반적인 오디 오,동 영상 을 재생 할 때 보통 플레이어 에 사용 된다.
② SoundPool 사 운 드 풀 사 운 드 풀 은 보통 작고 자주 재생 되 는 음악 으로 여러 음악 을 동시에 재생 해 야 한다.
VIBRATE 진동 센서 는 권한 을 추가 해 야 합 니 다.

실현 되 기 전에 핸드폰 에 있 는 센서 가 어떤 것 이 있 는 지 먼저 살 펴 보 자.여 기 는 생략...)
onCreat();텍스트 뷰 설정

sensor = (TextView) findViewById(R.id.sensor);

  mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

  List<Sensor> sensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);

  StringBuilder builder = new StringBuilder();

  builder.append("     : "+sensorList.size()+"
"); for (int i = 0; i < sensorList.size(); i++) { Sensor sensor = sensorList.get(i); builder.append(" : "+sensor.getName()+", : "+sensor.getVendor()+"
"); } sensor.setText(builder.toString());
놀랍게도 샤 오미 5 에 41 개의 센서 가 있다 니..대단 하군.
자,자,이제 본론 으로 들 어가 자.위 챗 흔 들 어 봐.
화면 에 있 으 면 세 장의 그림 을 레이 어드 하고 RelativeLayout 를 배치 합 니 다.

onCreate 에 봉인 하 는 방법

//   ---->     
  //1、     
  initView();
  //2、     SoundPool
  initSoundPool();
  //3、  
  initVibrator();
위 에 찍 힌 두 장의 그림 을 초기 화 합 니 다.

up_logo = (ImageView) findViewById(R.id.up_logo);
down_logo = (ImageView) findViewById(R.id.down_logo);
SoundPool 을 초기 화 했 을 때 new SoundPool 은 추천 하지 않 았 고 새 버 전(API>21)은 Builder 로 구축 되 었 기 때문에 여기 서 버 전 을 사용 하여 판단 하 였 습 니 다.

private void initSoundPool() {
  if(Build.VERSION.SDK_INT>=21){
   SoundPool.Builder builder = new SoundPool.Builder();
   //  
   builder.setMaxStreams(1);
   AudioAttributes attributes = new AudioAttributes.Builder()
     .setLegacyStreamType(AudioManager.STREAM_MUSIC)
     .build();

   builder.setAudioAttributes(attributes);

   mSoundPool = builder.build();
  }else {
   //    ,   
   mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);//       
  }
  //    
  //                
  mSoundPool_id = mSoundPool.load(this, R.raw.awe, 1);
 }

진동기 초기 화:(시스템 서비스 입 니 다)
mVibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
흔 들 어 보 는 것 은 가속도 센서 로 변화 상 태 를 감청 해 야 한다.
Sensor Manager 의 등록 도 한 쌍 으로 나 왔 습 니 다.

 @Override
 protected void onStart() {
  super.onStart();
  //4、       ,     
  initSensor();
 }

 @Override
 protected void onStop() {
  super.onStop();
  //    
  mSensorManager.unregisterListener(this);
 }

 private void initSensor() {
  Sensor accelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  //  SensorManager         
  mSensorManager.registerListener(this,accelerometerSensor,SensorManager.SENSOR_DELAY_UI);
 }

이곳 의 감청 은 수 동 으로 이 루어 져 야 한다.
implements Sensor Event Listener 는 두 가지 방법 을 실현 합 니 다.

//      
 @Override
 public void onSensorChanged(SensorEvent event) {

 }

//      ,    ,      
 @Override
 public void onAccuracyChanged(Sensor sensor, int accuracy) {

 }

다음은 방법의 구체 적 인 실현 이다.

@Override
 public void onSensorChanged(SensorEvent event) {//      
  Sensor sensor = event.sensor;
  int type = sensor.getType();
  switch (type){
   case Sensor.TYPE_ACCELEROMETER://     

    float[] values = event.values;
    //x,y,z     
    //9.8
    float x = values[0];
    float y = values[1];
    float z = values[2];

    if(Math.abs(x)>25||Math.abs(y)>25||Math.abs(z)>25){
     //     
     //    
     mSoundPool.play(mSoundPool_id,1,1,0,0,1);

     //  (-1       )
     mVibrator.vibrate(new long[]{200,300,400,200},-1);
     //    
     initAnimation();
    }
    break;
  }
 }

애니메이션 은 설명 할 것 이 없어 서 바로 코드 에 올 랐 다.

private void initAnimation() {
 //up_logo     ,       
 AnimationSet set_up = new AnimationSet(true);
 TranslateAnimation up_up = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,0,//y   
  TranslateAnimation.RELATIVE_TO_SELF,-1//y   
  );

 up_up.setDuration(1000);

 TranslateAnimation up_down = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,-1,//y   
  TranslateAnimation.RELATIVE_TO_SELF,0//y   
 );
 up_down.setDuration(1000);

 //    set      
 up_down.setStartOffset(500);
 set_up.addAnimation(up_up);//    ;
 set_up.addAnimation(up_down);//   
 up_logo.startAnimation(set_up);

//----------

 AnimationSet set_down = new AnimationSet(true);
 TranslateAnimation down_down = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,0,//y   
  TranslateAnimation.RELATIVE_TO_SELF,1//y   
 );
 down_down.setDuration(1000);
 TranslateAnimation down_up = new TranslateAnimation(
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,0,//x   
  TranslateAnimation.RELATIVE_TO_SELF,1,//y   
  TranslateAnimation.RELATIVE_TO_SELF,0//y   
 );
 down_up.setDuration(1000);
 down_up.setStartOffset(500);
 set_down.addAnimation(down_down);//    
 set_down.addAnimation(down_up);//    
 down_logo.startAnimation(set_down);

 }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기