Android: How to rotate a moving animated sprite based on the coordinates of its destination

前端 未结 2 741
感动是毒
感动是毒 2020-12-14 05:11

My application fires up sprite instances around a Canvas which then move across the screen towards a x/y coordinate. I would like to be able to rotate the sprite around its

2条回答
  •  温柔的废话
    2020-12-14 05:42

    using this referance to calculate Angle:

    private double angleFromCoordinate(double lat1, double long1, double lat2,
            double long2) {
    
        double dLon = (long2 - long1);
    
        double y = Math.sin(dLon) * Math.cos(lat2);
        double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
                * Math.cos(lat2) * Math.cos(dLon);
    
        double brng = Math.atan2(y, x);
    
        brng = Math.toDegrees(brng);
        brng = (brng + 360) % 360;
        brng = 360 - brng;
    
        return brng;
    }
    

    and then rotate ImageView to this angle

    private void rotateImage(ImageView imageView, double angle) {
    
        Matrix matrix = new Matrix();
        imageView.setScaleType(ScaleType.MATRIX); // required
        matrix.postRotate((float) angle, imageView.getDrawable().getBounds()
                .width() / 2, imageView.getDrawable().getBounds().height() / 2);
        imageView.setImageMatrix(matrix);
    }
    

提交回复
热议问题