陀螺仪监听器动态让背景动起来。

来源:互联网 发布:ipad1怎么装软件 编辑:程序博客网 时间:2024/05/27 00:41
package simai.gyroscope.sensor;

import simai.sensor.R;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

/**螺旋仪传感器**/
public class Gyroscope_Sensor extends Activity implements SensorEventListener{
       
        TextView gyText;
        SensorManager sManager;
        Sensor mSensor ;
        ImageView mgyrosImage;
        private float gyros_num = 0;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);       
                setContentView(R.layout.gyroscope_layout);
                InitView();
        }
       
        public void InitView(){
                sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
                mSensor = sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
                gyText = (TextView) findViewById(R.id.groscope_text);
                mgyrosImage = (ImageView) findViewById(R.id.groscope_image);
                mgyrosImage.setImageResource(R.drawable.proximity10);
                gyText.setTextColor(Color.WHITE);
                gyText.setTextSize(15);
        }
        @Override
        public void onSensorChanged(SensorEvent event) {
                // TODO Auto-generated method stub
                switch (event.sensor.getType()) {
                case Sensor.TYPE_GYROSCOPE:
                        gyText.setText("X轴上的角度为:"+event.values [SensorManager.DATA_X]
                                        +"\nY轴上的角度为:"+event.values [SensorManager.DATA_Y]
                                                        +"\nZ轴上的角度为:"+event.values [SensorManager.DATA_Z]);
                        int[] location = new int[2];

                        mgyrosImage.getLocationOnScreen(location);

                /*        int width = (location[0] + mgyrosImage.getRight() - mgyrosImage.getLeft()) / 2;

                        int height = (location[1] + mgyrosImage.getBottom() - mgyrosImage
                                        .getTop()) / 2;*/

                        final Animation animation = new RotateAnimation(gyros_num,
                                        event.values[SensorManager.DATA_X]*5, mgyrosImage.getWidth()/2,
                                        mgyrosImage.getHeight()/2);

                        animation.setDuration(2000);

                        mgyrosImage.startAnimation(animation);
                        gyros_num = event.values[SensorManager.DATA_X];
                        break;

                default:
                        break;
                }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
                // TODO Auto-generated method stub
               
        }
       
        @Override
        protected void onResume() {
                // TODO Auto-generated method stub
                super.onResume();
                sManager.registerListener(this, mSensor,Sensor.TYPE_GYROSCOPE);
        }
       
        @Override
        protected void onDestroy() {
                // TODO Auto-generated method stub
                super.onDestroy();
                sManager.unregisterListener(this);
        }
}
0 0
原创粉丝点击