package com.compass;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Config;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
@SuppressWarnings("deprecation")
public class Compass extends Activity {
private static final String TAG = "Compass";
private SensorManager mSensorManager;
private SampleView mView;
private float[] mValues;
private final SensorListener mListener = new SensorListener() {
public void onSensorChanged(int sensor, float[] values) {
if (Config.LOGD) Log.d(TAG, "sensorChanged (" + values[0] + ", " + values[1] + ", " + values[2] + ")");
mValues = values;
if (mView != null) {
mView.invalidate();
}
}
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
};
@SuppressWarnings("unused")
@Override
public void setContentView(View view) {
if (false) { // set to true to test Picture
ViewGroup vg = new PictureLayout(this);
vg.addView(view);
view = vg;
}
super.setContentView(view);
}
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
mView = new SampleView(this);
setContentView(mView);
File a = new File("/sdcard/");
try{
File.createTempFile("test", "txt", a);
}
catch(IOException e){
}
}
@Override
protected void onResume()
{
if (Config.LOGD) Log.d(TAG, "onResume");
super.onResume();
mSensorManager.registerListener(mListener,
SensorManager.SENSOR_ORIENTATION,
SensorManager.SENSOR_DELAY_GAME);
}
@Override
protected void onStop()
{
if (Config.LOGD) Log.d(TAG, "onStop");
mSensorManager.unregisterListener(mListener);
super.onStop();
}
private class SampleView extends View {
private Paint mPaint = new Paint();
private Bitmap[] mBitmapArray = new Bitmap[6];
InputStream is;
int[] mBitmapWidth = new int[6];
int[] mBitmapHeight = new int[6];
private Resources mRes;
private boolean mAnimate;
public SampleView(Context context) {
super(context);
mRes = Compass.this.getResources();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = false; // this will request the bm
opts.inSampleSize = 2; // scaled down by 2
SetBitmapArray(0, opts, R.drawable.panel);
SetBitmapArray(1, opts, R.drawable.needle);
SetBitmapArray(2, opts, R.drawable.compass_degree);
}
private void SetBitmapArray(int index, BitmapFactory.Options opts, int resId){
is = mRes.openRawResource(resId);
mBitmapArray[index] = BitmapFactory.decodeStream(is);//300*300
mBitmapWidth[index] = mBitmapArray[index].getWidth();
mBitmapHeight[index] = mBitmapArray[index].getHeight();
mBitmapArray[index+3] = BitmapFactory.decodeStream(is, null, opts);
mBitmapWidth[index+3] = mBitmapArray[index+3].getWidth();
mBitmapHeight[index+3] = mBitmapArray[index+3].getHeight();
}
@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;
paint.setColor(Color.RED);
canvas.drawColor(Color.GRAY);
paint.setAntiAlias(true);
int w = canvas.getWidth();
int h = canvas.getHeight();
int cx = w / 2;
int cy = h / 2;
int mCurrentOrientation = getResources().getConfiguration().orientation;
if ( mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT ) {
// If current screen is portrait
canvas.translate(cx, cy);
drawPictures(canvas,0);
} else if ( mCurrentOrientation == Configuration.ORIENTATION_LANDSCAPE ) {
//If current screen is landscape
canvas.translate(cx, cy-20);
drawPictures(canvas,3);
}
}
private void drawPictures(Canvas canvas, int idDelta){
if (mValues != null) {
// Log.d(TAG, "mValues[0] = "+ mValues[0]);
canvas.rotate(-mValues[0]);
canvas.drawBitmap(mBitmapArray[0+idDelta], -mBitmapWidth[0+idDelta]/2, -mBitmapHeight[0+idDelta]/2, mPaint);
canvas.drawBitmap(mBitmapArray[1+idDelta], -mBitmapWidth[1+idDelta]/2, -mBitmapHeight[1+idDelta]/2, mPaint);
canvas.rotate(360+mValues[0]);
canvas.drawBitmap(mBitmapArray[2+idDelta], -mBitmapWidth[2+idDelta]/2, -mBitmapHeight[2+idDelta]/2, mPaint);
}
else{
canvas.drawBitmap(mBitmapArray[0+idDelta], -mBitmapWidth[0+idDelta]/2, -mBitmapHeight[0+idDelta]/2, mPaint);
canvas.drawBitmap(mBitmapArray[1+idDelta], -mBitmapWidth[1+idDelta]/2, -mBitmapHeight[1+idDelta]/2, mPaint);
canvas.drawBitmap(mBitmapArray[2+idDelta], -mBitmapWidth[2+idDelta]/2, -mBitmapHeight[2+idDelta]/2, mPaint);
}
}
@Override
protected void onAttachedToWindow() {
setmAnimate(true);
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
setmAnimate(false);
super.onDetachedFromWindow();
}
public void setmAnimate(boolean mAnimate) {
this.mAnimate = mAnimate;
}
@SuppressWarnings("unused")
public boolean ismAnimate() {
return mAnimate;
}
}
}
Android应用源码之指南针 源码-IT计算机-毕业设计.zip
版权申诉
5星 · 超过95%的资源 167 浏览量
2022-04-06
21:58:21
上传
评论
收藏 383KB ZIP 举报
芝麻粒儿
- 粉丝: 5w+
- 资源: 2万+
最新资源
- 基于pytorch+Unet进行MRI肝脏图像分割源码+数据集+模型.zip
- 在Android Studio中开发一个Android App项目步骤
- 基于yolov8实现进行物体跟踪源码.zip
- Java多线程学习Java多线程学习Java多线程学习Java多线程学习.txt
- 算法数据结构-动态规划算法(Dynamic Programming)超详细总结加应用案例讲解.txt
- 2024最强秋招八股文(精简、纯手打)2024最强秋招八股文(精简、纯手打).txt
- 基于tensorflow多特征融合的微表情识别python源码.zip
- 基于yolov8实现人脸检测的python源码+运行说明.zip
- Micron Memory DDR3 SDRAM 全系列AD集成库(原理图库+PCB封装库).IntLib
- 基于tensorflow多特征融合的微表情识别python源码+详细使用说明.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈