博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Androidの高级交互之仿微信摇一摇(真心摇一摇,而不是像网传的就那么简单的震动一下)...
阅读量:5159 次
发布时间:2019-06-13

本文共 6531 字,大约阅读时间需要 21 分钟。

 

话不多说看代码

 

布局文件:

 

 java:

package cn.com.bvin.shake;import java.io.IOException;import android.app.Activity;import android.content.Context;import android.content.res.AssetFileDescriptor;import android.content.res.AssetManager;import android.graphics.Color;import android.hardware.Sensor;import android.hardware.SensorEvent;import android.hardware.SensorEventListener;import android.hardware.SensorManager;import android.media.MediaPlayer;import android.os.Bundle;import android.util.Log;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup.LayoutParams;import android.view.animation.AccelerateInterpolator;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.DecelerateInterpolator;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class MainActivity extends Activity implements SensorEventListener,AnimationListener{    /** Called when the activity is first created. */        private SensorManager sMan;    private Sensor sensor;    private Animation topInAnim = null;    private Animation topOutAnim = null;    private Animation botInAnim = null;    private Animation botOutAnim = null;    private int duration =630;    private ImageView ivTop;    private ImageView ivBot;        private ImageView shake_bg;        private ImageView shake_line_up;    private ImageView shake_line_down;        private RelativeLayout shake_panel_up;    private RelativeLayout shake_panel_down;        private int scr_h,src_w;        private MediaPlayer mPlayer;            @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        scr_h = getWindowManager().getDefaultDisplay().getHeight();        src_w = getWindowManager().getDefaultDisplay().getWidth();        initSensorManager();        initViews();        initAnim();        initPlayer();    }    private  void initPlayer(){        mPlayer = new MediaPlayer();        //mPlayer.setDataSource(context, uri);        AssetManager aMan = this.getAssets();        try {            AssetFileDescriptor  fileDescriptor = aMan.openFd("shake_sound_male.mp3");            mPlayer.setDataSource(fileDescriptor.getFileDescriptor(),fileDescriptor.getStartOffset(),fileDescriptor.getLength());            mPlayer.prepare();            //mPlayer.start();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }        private void initSensorManager(){                 sMan = (SensorManager)getSystemService(Context.SENSOR_SERVICE);         sensor = sMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);         sMan.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL);            }    private void initAnim(){                        topOutAnim = new TranslateAnimation(0,0,0,-(float)scr_h/4);        topOutAnim.setDuration(duration);        topOutAnim.setInterpolator(new DecelerateInterpolator());                botOutAnim = new TranslateAnimation(0,0,0,(float)scr_h/4);        botOutAnim.setDuration(duration);        botOutAnim.setInterpolator(new DecelerateInterpolator());                topInAnim = new TranslateAnimation(0,0,-(float)scr_h/4,0);        topInAnim.setDuration(duration);        topInAnim.setInterpolator(new AccelerateInterpolator());                botInAnim = new TranslateAnimation(0,0,(float)scr_h/4,0);        botInAnim.setDuration(duration);        botInAnim.setInterpolator(new AccelerateInterpolator());                botInAnim.setAnimationListener(this);        botOutAnim.setAnimationListener(this);            }        private void initViews(){                        shake_bg = (ImageView)findViewById(R.id.iv_hide);                shake_panel_up = (RelativeLayout)findViewById(R.id.frameTop);        shake_panel_down = (RelativeLayout)findViewById(R.id.frameBot);                shake_line_up = (ImageView)findViewById(R.id.shake_line_up);        shake_line_down = (ImageView)findViewById(R.id.shake_line_down);        shake_line_up.setVisibility(View.INVISIBLE);        shake_line_down.setVisibility(View.INVISIBLE);                ivTop =  (ImageView)findViewById(R.id.ivTop);        ivBot = (ImageView)findViewById(R.id.ivBot);    }        @Override    public void onAnimationEnd(Animation arg0) {        // TODO Auto-generated method stub        if(arg0 == botOutAnim){
//夹门打开 shake_panel_up.startAnimation(topInAnim); shake_panel_down.startAnimation(botInAnim); }else if(arg0.equals(botInAnim)){
//夹门关闭 Log.e("wn", "sdgsdg"); sMan.registerListener(this, sensor,SensorManager.SENSOR_DELAY_NORMAL); shake_line_up.setVisibility(View.INVISIBLE); shake_line_down.setVisibility(View.INVISIBLE); shake_bg.setVisibility(View.INVISIBLE); //完成动画后,再次监听加速器 } } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } @Override public void onSensorChanged(SensorEvent event) { // TODO Auto-generated method stub float x = event.values[SensorManager.DATA_X]; float y = event.values[SensorManager.DATA_Y]; float z = event.values[SensorManager.DATA_Z]; if(Math.abs(x)>=14||Math.abs(y)>=14||Math.abs(z)>=14){ shake_line_up.setVisibility(View.VISIBLE); shake_line_down.setVisibility(View.VISIBLE); shake_bg.setVisibility(View.VISIBLE); mPlayer.start(); shake_panel_up.startAnimation(topOutAnim); shake_panel_down.startAnimation(botOutAnim); sMan.unregisterListener(this); } } }

 

 附件怎么传啊?怎么我没看到哪里传附件啊,我没权限么,源码可以共享啊

转载于:https://www.cnblogs.com/bvin/archive/2012/12/13/2816840.html

你可能感兴趣的文章
Real-Time Rendering 笔记
查看>>
多路复用
查看>>
spring IOC装配Bean(注解方式)
查看>>
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“Manag
查看>>
利用SignalR来同步更新Winfrom
查看>>
反射机制
查看>>
CocoaPod
查看>>
BZOJ 1251: 序列终结者 [splay]
查看>>
5G边缘网络虚拟化的利器:vCPE和SD-WAN
查看>>
MATLAB基础入门笔记
查看>>
【UVA】434-Matty's Blocks
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
django知识点总结
查看>>
C++ STL stack、queue和vector的使用
查看>>
使用Reporting Services时遇到的小问题
查看>>
约瑟夫问题
查看>>
Arduino 报错总结
查看>>
树莓派Android Things物联网开发:树莓派GPIO引脚图
查看>>
矩阵快速幂---BestCoder Round#8 1002
查看>>