动画不适用于android gallery的第一个元素

动画不适用于android gallery的第一个元素,android,android-animation,Android,Android Animation,我用过画廊。画廊的每一项都占据了整个屏幕的宽度。我还放置了一个按钮来翻转。当第一个项目显示在库中时,如果我单击“翻转”按钮,它不会显示翻转动画。但是,如果我滚动库并转到另一个项目或再次返回到第一个项目,则动画工作正常。谁能猜出问题出在哪里 我使用所选项目的位置调用flipView方法。动画的方法和类如下所示: public void flipView(int position) { applyRotation(0, 90, position); isFrontShowing[po

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

我使用所选项目的位置调用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

公共类Rotate3动画扩展动画{ 私人最终学位; 私人最终浮动协议; 私人最终浮动mCenterX; 私人最终浮动麦肯锡; 私人最终浮动mDepthZ; 私有最终布尔mReverse; 私人摄像机麦卡梅拉; 公共旋转3数据化(从度浮动,从度浮动到度, 浮动中心X、浮动中心Y、浮动深度Z、布尔反转){ mFromDegrees=fromDegrees; mToDegrees=toDegrees; mCenterX=centerX; mCenterY=centerY; mDepthZ=深度; mReverse=反向; }

}

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

公共最终类DisplayNextView实现Animation.AnimationListener{ 私有布尔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) {
}
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);
    }
}
}

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

公共最终类SwapViews实现了Runnable{ 私有布尔错误优先视图; 相对布局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) {
}
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);
    }
}

}

经过多次尝试和错误,我找到了问题的解决方案

在onCreate方法中,我最后输入了以下代码:

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

            wordDetailAdapter.notifyDataSetChanged();
        };
    };
    Message msg = tempHandler.obtainMessage();
    tempHandler.sendMessageDelayed(msg, 500);
问题是当画廊第一次出现在屏幕上时,翻转动画不起作用。但是,如果我滚动画廊转到其他项目。在这之后,每一个项目的动画工作

这个解决方案奏效了,但我不知道为什么会出现这个问题。有一件事,问题不是发生在2.1版本中,而是发生在2.2及更高版本中

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

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