我需要做的是在圆圈外边缘绘制的位图 我有时会把它弄好,但是当我旋转设备时它会出错 我也尝试了旋转动画,但我没有得到我想要的结果。
请帮忙。
这是预期的:
我得到的是:对于这张图片,它运作良好
它在这里出错了
在这里:
代码(指南针的谷歌教程):
CompassFragment类:
public class CompassFragment extends Fragment implements SensorEventListener {
SensorManager sensorManager;
private Sensor sensorAccelerometer;
private Sensor sensorMagneticField;
private float[] valuesAccelerometer;
private float[] valuesMagneticField;
private float[] matrixR;
private float[] matrixI;
private float[] matrixValues;
TextView readingAzimuth, readingPitch, readingRoll;
Compass myCompass;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_compass, container, false);
readingAzimuth = (TextView) view.findViewById(R.id.azimuth);
readingPitch = (TextView) view.findViewById(R.id.pitch);
readingRoll = (TextView) view.findViewById(R.id.roll);
myCompass = (Compass) view.findViewById(R.id.mycompass);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sensorManager = (SensorManager) getActivity().getSystemService(Context.SENSOR_SERVICE);
sensorAccelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sensorMagneticField = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
valuesAccelerometer = new float[3];
valuesMagneticField = new float[3];
matrixR = new float[9];
matrixI = new float[9];
matrixValues = new float[3];
}
@Override
public void onResume() {
sensorManager.registerListener(this,
sensorAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorMagneticField,
SensorManager.SENSOR_DELAY_NORMAL);
super.onResume();
}
@Override
public void onPause() {
sensorManager.unregisterListener(this,
sensorAccelerometer);
sensorManager.unregisterListener(this,
sensorMagneticField);
super.onPause();
}
@Override
public void onSensorChanged(SensorEvent event) {
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
for (int i = 0; i < 3; i++) {
valuesAccelerometer[i] = event.values[i];
}
break;
case Sensor.TYPE_MAGNETIC_FIELD:
for (int i = 0; i < 3; i++) {
valuesMagneticField[i] = event.values[i];
}
break;
}
boolean success = SensorManager.getRotationMatrix(
matrixR,
matrixI,
valuesAccelerometer,
valuesMagneticField);
if (success) {
SensorManager.getOrientation(matrixR, matrixValues);
double azimuth = Math.toDegrees(matrixValues[0]);
double pitch = Math.toDegrees(matrixValues[1]);
double roll = Math.toDegrees(matrixValues[2]);
readingAzimuth.setText("Azimuth: " + String.valueOf(azimuth));
readingPitch.setText("Pitch: " + String.valueOf(pitch));
readingRoll.setText("Roll: " + String.valueOf(roll));
myCompass.update(matrixValues[0]);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
指南针课程:
public class Compass extends View {
private Bitmap bitmap;
private float direction;
private HashMap<String, Bitmap> mStore = new HashMap<String, Bitmap>();
private Context context;
private int h, w, r;
private Paint paint;
private Canvas canvas;
public Compass(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context = context;
}
public Compass(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Compass(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-ge
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(
MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
protected void onDraw(Canvas canvas) {
w = getMeasuredWidth();
h = getMeasuredHeight();
r = 0;
if (w > h) {
r = h / 2;
} else {
r = w / 2;
}
paint = new Paint();
// Paint.ANTI_ALIAS_FLAG
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
canvas.drawCircle(w / 2, h / 2, r, paint);
paint.setColor(Color.RED);
drawImage1(canvas);
}
private void drawImage1(Canvas canvas) {
BitmapFactory.Options myOptions = new BitmapFactory.Options();
myOptions.inDither = true;
myOptions.inScaled = false;
myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important
myOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher, myOptions);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
float left = (float) ((w / 2 + r * Math.sin(-direction)));
float top = (float) (h / 2 - r * Math.cos(-direction));
Log.e("cords", left + " " + top);
canvas.drawBitmap(mutableBitmap, left, top, paint);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/azimuth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/mainColor" />
<TextView
android:id="@+id/pitch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/mainColor" />
<TextView
android:id="@+id/roll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/mainColor" />
</LinearLayout>
<com.example.compass.Compass
android:id="@+id/mycompass"
class="com.nilecode.Compass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
</com.example.compass.Compass>
</LinearLayout>