List of usage examples for android.graphics Matrix setRotate
public void setRotate(float degrees, float px, float py)
From source file:uk.org.rivernile.edinburghbustracker.android.fragments.general.BusStopDetailsFragment.java
/** * Update the direction needle so that it is pointing towards the bus stop, * based on the device location and the direction it is facing. */// ww w. j ava2 s . c o m private void updateDirectionNeedle() { // We need values for location, the accelerometer and magnetometer to // continue. if (lastLocation == null || accelerometerValues == null || magnetometerValues == null) { // Make sure the needle isn't showing. txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); recycleNeedleBitmapIfNotNull(null); return; } // Calculating the rotation matrix may fail, for example, if the device // is in freefall. In that case we cannot continue as the values will // be unreliable. if (!SensorManager.getRotationMatrix(rotationMatrix, null, accelerometerValues, magnetometerValues)) { return; } // The screen rotation was obtained earlier. switch (screenRotation) { // There's lots of information about this elsewhere, but briefly; // The values from the sensors are in the device's coordinate system // which may be correct if the device is in its natural orientation, // but it needs to be remapped if the device is rotated. case Surface.ROTATION_0: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_X, SensorManager.AXIS_Z, rotationMatrix); break; case Surface.ROTATION_90: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_Z, SensorManager.AXIS_MINUS_X, rotationMatrix); break; case Surface.ROTATION_180: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_MINUS_Z, rotationMatrix); break; case Surface.ROTATION_270: SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_Z, SensorManager.AXIS_X, rotationMatrix); break; } // Get the X, Y and Z orientations, which are in radians. Covert this // in to degrees East of North. SensorManager.getOrientation(rotationMatrix, headings); double heading = Math.toDegrees(headings[0]); // If there's a GeomagneticField value, then adjust the heading to take // this in to account. if (geoField != null) { heading -= geoField.getDeclination(); } // The orientation is in the range of -180 to +180. Convert this in to // a range of 0 to 360. final float bearingTo = distance[1] < 0 ? distance[1] + 360 : distance[1]; // This is the heading to the bus stop. heading = bearingTo - heading; // The above calculation may come out as a negative number again. Put // this back in to the range of 0 to 360. if (heading < 0) { heading += 360; } // This 'if' statement is required to prevent a crash during device // rotation. It ensured that the Fragment is still part of the Activity. if (isAdded()) { // Get the arrow bitmap from the resources. final Bitmap needleIn = BitmapFactory.decodeResource(getResources(), R.drawable.heading_arrow); // Get an identity matrix and rotate it by the required amount. final Matrix m = new Matrix(); m.setRotate((float) heading % 360, (float) needleIn.getWidth() / 2, (float) needleIn.getHeight() / 2); // Apply the rotation matrix to the Bitmap, to create a new Bitmap. final Bitmap needleOut = Bitmap.createBitmap(needleIn, 0, 0, needleIn.getWidth(), needleIn.getHeight(), m, true); // Recycle the needle read in if it's not the same as the rotated // needle. if (needleIn != needleOut) { needleIn.recycle(); } // This Bitmap needs to be converted to a Drawable type. final BitmapDrawable drawable = new BitmapDrawable(getResources(), needleOut); // Set the new needle to be on the right hand side of the TextView. txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null); recycleNeedleBitmapIfNotNull(needleOut); } else { // If the Fragment is not added to the Activity, then make sure // there's no needle. txtDistance.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null); recycleNeedleBitmapIfNotNull(null); } }
From source file:com.kjsaw.alcosys.ibacapp.IBAC.java
private Bitmap processBitmap(Bitmap orginalBitmap, int degrees) { Matrix matrixRotate = new Matrix(); matrixRotate.setRotate(degrees, (float) orginalBitmap.getWidth() / 2, (float) orginalBitmap.getHeight() / 2); Bitmap rotatedBitmap = Bitmap.createBitmap(orginalBitmap, 0, 0, orginalBitmap.getWidth(), orginalBitmap.getHeight(), matrixRotate, true); int width = rotatedBitmap.getWidth(); int height = rotatedBitmap.getHeight(); int newWidth = 240; int newHeight = 320; float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrixResize = new Matrix(); if (Session.isFacingCamera) { matrixResize.setScale(-1, 1);// reversal }/*from www . j ava 2 s. c om*/ matrixResize.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(rotatedBitmap, 0, 0, width, height, matrixResize, true); }