android “摇一摇”动态更换皮肤 实例源码下载

Java基础

浏览数:93

2020-7-16

AD:资源代下载服务

package com.shf.backgroundchange;





import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.res.Resources;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TestActivity extends Activity {
	// 定义sensor管理器
	private SensorManager mSensorManager;
	
	// 震动
	Vibrator vibrator;
	// 震动监听器
	private sensorListener sensor;

	public static int flag = 0;
	private static LinearLayout L = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		L = (LinearLayout)findViewById(R.id.L);
		Resources r = getResources();
		L.setBackgroundDrawable(r.getDrawable(R.drawable.bg01));
		// 获取传感器管理服务
		mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
		// 震动
		vibrator = (Vibrator) getSystemService(Service.VIBRATOR_SERVICE);
		// 监听器
		sensor = new sensorListener();
		// 加速度传感器
		mSensorManager.registerListener(sensor,
				mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
				// 还有SENSOR_DELAY_UI、SENSOR_DELAY_FASTEST、SENSOR_DELAY_GAME等,
				// 根据不同应用,需要的反应速率不同,具体根据实际情况设定
				SensorManager.SENSOR_DELAY_NORMAL);
	}

	@Override
	protected void onPause() {
		//mSensorManager.unregisterListener(sensor);
		super.onPause();
	}

	@Override
	protected void onStop() {
		//mSensorManager.unregisterListener(sensor);
		super.onStop();
	}
	
    @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mHandler.removeMessages(0);
	}
	public Handler mHandler = new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);

			Resources r = getResources();
			switch (flag) {
			case 0:
				L.setBackgroundDrawable(r.getDrawable(R.drawable.bg01));
				break;
			case 1:
				L.setBackgroundDrawable(r.getDrawable(R.drawable.bg02));
				break;
			case 2:
				L.setBackgroundDrawable(r.getDrawable(R.drawable.bg03));
				break;
			case 3:
				L.setBackgroundDrawable(r.getDrawable(R.drawable.bg04));
				break;
			}

		}

    };

	private class sensorListener implements SensorEventListener {
		@Override
		public void onAccuracyChanged(Sensor arg0, int arg1) {
			// TODO Auto-generated method stub
		}

		@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]) > 12 || Math.abs(values[1]) > 12 || Math
						.abs(values[2]) > 12)) {
					flag  ;
					if(flag>3) flag = 0;
					 long[] pattern = {500,500}; // OFF/ON/OFF/ON            
				        vibrator.vibrate(pattern,-1);
					mHandler.sendEmptyMessage(0);
				}
			}
		}
	}

}