forked from hzuapps/android-labs-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
304 additions
and
334 deletions.
There are no files selected for viewing
18 changes: 6 additions & 12 deletions
18
students/Soft1714080902403/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
265 changes: 230 additions & 35 deletions
265
...s/Soft1714080902403/app/src/main/java/edu/androidlabs/soft1714080902403/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,254 @@ | ||
package edu.androidlabs.soft1714080902403; | ||
|
||
import android.content.Context; | ||
import android.content.Intent; | ||
import android.content.pm.ActivityInfo; | ||
import android.hardware.Sensor; | ||
import android.hardware.SensorEvent; | ||
import android.hardware.SensorEventListener; | ||
import android.hardware.SensorManager; | ||
import android.media.AudioManager; | ||
import android.media.SoundPool; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.os.Vibrator; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
import android.view.animation.Animation; | ||
import android.view.animation.TranslateAnimation; | ||
import android.widget.ImageView; | ||
import android.widget.LinearLayout; | ||
|
||
import java.io.FileOutputStream; | ||
import edu.androidlabs.soft1714080902403.R; | ||
|
||
public class MainActivity extends AppCompatActivity { | ||
import java.lang.ref.WeakReference; | ||
|
||
private TextView textView; | ||
private Button btn_save; | ||
private EditText et_info; | ||
public class MainActivity extends AppCompatActivity implements SensorEventListener { | ||
|
||
private static final String TAG = "MainActivity"; | ||
private static final int START_SHAKE = 0x1; | ||
private static final int AGAIN_SHAKE = 0x2; | ||
private static final int END_SHAKE = 0x3; | ||
|
||
private SensorManager mSensorManager; | ||
private Sensor mAccelerometerSensor; | ||
private Vibrator mVibrator;//手机震动 | ||
private SoundPool mSoundPool;//摇一摇音效 | ||
|
||
//记录摇动状态 | ||
private boolean isShake = false; | ||
|
||
|
||
private LinearLayout mTopLayout; | ||
private LinearLayout mBottomLayout; | ||
private ImageView mTopLine; | ||
private ImageView mBottomLine; | ||
|
||
private MyHandler mHandler; | ||
private int mWeiChatAudio; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
//设置只竖屏 | ||
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); | ||
setContentView(R.layout.activity_main); | ||
et_info = (EditText) findViewById(R.id.et_info); | ||
btn_save = (Button) findViewById(R.id.btn_save); | ||
btn_save.setOnClickListener(new ButtonListener()); | ||
} | ||
|
||
|
||
public class ButtonListener implements View.OnClickListener { | ||
public void onClick(View v) { | ||
switch (v.getId()) { | ||
case R.id.btn_save: | ||
String saveinfo = et_info.getText().toString().trim(); | ||
FileOutputStream fos; | ||
try { | ||
fos = openFileOutput("data.txt", Context.MODE_APPEND); | ||
fos.write(saveinfo.getBytes()); | ||
fos.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
//初始化View | ||
initView(); | ||
mHandler = new MyHandler(this); | ||
|
||
//初始化SoundPool | ||
mSoundPool = new SoundPool(1, AudioManager.STREAM_SYSTEM, 5); | ||
mWeiChatAudio = mSoundPool.load(this, R.raw.weichat_audio, 1); | ||
|
||
//获取Vibrator震动服务 | ||
mVibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); | ||
|
||
} | ||
|
||
private void initView() { | ||
|
||
mTopLayout = (LinearLayout) findViewById(R.id.main_linear_top); | ||
mBottomLayout = ((LinearLayout) findViewById(R.id.main_linear_bottom)); | ||
mTopLine = (ImageView) findViewById(R.id.main_shake_top_line); | ||
mBottomLine = (ImageView) findViewById(R.id.main_shake_bottom_line); | ||
|
||
//默认 | ||
mTopLine.setVisibility(View.GONE); | ||
mBottomLine.setVisibility(View.GONE); | ||
|
||
|
||
} | ||
|
||
@Override | ||
protected void onStart() { | ||
super.onStart(); | ||
//获取 SensorManager 负责管理传感器 | ||
mSensorManager = ((SensorManager) getSystemService(SENSOR_SERVICE)); | ||
if (mSensorManager != null) { | ||
//获取加速度传感器 | ||
mAccelerometerSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); | ||
if (mAccelerometerSensor != null) { | ||
mSensorManager.registerListener(this, mAccelerometerSensor, SensorManager.SENSOR_DELAY_UI); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
|
||
if (mSensorManager != null) { | ||
mSensorManager.unregisterListener(this); | ||
} | ||
super.onPause(); | ||
} | ||
|
||
|
||
|
||
|
||
@Override | ||
public void onSensorChanged(SensorEvent event) { | ||
int type = event.sensor.getType(); | ||
|
||
if (type == Sensor.TYPE_ACCELEROMETER) { | ||
//获取三个方向值 | ||
float[] values = event.values; | ||
float x = values[0]; | ||
float y = values[1]; | ||
float z = values[2]; | ||
|
||
if ((Math.abs(x) > 17 || Math.abs(y) > 17 || Math | ||
.abs(z) > 17) && !isShake) { | ||
isShake = true; | ||
|
||
Thread thread = new Thread() { | ||
@Override | ||
public void run() { | ||
|
||
|
||
super.run(); | ||
try { | ||
Log.d(TAG, "onSensorChanged: 摇动"); | ||
|
||
//开始震动 发出提示音 展示动画效果 | ||
mHandler.obtainMessage(START_SHAKE).sendToTarget(); | ||
Thread.sleep(500); | ||
//再来一次震动提示 | ||
mHandler.obtainMessage(AGAIN_SHAKE).sendToTarget(); | ||
Thread.sleep(500); | ||
mHandler.obtainMessage(END_SHAKE).sendToTarget(); | ||
|
||
|
||
} catch (InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
Toast.makeText(MainActivity.this, "输入答案成功", Toast.LENGTH_SHORT).show(); | ||
break; | ||
}; | ||
thread.start(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onAccuracyChanged(Sensor sensor, int accuracy) { | ||
} | ||
|
||
|
||
public void click(View view){ | ||
Intent intent=new Intent(this,Soft1714080902403.class); | ||
startActivity(intent); | ||
private static class MyHandler extends Handler { | ||
private WeakReference<MainActivity> mReference; | ||
private MainActivity mActivity; | ||
public MyHandler(MainActivity activity) { | ||
mReference = new WeakReference<MainActivity>(activity); | ||
if (mReference != null) { | ||
mActivity = mReference.get(); | ||
} | ||
} | ||
@Override | ||
public void handleMessage(Message msg) { | ||
super.handleMessage(msg); | ||
switch (msg.what) { | ||
case START_SHAKE: | ||
//This method requires the caller to hold the permission VIBRATE. | ||
mActivity.mVibrator.vibrate(300); | ||
//发出提示音 | ||
mActivity.mSoundPool.play(mActivity.mWeiChatAudio, 1, 1, 0, 0, 1); | ||
mActivity.mTopLine.setVisibility(View.VISIBLE); | ||
mActivity.mBottomLine.setVisibility(View.VISIBLE); | ||
mActivity.startAnimation(false);//参数含义: (不是回来) 也就是说两张图片分散开的动画 | ||
break; | ||
case AGAIN_SHAKE: | ||
mActivity.mVibrator.vibrate(300); | ||
break; | ||
case END_SHAKE: | ||
//整体效果结束, 将震动设置为false | ||
mActivity.isShake = false; | ||
// 展示上下两种图片回来的效果 | ||
mActivity.startAnimation(true); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} | ||
/** | ||
* 开启 摇一摇动画 | ||
* | ||
* @param isBack 是否是返回初识状态 | ||
*/ | ||
private void startAnimation(boolean isBack) { | ||
//动画坐标移动的位置的类型是相对自己的 | ||
int type = Animation.RELATIVE_TO_SELF; | ||
|
||
float topFromY; | ||
float topToY; | ||
float bottomFromY; | ||
float bottomToY; | ||
if (isBack) { | ||
topFromY = -0.5f; | ||
topToY = 0; | ||
bottomFromY = 0.5f; | ||
bottomToY = 0; | ||
} else { | ||
topFromY = 0; | ||
topToY = -0.5f; | ||
bottomFromY = 0; | ||
bottomToY = 0.5f; | ||
} | ||
|
||
//上面图片的动画效果 | ||
TranslateAnimation topAnim = new TranslateAnimation( | ||
type, 0, type, 0, type, topFromY, type, topToY | ||
); | ||
topAnim.setDuration(200); | ||
//动画终止时停留在最后一帧~不然会回到没有执行之前的状态 | ||
topAnim.setFillAfter(true); | ||
|
||
//底部的动画效果 | ||
TranslateAnimation bottomAnim = new TranslateAnimation( | ||
type, 0, type, 0, type, bottomFromY, type, bottomToY | ||
); | ||
bottomAnim.setDuration(200); | ||
bottomAnim.setFillAfter(true); | ||
|
||
|
||
if (isBack) { | ||
bottomAnim.setAnimationListener(new Animation.AnimationListener() { | ||
@Override | ||
public void onAnimationStart(Animation animation) {} | ||
@Override | ||
public void onAnimationRepeat(Animation animation) {} | ||
@Override | ||
public void onAnimationEnd(Animation animation) { | ||
//当动画结束后 , 将中间两条线GONE掉, 不让其占位 | ||
mTopLine.setVisibility(View.GONE); | ||
mBottomLine.setVisibility(View.GONE); | ||
} | ||
}); | ||
} | ||
//设置动画 | ||
mTopLayout.startAnimation(topAnim); | ||
mBottomLayout.startAnimation(bottomAnim); | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.