【问题标题】:Applying successive animations to ImageView in Android将连续动画应用于 Android 中的 ImageView
【发布时间】:2010-04-01 21:54:40
【问题描述】:

我想将连续动画(比如 ScaleAnimation)应用到显示资源图像的 ImageView。动画由按钮触发。例如,我想在每次单击按钮时逐步放大图像。

我在动画上设置了 fillAfter="true"。但是,所有的动画都是从 ImageView 的原始状态开始的。看起来好像 ImageView 重置了它的状态,动画总是一样的,而不是从上一个动画的最终状态开始。

我做错了什么?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button)findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            animate();

        }});
}

private void animate() {
    ImageView imageView = (ImageView) findViewById(R.id.ImageView01);

    ScaleAnimation scale = new ScaleAnimation((float)1.0, (float)1.5, (float)1.0, (float)1.5);
    scale.setFillAfter(true);
    scale.setDuration(500);
    imageView.startAnimation(scale); 
}

【问题讨论】:

    标签: android animation imageview drawable


    【解决方案1】:

    好像 ImageView 重置了 它的状态和动画总是 相同,而不是从 上一个动画的最终状态。

    没错! fillAfter="true" 肯定有用,但我还没有弄清楚它的意义。

    您需要做的是在每个相关的Animation 上设置一个AnimationListener,并在侦听器的onAnimationEnd() 中执行一些操作以实际保持动画的最终状态。我没有玩过ScaleAnimation,所以我不太确定“坚持最终状态”的方式是什么。如果这是一个AlphaAnimation,从1.0 到0.0,您可以在onAnimationEnd() 中创建小部件INVISIBLE 或GONE。

    【讨论】:

    • 感谢 CommonsWare。对于 AlphaAnimation 来说非常简单,但对于 ScaleAnimation 和 TranslateAnimation 来说,这样的解决方案需要一些工作。关于如何处理它的任何想法?也许它涉及到 setImageMatrix?
    • TranslateAnimation 要求您调整LayoutParams 以影响更改(例如,更改边距)或可能将小部件从一个父级移动到另一个父级(通过removeView() 和addView())。 ScaleAnimation...再说一次,我没用过,所以在这里我没有什么特别的建议。对不起!
    • 再次感谢。我正在考虑一个更通用的解决方案。就像向动画询问它的变换矩阵,然后将这样的矩阵应用于视图。不幸的是,我还不能让它工作。
    【解决方案2】:

    我遇到了同样的问题并创建了以下代码来轻松使用不同的动画。它目前只支持平移和 alpha 级别,因为我没有使用缩放,但可以轻松扩展以支持更多功能。

    我在开始动画之前重置了滚动和可见性,但这只是因为我需要开/关动画。

    并且“doEnd”布尔值是为了避免递归堆栈溢出(scrollTo 调用 onAnimationEnd 出于某种晦涩的原因......)

    private void setViewPos(View view, Animation anim, long time){
        // Get the transformation
        Transformation trans = new Transformation();
        anim.getTransformation(time, trans);
    
        // Get the matrix values
        float[] values = new float[9];
        Matrix m = trans.getMatrix();
        m.getValues(values);
    
        // Get the position and apply the scroll
        final float x = values[Matrix.MTRANS_X];
        final float y = values[Matrix.MTRANS_Y];
        view.scrollTo(-(int)x, -(int)y);
    
        // Show/hide depending on final alpha level
        if (trans.getAlpha() > 0.5){
            view.setVisibility(VISIBLE);
        } else {
            view.setVisibility(INVISIBLE);       
        }
    }
    
    private void applyAnimation(final View view, final Animation anim){
        view.scrollTo(0, 0);
        view.setVisibility(VISIBLE);
    
        anim.setAnimationListener(new AnimationListener(){
            private boolean doEnd = true;
    
            @Override
            public void onAnimationEnd(Animation animation) {
                if (doEnd){
                    doEnd = false;
                    setViewPos(view, animation, anim.getStartTime() + anim.getDuration());
                }
            }
    
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
    
            @Override
            public void onAnimationStart(Animation animation) {
            }
    
        });
    
        view.startAnimation(anim);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-03
      • 2016-08-22
      • 2022-08-14
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多