0

我用过画廊。图库的每个项目都占据屏幕的整个宽度。我还放置了一个按钮来翻转。当第一个项目显示在画廊中时,如果我单击翻转按钮,它不会显示翻转动画。但是,如果我滚动图库并转到另一个项目或再次返回第一个项目,则动画可以正常工作。谁能猜出问题出在哪里。

我用所选项目的位置调用方法flipView。动画的方法和类如下:

public void flipView(int position) {
    applyRotation(0, 90, position);
    isFrontShowing[position] = !isFrontShowing[position];       
}

private void applyRotation(float start, float end, int position) {
    // Find the center of image
    final float centerX, centerY;
    if(isFrontShowing[position] == true) {
        centerX = detailsLayout[position].getMeasuredWidth() / 2.0f;
        centerY = detailsLayout[position].getMeasuredHeight() / 2.0f;
        detailsLayout[position].requestFocus();
        detailsLayout[position].bringToFront();
    } else {
        centerX = scriptLayout[position].getMeasuredWidth() / 2.0f;
        centerY = scriptLayout[position].getMeasuredHeight() / 2.0f;
        scriptLayout[position].requestFocus();
        scriptLayout[position].bringToFront();
    }


    final Rotate3dAnimation rotation =
        new Rotate3dAnimation(start, end, centerX, centerY, 200.0f, true);
    rotation.setDuration(350);
    rotation.setFillAfter(true);
    rotation.setInterpolator(new LinearInterpolator());
    rotation.setAnimationListener(new DisplayNextView(isFrontShowing[position], detailsLayout[position], scriptLayout[position]));

    if (isFrontShowing[position] == true)
    {
        detailsLayout[position].startAnimation(rotation);
    } else {
        //System.out.println("---Backward flipping started...");

        scriptLayout[position].startAnimation(rotation);
    }

}



import android.view.animation.Animation;

导入android.view.animation.Transformation;导入android.graphics.Camera;导入android.graphics.Matrix;

公共类 Rotate3dAnimation 扩展动画 { 私有最终浮点 mFromDegrees; 私人最终浮动 mToDegrees;私人最终浮动 mCenterX;私人最终浮动 mCenterY;私人最终浮动 mDepthZ;私有最终布尔值 mReverse;私人相机 mCamera;public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = 中心X;mCenterY = 中心Y;mDepthZ = 深度Z;mReverse = 反向;}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
    super.initialize(width, height, parentWidth, parentHeight);
    mCamera = new Camera();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    final float fromDegrees = mFromDegrees;
    float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

    final float centerX = mCenterX;
    final float centerY = mCenterY;
    final Camera camera = mCamera;

    final Matrix matrix = t.getMatrix();

    camera.save();
    if (mReverse) {
        camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
    } else {
        camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
    }
    camera.rotateY(degrees);
    camera.getMatrix(matrix);
    camera.restore();

    matrix.preTranslate(-centerX, -centerY);
    matrix.postTranslate(centerX, centerY);
}

}

package com.vocabAhead.SATVocab;

导入android.view.animation.Animation;导入 android.widget.RelativeLayout;

公共最终类 DisplayNextView 实现 Animation.AnimationListener { private boolean mCurrentView; 相对布局布局1​​;相对布局布局2;

public DisplayNextView(boolean currentView, RelativeLayout layout1,
        RelativeLayout layout2) {
    mCurrentView = currentView;
    this.layout1 = layout1;
    this.layout2 = layout2;
}

public void onAnimationStart(Animation animation) {

}

public void onAnimationEnd(Animation animation) {
    if(mCurrentView == true)
        layout1.post(new SwapViews(mCurrentView, layout1, layout2));        
    else
        layout2.post(new SwapViews(mCurrentView, layout1, layout2));
}

public void onAnimationRepeat(Animation animation) {
}

}

package com.vocabAhead.SATVocab;

导入android.view.View;导入android.view.animation.Animation;导入 android.view.animation.LinearInterpolator;导入 android.view.animation.Animation.AnimationListener;导入 android.widget.RelativeLayout;

公共最终类 SwapViews 实现 Runnable { private boolean mIsFirstView; 相对布局布局1​​;相对布局布局2;

public SwapViews(boolean isFirstView, RelativeLayout layout1, RelativeLayout layout2) {
    mIsFirstView = isFirstView;
    this.layout1 = layout1;
    this.layout2 = layout2;
}

public void run() {
    final float centerX, centerY;
    if(mIsFirstView) {
        centerX = layout1.getWidth() / 2.0f;
        centerY = layout1.getHeight() / 2.0f;
    } else {
        centerX = layout2.getWidth() / 2.0f;
        centerY = layout2.getHeight() / 2.0f;
    }

    Rotate3dAnimation rotation;

    if (mIsFirstView == true) {
        layout1.setVisibility(View.GONE);
        layout2.setVisibility(View.VISIBLE);
        layout2.requestFocus();
        layout2.bringToFront();
        rotation = new Rotate3dAnimation( -90, 0, centerX, centerY, 200.0f, false);
    } else {
        layout2.setVisibility(View.GONE);
        layout1.setVisibility(View.VISIBLE);
        layout1.requestFocus();
        layout1.bringToFront();
        rotation = new Rotate3dAnimation(-90, 0, centerX, centerY, 200.0f, false);
        //rotation = new Flip3dAnimation(-90, 0, centerX, centerY);
    }

    rotation.setDuration(350);
    rotation.setFillAfter(true);
    rotation.setInterpolator(new LinearInterpolator());
    rotation.setAnimationListener(new AnimationListener() {

        public void onAnimationStart(Animation arg0) {

        }

        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub

        }

        public void onAnimationEnd(Animation arg0) {
            WordDetailItemAdapter.notifyAdapter();
        }
    });
    if (mIsFirstView == true) {
        layout2.startAnimation(rotation);
    } else {
        layout1.startAnimation(rotation);
    }
}

}

4

1 回答 1

1

经过多次试验和错误,我找到了解决问题的方法。

在最后的 onCreate 方法中,我放置了以下代码:

Handler tempHandler = new Handler() {
        public void handleMessage(Message msg) {

            wordDetailAdapter.notifyDataSetChanged();
        };
    };
    Message msg = tempHandler.obtainMessage();
    tempHandler.sendMessageDelayed(msg, 500);

问题是当第一次在屏幕上显示画廊时,翻转动画不起作用。但是,如果我滚动画廊去其他项目。之后,对于每个项目,动画都在工作。

该解决方案有效,但我不知道为什么会出现问题。有一件事,这个问题没有出现在 2.1 版本中,它出现在 2.2 及更高版本中。

于 2011-05-19T18:58:37.837 回答