相对运动图像

时间:2012-06-26 06:34:39

标签: android

我有一个围绕任意点旋转的图像。但我需要3个这样的图像同时旋转 这是我尝试实现相同的

private void rotateLogo(float degrees){
    Matrix matrix1 = new Matrix();
    //int radius = turntable.getWidth()/2;
    double radians = degrees* (Math.PI/180);
    double xcoordinate = 220 * Math.cos(radians)- 60;
    double ycoordinate = 220 * Math.sin(radians) + 50;
    matrix1.postRotate((int)radians, 220, 220);

    // people image
    FrameLayout.LayoutParams linLay  = (FrameLayout.LayoutParams) peopleLogo.getLayoutParams();
    linLay.bottomMargin = (int)ycoordinate + 10;
    linLay.rightMargin = (int)xcoordinate + 10;
    peopleLogo.setImageMatrix(matrix1);
    peopleLogo.setLayoutParams(linLay); 
    rotateLogo2(degrees - 2);
    }

private void rotateLogo2(float degrees){
    double radians = degrees* (Math.PI/180);
    double xcoordinate = 220 * Math.cos(radians)- 60;
    double ycoordinate = 220 * Math.sin(radians) + 50;

    // people image
    FrameLayout.LayoutParams linLay  = (FrameLayout.LayoutParams) serverLogo.getLayoutParams();
    linLay.bottomMargin = (int)ycoordinate + 10;
    linLay.rightMargin = (int)xcoordinate + 10;
    serverLogo.setLayoutParams(linLay);
}  

在上面的代码中我减小了角度并使用相同的代码旋转第二个图像,但图像不旋转,它只是在触摸时消失。

我还尝试在旋转前拍摄第一张图像的布局参数并将其应用到第二张图像,但这也不起作用..

图像在旋转时会消失但有时会消失

任何人都可以帮助我解决我可能出错的地方或建议任何其他方法吗?

1 个答案:

答案 0 :(得分:0)

您可以重构代码

private void rotateLogo(View logo, float degrees){
   Matrix matrix1 = new Matrix();
    //int radius = turntable.getWidth()/2;
    double radians = degrees* (Math.PI/180);
    double xcoordinate = 220 * Math.cos(radians)- 60;
    double ycoordinate = 220 * Math.sin(radians) + 50;
    matrix1.postRotate((int)radians, 220, 220);

    // people image
    FrameLayout.LayoutParams linLay  = (FrameLayout.LayoutParams) logo.getLayoutParams();
    linLay.bottomMargin = (int)ycoordinate + 10;
    linLay.rightMargin = (int)xcoordinate + 10;
    if (logo isInstanceOf ImageView) { 
        logo.setImageMatrix(matrix1);
    }
    logo.setLayoutParams(linLay); 
}

并使用不同的参数调用该方法两次..

rotateLogo(peopleLogo, degrees);
rotateLogo(serverLogo, degrees-2);