在android中旋转螺旋路径中的图像

时间:2015-06-05 15:27:42

标签: android android-animation

我已使用此代码在螺旋路径中旋转ImageView。但是在旋转第一个圆圈后,它会回到中心并开始旋转下一个圆圈。如何避免将图像视图移回中心?谢谢

public class MainActivity extends ActionBarActivity implements Animation.AnimationListener {

RelativeLayout.LayoutParams par;
ImageView star;
TranslateAnimation animation1,animation2,animation3;
AnimationSet animSet1 = new AnimationSet(false);
AnimationSet animSet2 = new AnimationSet(true);
AnimationSet animSet3 = new AnimationSet(true);
AnimationSet animSet4 = new AnimationSet(true);
RelativeLayout screen;
int width = 0;
int height = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    star = (ImageView)findViewById(R.id.imageView);
    par = (RelativeLayout.LayoutParams)star.getLayoutParams();
    screen = (RelativeLayout) findViewById(R.id.phoneScreen);

    DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    height = displaymetrics.heightPixels;
    width = displaymetrics.widthPixels;


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onAnimationEnd(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationStart(Animation animation) {
}

public class MyAnimation extends Animation {

    private View view;
    private float cx, cy;           // center x,y position of circular path
    private float prevX, prevY;     // previous x,y position of image during animation
    private float r;                // radius of circle


    /**
     * @param view - View that will be animated
     * @param r - radius of circular path
     */
    public MyAnimation(View view, float r){
        this.view = view;
        this.r = r;
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        // calculate position of image center
        int cxImage = width / 2;
        int cyImage = height / 2;

        cx = view.getLeft() + cxImage;
        cy = view.getTop() + cyImage;

        // set previous position to center
        prevX = cx;
        prevY = cy;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        if(interpolatedTime == 0){
            // I ran into some issue where interpolated would be
            return;
        }

        float angleDeg = (interpolatedTime * 360f + 90) % 360;
        float angleRad = (float) Math.toRadians(angleDeg);

        // r = radius, cx and cy = center point, a = angle (radians)
        float x = (float) (cx + r * Math.cos(angleRad));
        float y = (float) (cy + r * Math.sin(angleRad));


        float dx = prevX - x;
        float dy = prevY - y;

        prevX = x;
        prevY = y;

        t.getMatrix().setTranslate(dx, dy);
    }
}
public void buttonClick(View v)
{
    Animation anim = new MyAnimation(star, 150);
    anim.setDuration(4000);

    anim.setAnimationListener(new Animation.AnimationListener(){
        @Override
        public void onAnimationStart(Animation arg0) {
        }
        @Override
        public void onAnimationRepeat(Animation arg0) {

        }
        @Override
        public void onAnimationEnd(Animation arg0) {

            Animation anim2 = new MyAnimation(star, 145);
            anim2.setDuration(4000);
            star.startAnimation(anim2);
        }
    });
    star.startAnimation(anim);


}
}

1 个答案:

答案 0 :(得分:0)

而不是做

t.getMatrix().setTranslate(dx, dy);

DO

//you will need to change your math here a bit to absolute position 
//instead of change in position.
star.setX(newXPosition); 
star.setY(newYPosition);

这是因为您现在正在进行的转换只会改变绘制视图的位置,而不是实际的位置。