Android画廊幻灯片动画

我正在开发一个画廊应用程序,在这个应用程序中,我正在使用画廊以幻灯片方式显示图像.它工作正常,但我想添加一些动画.我只能使用此代码应用一个动画,但我想在图库视图中添加两个动画效果.

说翻译效果,从右到中,从中到左.请有人帮帮我.

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);  

    }
}

最佳答案 使用基于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);  

就这样…..

点赞