Hi. Hi have an absolute layout with pictures. Only 2 pictures are show simultaneously. Others are on the layout, but out of view.
I wan't on user click to show others picture. For this, I do an animation to translate the layer. For translation from right to left, it works correctly : an other picture come from right progressively while another exit on left side. But, for translation from left to right, it don't work : the right picture exits correctly on right side, but left picture only appear on left side at the end of the animation. while translating, an empty space appear on left until the end of translation. For translating, I make my own animation (not taken from XML because it depends on image size) : scrollLeft = new TranslateAnimation(0, -(pictoWidth+hspace), 0, 0); scrollLeft.setDuration(300); scrollLeft.setAnimationListener(this); scrollRight = new TranslateAnimation(0, (pictoWidth+hspace), 0, 0); scrollRight.setDuration(300); scrollRight.setAnimationListener(this); then, for translating, I do : layout.startAnimation(scrollRight); (I join the widget source in case somebody is interesting on doing the same thing ...) Does somebody have any idea how tohave the same effect on left to right scroll than right to left ? Thanks in advance. Mike PS: I can't use filpper because I have to picture visible at the same time and only one disappear at a time. It ould be possible but more complicated I thing ... --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---
package mba.android.widgets; import android.content.Context; import android.graphics.Color; import android.util.AttributeSet; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.view.animation.Animation.AnimationListener; import android.widget.AbsoluteLayout; import android.widget.FrameLayout; import android.widget.ImageButton; import com.fnacspectacles.android.R; public class MbaFlipper extends FrameLayout implements AnimationListener { private ImageButton[] buttons = null; private AbsoluteLayout layout = null; private Animation scrollLeft, scrollRight; private boolean wasNext = false; public MbaFlipper(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MbaFlipper(Context context) { super(context); init(); } public int getNbManifs() { return 8; } private int pictoWidth=65, pictoHeight=80, hspace = 20, border = 1, vspace = 3, offset = hspace / 2; /** * Appelé au début pour initialiser les vues. * Ensuite, il faudra appeler update avec les infos spécifiques à la manif. */ private void init() { layout = new AbsoluteLayout(getContext()); LayoutParams paramsLayout = new LayoutParams(getNbManifs()*(pictoWidth+hspace+2*border), LayoutParams.FILL_PARENT); addView(layout, paramsLayout); layout.setPadding(-1 * (pictoWidth+hspace+2*border), 0, 0, 0); buttons = new ImageButton[getNbManifs()]; for (int i=0; i<getNbManifs(); i++) { buttons[i]=new ImageButton(getContext()); buttons[i].setImageResource(R.drawable.wait); buttons[i].setId(i); buttons[i].setBackgroundColor(Color.GRAY); buttons[i].setPadding(0, 0, 0, 0); layout.addView(buttons[i], getPictoLayout(i)); } scrollLeft = new TranslateAnimation(0, -(pictoWidth+hspace), 0, 0); scrollLeft.setDuration(300); scrollLeft.setAnimationListener(this); scrollRight = new TranslateAnimation(0, (pictoWidth+hspace), 0, 0); scrollRight.setDuration(300); scrollRight.setAnimationListener(this); } @Override public void setOnClickListener(OnClickListener l) { for (ImageButton i : buttons) { i.setOnClickListener(l); } } public void setIndex(int idx, String imghref) { // removed : code to lazy load image from href } public void next() { wasNext = true; layout.startAnimation(scrollLeft); } public void prev() { wasNext=false; layout.startAnimation(scrollRight); } private AbsoluteLayout.LayoutParams getPictoLayout(int i) { return new AbsoluteLayout.LayoutParams(pictoWidth+3*border/2, pictoHeight+2*border, getOffsetIdx(i), 0); } private int getOffsetIdx(int idx) { return offset + idx*(pictoWidth+hspace); } @Override public void onAnimationEnd(Animation animation) { if (wasNext) { // On deplace le premier à la fin ImageButton b = buttons[0]; for (int i=1; i<buttons.length; i++) { buttons[i-1]=buttons[i]; } buttons[buttons.length-1]=b; } else { // l'inverse ImageButton b = buttons[buttons.length-1]; for (int i=buttons.length-1; i>0; i--) { buttons[i]=buttons[i-1]; } buttons[0]=b; } for (int i=0; i<buttons.length; i++) { buttons[i].setLayoutParams(getPictoLayout(i)); } } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationStart(Animation animation) { } }