Android:ImageSwitcher

时间:2011-05-10 13:30:20

标签: android

我有一个ImageSwitcher和2个按钮......“下一个”和“上一个”来滑动图像...但它的动画我只能从左到右一侧滑动......如何解决这个问题?感谢...

Integer[] imageIDs = { R.drawable.image_one, R.drawable.image_two,
        R.drawable.image_tree };

private ImageSwitcher imageSwitcher;
private Button nextButton;
private Button previousButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Animation in = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_in_left);
    final Animation out = AnimationUtils.loadAnimation(this,
            android.R.anim.slide_out_right);

    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
    imageSwitcher.setFactory(this);
    imageSwitcher.setInAnimation(in);
    imageSwitcher.setOutAnimation(out);
    imageSwitcher.setImageResource(imageIDs[0]);

    nextButton = (Button) findViewById(R.id.next);
    nextButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            imageSwitcher.setImageResource(imageIDs[1]);

        }
    });

    previousButton = (Button) findViewById(R.id.previous);
    previousButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            imageSwitcher.setImageResource(imageIDs[0]);
        }
    });

}

public View makeView() {
    ImageView imageView = new ImageView(this);
    imageView.setBackgroundColor(0xFF000000);
    imageView.setScaleType(ImageView.ScaleType.CENTER);
    imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    return imageView;
}

1 个答案:

答案 0 :(得分:2)

更改onClickListeners中的输入/输出动画。 试试这个:(没有检查语法等...)

@Override public void onCreate(Bundle savedInstanceState) {    
 super.onCreate(savedInstanceState);    
 setContentView(R.layout.main);    



 imageSwitcher.setImageResource(imageIDs[0]);     
 nextButton = (Button) findViewById(R.id.next);    
 nextButton.setOnClickListener(new OnClickListener() {       
   public void onClick(View v) {  
 Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);     
 Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);   
 imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);    
 imageSwitcher.setFactory(this); 
 imageSwitcher.setInAnimation(in);    
 imageSwitcher.setOutAnimation(out);        
    imageSwitcher.setImageResource(imageIDs[1]);     
     }    
 });    
  previousButton = (Button) findViewById(R.id.previous);     previousButton.setOnClickListener(new OnClickListener() { 
         public void onClick(View v) {  
 Animation out= AnimationUtils.loadAnimation(this, android.R.anim.slide_out_left);     
 Animation in= AnimationUtils.loadAnimation(this, android.R.anim.slide_in_right);   
 imageSwitcher.setFactory(this); 
 imageSwitcher.setInAnimation(in);    
 imageSwitcher.setOutAnimation(out);  
           imageSwitcher.setImageResource(imageIDs[0]);     
    }   
  }); 
 } 

 public View makeView() {   
  ImageView imageView = new ImageView(this);   
  imageView.setBackgroundColor(0xFF000000);   
  imageView.setScaleType(ImageView.ScaleType.CENTER);  
   imageView.setLayoutParams(new ImageSwitcher.LayoutParams(             LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    return imageView; }