ZigZag动画的android中的图像

时间:2013-11-24 11:14:48

标签: android animation android-animation

我正在使用一个Android应用程序,其中我有10个气球 我想以锯齿形风格为这些气球制作动画。 我正在使用valueanimator 我的代码是

    Display display = getWindowManager().getDefaultDisplay(); 
    display.getRectSize(mDisplaySize);

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    mScale = metrics.density;

    mRootLayout = (RelativeLayout) findViewById(R.id.main_layout);

    new Timer().schedule(new ExeTimerTask(), 0, 2000);
}

public void startAnimation(final ImageView aniView) {

    aniView.setPivotX(aniView.getWidth()/2);
    aniView.setPivotY(aniView.getHeight()/2);
    aniView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Toast.makeText(getBaseContext(), "Clicked", Toast.LENGTH_SHORT).show();

        }
    });
    long delay = new Random().nextInt(Constants.MAX_DELAY);

    final ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
    animator.setDuration(Constants.ANIM_DURATION);
    animator.setInterpolator(new AccelerateInterpolator());
    animator.setStartDelay(delay);

    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

    //  int angle = 40 + (int)(Math.random() * 101);
        //int angle = 40 + (int)(Math.random() * 101);
        int movex = new Random().nextInt(mDisplaySize.right);

        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            float value = ((Float) (animation.getAnimatedValue())).floatValue();
        //  aniView.setRotation(angle*value);
            aniView.setTranslationX((movex-40)*value);
            aniView.setTranslationY((mDisplaySize.bottom + (150*mScale))*value);



        }
    });

    animator.start();

}

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        int viewId = new Random().nextInt(LEAVES.length);
        Drawable d = getResources().getDrawable(LEAVES[viewId]);
        LayoutInflater inflate = LayoutInflater.from(FallAnimationActivity.this);
        ImageView imageView = (ImageView) inflate.inflate(R.layout.ani_image_view, null);
        imageView.setImageDrawable(d);
        mRootLayout.addView(imageView);

        mAllImageViews.add(imageView);          

        LayoutParams animationLayout = (LayoutParams) imageView.getLayoutParams();
        animationLayout.setMargins(0, (int)(-150*mScale), 0, 0);
        animationLayout.width = (int) (60*mScale);
        animationLayout.height = (int) (60*mScale);

        startAnimation(imageView);
    }
};

private class ExeTimerTask extends TimerTask {
    @Override
    public void run() {
        // we don't really use the message 'what' but we have to specify something.
        mHandler.sendEmptyMessage(Constants.EMPTY_MESSAGE_WHAT);
    }
}

但它的动作不是曲折如何制作动画曲折任何想法提前感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

class ZigZagAnimation extends Animation {
    private PathMeasure pm;
    float[] pos = new float[2];

    public ZigZagAnimation() {
        Path p = new Path();
        p.moveTo(0f, 0f);
        p.lineTo(12f, 5f);
        p.lineTo(8f, 14f);
        p.lineTo(25f, 17f);
        p.lineTo(13f, 31f);
        pm = new PathMeasure(p, false);
        setDuration(4000);
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        float distance = pm.getLength() * interpolatedTime;
        pm.getPosTan(distance, pos, null);
        t.getMatrix().postTranslate(pos[0], pos[1]);
    }
}