Android gallery slideshow animation

匿名 (未验证) 提交于 2019-12-03 08:52:47

问题:

I'm developing a gallery app, in this app I'm displaying images in slideshow using gallery. It's working fine, but I want to add some animation. I can apply only one animation with this code but I want to add two animation effects in gallery view. Say in translation effect, right-to-center and center-to-left. Please anyone help me out.

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:

Use Xml based animation Create a Xml file in folder res/anim/animate.xml

put the code

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

now in your function myslideshow() change

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

Thats all.....



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!