4

我正在开发一个画廊应用程序,在这个应用程序中,我使用画廊在幻灯片中显示图像。它工作正常,但我想添加一些动画。我只能使用此代码应用一个动画,但我想在图库视图中添加两个动画效果。在翻译效果中说,从右到中心和从中心到左。请任何人帮助我。

public void slidShow(){

     Runnable runnable = new Runnable() {

        @Override
        public void run() {
            myslideshow();
            handler.postDelayed(this, 3000);                
        }
    };
    new Thread(runnable).start();
}

private void myslideshow(){
    PicPosition = gallery.getSelectedItemPosition() +1;             
    if (PicPosition >=  bitmaps.size()){
        PicPosition =  gallery.getSelectedItemPosition(); //stop    
    } 
    else{
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f,
                Animation.RELATIVE_TO_PARENT,  0.0f);
        inFromRight.setDuration(500);
        inFromRight.setInterpolator(new AccelerateInterpolator()); 
        gallery.startAnimation(inFromRight);
        gallery.setSelection(PicPosition);  

    }
}
4

1 回答 1

2

使用基于 Xml 的动画 在文件夹 res/anim/animate.xml 中创建一个 Xml 文件

把代码

  <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true">
    <translate 
        android:fromXDelta="0%p" android:toXDelta="50%p" // change this to decide the range
        android:duration="500" android:startOffset="0"/>
    <translate 
        android:fromXDelta="0%p" android:toXDelta="100%p" 
        android:duration="500" android:startOffset="500"/>// change this to increase the 
time for image to stay 

</set>

现在在你的函数 myslideshow() 改变

Animation inFromRight =  AnimationUtils.loadAnimation(this, R.anim.animate);
gallery.startAnimation(inFromRight);
        gallery.setSelection(PicPosition);  

就这样.....

于 2012-10-12T11:38:53.393 回答