Android画廊幻灯片动画

4

我正在开发一个图库应用程序,在这个应用程序中,我使用画廊(gallery)以幻灯片形式展示图片。目前这个功能已经正常运行,但我想添加一些动画效果。使用这段代码,我只能应用一种动画效果,但我想在画廊视图中添加两种动画效果:从右到中心和从中心到左。请有经验的人帮我解决问题。

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

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

Thats all.....


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接