仿微信摇一摇功能的实现
2016-03-04 10:05
627 查看
用最基本的android东西写了一个仿照微信摇一摇的功能。下面给出详细的代码。
最底层放一张红色的菊花图片,然后上层放一张手的图片,上层这张图片一分为二,要的时候,添加动画图片的上半部分向上,下办部分向下。ok还是看代码吧~~~。
主要布局使用FrameLayout布局来实现,其他都是最简单最傻x的实现。
下面贴出activity实现部分
这里写代码片首先贴出布局文件。
最底层放一张红色的菊花图片,然后上层放一张手的图片,上层这张图片一分为二,要的时候,添加动画图片的上半部分向上,下办部分向下。ok还是看代码吧~~~。
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#282e2c"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:layout_margin="50dp" android:layout_gravity="center" android:src="@mipmap/flower" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:id="@+id/shake_topId" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#101010"> <ImageView android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="bottom" android:src="@mipmap/shake_top" /> </LinearLayout> <LinearLayout android:id="@+id/shake_botId" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#101010"> <ImageView android:layout_width="match_parent" android:layout_height="100dp" android:src="@mipmap/shake_bottom" /> </LinearLayout> </LinearLayout> <TextView android:id="@+id/shakeTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:text="欢迎使用微信摇一摇" android:textColor="@color/colorAccent" android:textSize="25sp" /> </FrameLayout>
主要布局使用FrameLayout布局来实现,其他都是最简单最傻x的实现。
下面贴出activity实现部分
/** * Created by sfbao1 on 2016/3/3. * 摇一摇的功能 */ public class HaveAShake extends Activity implements SensorEventListener,Animation.AnimationListener { TextView tv = null; MediaPlayer mp; SensorManager sensorManager = null; LinearLayout topLv,botLv; TranslateAnimation atopup,atopdown,abotup,abotdown; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.shake); initPlayer(); initWidget(); initAnimation(); } /** * 初始化各类控件 */ private void initWidget() { sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); tv = ((TextView) findViewById(R.id.shakeTv)); topLv= ((LinearLayout) findViewById(R.id.shake_topId)); botLv= ((LinearLayout) findViewById(R.id.shake_botId)); } /** * 初始化上下两张图片的动画 */ private void initAnimation() { atopup=new TranslateAnimation(0,0,0,-300.0f); abotup=new TranslateAnimation(0,0,0, 300.0f); atopdown=new TranslateAnimation(0,0,-300.0f,0); abotdown=new TranslateAnimation(0,0,300.0f,0); atopup.setDuration(1000); abotup.setDuration(1000); abotdown.setDuration(1000); atopdown.setDuration(1000); abotdown.setFillAfter(true); abotup.setFillAfter(true); atopdown.setFillAfter(true); atopup.setFillAfter(true); AccelerateDecelerateInterpolator as=new AccelerateDecelerateInterpolator(); atopup.setInterpolator(as); atopdown.setInterpolator(as); abotup.setInterpolator(as); abotdown.setInterpolator(as); atopup.setAnimationListener(this); abotup.setAnimationListener(this); } /** * 音频文件相关 */ private void initPlayer(){ mp=MediaPlayer.create(this,R.raw.shake_sound_male); } /** * 传感器感受到摇晃时候发生变化 * @param event */ @Override public void onSensorChanged(SensorEvent event) { int sensorType = event.sensor.getType(); //values[0]:X轴,values[1]:Y轴,values[2]:Z轴 float[] values = event.values; if (sensorType == Sensor.TYPE_ACCELEROMETER) { if ((Math.abs(values[0]) > 17 || Math.abs(values[1]) > 17 || Math.abs(values[2]) > 17)) { tv.setText("摇一摇成功"); topLv.startAnimation(atopup); botLv.startAnimation(abotup); mp.start(); } } } /** * 传感器精度改变的时候回调这个方法 * * @param sensor * @param accuracy */ @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @Override protected void onPause() { super.onPause(); sensorManager.unregisterListener(this); } @Override protected void onDestroy() { super.onDestroy(); mp.release(); } @Override protected void onResume() { super.onResume(); sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { botLv.startAnimation(abotdown); topLv.startAnimation(atopdown); } @Override public void onAnimationRepeat(Animation animation) { } }
相关文章推荐
- 一个java小程序(打印出目录)
- NodeJs 开发微信公众号(五)真实环境部署
- iOS之微信支付(一)
- 夺命雷公狗---微信开发40----微信语言识别接口2(点歌系统)
- 夺命雷公狗---微信开发39----微信语言识别接口1
- 夺命雷公狗---微信开发38----临时二维码
- 夺命雷公狗---微信开发37----永久二维码2
- 夺命雷公狗---微信开发36----永久二维码1
- Java微信公众平台开发(3) 接收消息的分类及实体的创建
- Java微信公众平台开发(1) 接入微信公众平台
- 马化腾回应微信提现收费:成本一个月超过3亿,实在受不了
- 微信公众号定位和盈利模式
- 用什么软件可以快速制作微信视频
- 用node.js进行微信公众平台的开发
- 仿微信公众平台登录页面
- 安卓类似于微信朋友圈功能,集合了Gson,PhotoView,android-universal-image-loader这些用法
- 完整的单步二维码扫描下载html代码包括微信
- iOS集成微信支付的一些坑:onResp不回调、只显示一个确定按钮、闪回
- 微信公众平台开发教程
- Jeecg BPM 重磅来袭,推出移动OA审批功能,无缝对接微信公众号和微信企业号