1

我在一個數組中存儲了兩個圖像,圖像出現了,但是我試圖實現的是當用戶向右滑動屏幕時出現第二個圖像,並且當向左滑動時第一張圖片。我很新的android應用程序開發一些幫助將不勝感激。目前發生的所有事情都是當用戶點擊圖像時,它會轉到第二個圖像,並且就是這樣。左右滑動圖像android

我也加入了我的代碼:

public class ScrollingQuran extends Activity { 

private ImageView imgView; 


private Integer[] Imgid = { 

     R.drawable.page1, R.drawable.page2 

}; 



@Override 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.qurangallery); 


    imgView = (ImageView)findViewById(R.id.ImageView01); 

    imgView.setImageResource(Imgid[0]); 

    imgView.setOnTouchListener(new OnTouchListener() 
    { 

     public boolean onTouch(View v, MotionEvent event) 
     { 
     ImageView iv = (ImageView) v; 
     if (event.getAction() == MotionEvent.AXIS_VSCROLL) { 

      iv.setImageResource(R.drawable.page2); 
      return true; 
     } else if (event.getAction() == MotionEvent.AXIS_HSCROLL) { 
      iv.setImageResource(R.drawable.page2); 
      return true; 
     } 

     return false; 
    } 

});

回答

0

我覺得這個代碼將有助於you.In這transimg1是imageswitcher

in_right = AnimationFactory.inFromRight(); 
in_left = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); 
out_right = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);  
out_left = AnimationFactory.outToLeft(); 
gestureDetector1 = new GestureDetector(new MyGestureDetector1()); 
gestureListener1 = new View.OnTouchListener() { 
    public boolean onTouch(View v, MotionEvent event) { 
      return gestureDetector1.onTouchEvent(event); 
    } 
}; 

transimg1.setOnTouchListener(gestureListener1); 
class MyImageSwitcherFactory implements ViewFactory 
{ 
    public View makeView() 
    { 

     ImageView imageView = new ImageView(getApplicationContext()); 
     imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     return imageView; 

    } 
} 

public static class AnimationFactory 
{ 
     public static Animation inFromRight() 
     { 
      Animation inFromLeft = 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 
      ); 
      inFromLeft.setDuration(250); 
      inFromLeft.setInterpolator(new AccelerateInterpolator()); 
      return inFromLeft; 
     } 
     public static Animation outToLeft() 
     { 
      Animation inFromLeft = new TranslateAnimation 
      (
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
      ); 
      inFromLeft.setDuration(250); 
      inFromLeft.setInterpolator(new AccelerateInterpolator()); 
      return inFromLeft; 
     } 
}class MyGestureDetector1 extends SimpleOnGestureListener { 
    @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
     { 
      try 
      { 

       if((e1.getX()-e2.getX()>SWIPE_MIN_DISTANCE)) 
       { 
        gif.setVisibility(View.INVISIBLE); 
       } 
       else if(e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) 
       { 
        gif.setVisibility(View.VISIBLE); 
        gif.start(); 
       } 
      } 
      catch(Exception e) 
      { 
       Toast.makeText(getApplicationContext(), e.toString() + " MyGestureDetector2 count=" + exccount , Toast.LENGTH_SHORT).show(); 
      } 
      return false; 
     } 

    @Override 
    public boolean onDown(MotionEvent arg0) 
    { 
     return true; 
    } 
} 

這裏是Dustin Graham一個博客有更多的例子:http://developingandroid.blogspot.in/2009/09/implementing-swipe-gesture.html