您的位置:首页 > 移动开发 > 微信开发

仿微信摇一摇功能的实现

2016-03-04 10:05 627 查看
用最基本的android东西写了一个仿照微信摇一摇的功能。下面给出详细的代码。
这里写代码片
首先贴出布局文件。

最底层放一张红色的菊花图片,然后上层放一张手的图片,上层这张图片一分为二,要的时候,添加动画图片的上半部分向上,下办部分向下。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) {

}
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: