无法在Android应用程序中旋转图像

时间:2014-06-12 16:17:59

标签: java android image rotation gps

我目前正在编写一个Android应用程序,它应该根据用户当前位置将图像旋转到设置位置。我可以看出设置位置设置正确,当前位置似乎正在更新,但是应用程序只是放大图像而不执行任何操作,而不是旋转图像。真的很感激任何帮助!

  public double bearing(double lat1, double lon1, double lat2, double lon2) {
    double longitude1 = lon1;
    double longitude2 = lon2;
    double latitude1 = Math.toRadians(lat1);
    double latitude2 = Math.toRadians(lat2);
    double longDiff = Math.toRadians(longitude2 - longitude1);
    double y = Math.sin(longDiff) * Math.cos(latitude2);
    double x = Math.cos(latitude1) * Math.sin(latitude2) - Math.sin(latitude1) * Math.cos(latitude2) * Math.cos(longDiff);

    return (Math.toDegrees(Math.atan2(y, x)) + 360) % 360;
}

private void rotateImageView(ImageView imageView, int drawable, float rotate) {

    // Decode the drawable into a bitmap
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
            drawable);

    // Get the width/height of the drawable
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = bitmapOrg.getWidth(), height = bitmapOrg.getHeight();

    // Initialize a new Matrix
    Matrix matrix = new Matrix();

    // Decide on how much to rotate
    rotate = rotate % 360;

    // Actually rotate the image
    matrix.postRotate(rotate, width, height);

    // recreate the new Bitmap via a couple conditions
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
    //BitmapDrawable bmd = new BitmapDrawable( rotatedBitmap );

    //imageView.setImageBitmap( rotatedBitmap );
    imageView.setImageDrawable(new BitmapDrawable(getResources(), rotatedBitmap));
    imageView.setScaleType(ImageView.ScaleType.CENTER);
}

public void onLocationChange() {

    // If we don't have a Location, we break out
    if (currentLocation == null) return;

    double azimuth = bearing(currentLocation.getLatitude(), currentLocation.getLongitude(), baseLocation.getLatitude(), baseLocation.getLongitude());
    double baseAzimuth = azimuth;

    GeomagneticField geoField = new GeomagneticField(Double
            .valueOf(currentLocation.getLatitude()).floatValue(), Double
            .valueOf(currentLocation.getLongitude()).floatValue(),
            Double.valueOf(currentLocation.getAltitude()).floatValue(),
            System.currentTimeMillis()
    );

    azimuth -= geoField.getDeclination(); // converts magnetic north into true north

    // Store the bearingTo in the bearTo variable
    float bearTo = currentLocation.bearingTo(baseLocation);

    // If the bearTo is smaller than 0, add 360 to get the rotation clockwise.
    if (bearTo < 0) {
        bearTo = bearTo + 360;
    }

    //This is where we choose to point it
    double direction = bearTo - azimuth;

    // If the direction is smaller than 0, add 360 to get the rotation clockwise.
    if (direction < 0) {
        direction = direction + 360;
    }
    float fDir = (float) direction;
    rotateImageView((ImageView) findViewById(R.id.arrow), R.drawable.ic_launcher, fDir);

}


    private Runnable updateTimeChange = new Runnable() {
        public void run() {
            onLocationChange();
            customHandler.postDelayed(this, 500);
        }


    };

1 个答案:

答案 0 :(得分:0)

如果您的目标大于11,那么您可以尝试view.setRotation()方法:

image.setRotation(angle); // this is for your imageview where image is the imageview object

来自developers page。 getRotation()

  

设置视图围绕轴心点旋转的度数。增加值会导致顺时针旋转。

如果你想在xml中设置setRotation,xml标签是:

android:rotation