我使用过画廊。图库的每个项目都占据了屏幕的整个宽度。我还放了一个按钮来翻转。如果我在图库中显示第一个项目,如果单击翻转按钮,则不会显示翻转动画。但是,如果我滚动图库并转到另一个项目或再次返回到第一个项目,则动画可以正常工作。任何人都可以猜到问题出在哪里。
我用选定项目的位置调用方法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;
import android.view.animation.Transformation; import android.graphics.Camera; import android.graphics.Matrix;
public class Rotate3dAnimation extends Animation { 私人最终浮动mFromDegrees; 私人最终浮动mToDegrees; 私人最终浮动mCenterX; 私人最终浮动mCenterY; 私人最终浮动mDepthZ; private final boolean mReverse; 私人相机mCamera; public Rotate3dAnimation(float fromDegrees,float toDegrees, float centerX,float centerY,float depthZ,boolean reverse){ mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; 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;
import android.view.animation.Animation; import android.widget.RelativeLayout;
public final class DisplayNextView实现Animation.AnimationListener { private boolean mCurrentView; RelativeLayout layout1; RelativeLayout layout2;
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;
import android.view.View; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; import android.view.animation.Animation.AnimationListener; import android.widget.RelativeLayout;
public final class SwapViews实现Runnable { private boolean mIsFirstView; RelativeLayout layout1; RelativeLayout layout2;
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);
}
}
}
答案 0 :(得分: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及更高版本中。