如何在android中一次滚动一张图片到图库



嗨,我在这里做一个应用程序,我需要水平滚动显示图像。每个图像我都需要显示整个页面。所以我使用图库,我显示图像的工作效果很好。但当我滚动或移动图库时,意味着一次滚动更多的图像。但我需要在滚动图像时,一次滚动一个图像。有人建议使用onfling方法。我用了那个方法。但我没有得到结果。所以没有人帮我哪里做错了。

Gallerynew.class:
   public class Gallerynew extends Activity implements OnItemSelectedListener{
         public boolean Visibility=true;
      /** Called when the activity is first created. */
      @Override
  public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
  Gallery g=(Gallery)findViewById(R.id.gallery);
  g.setAdapter(new ImageAdapter(this));
  g.setSpacing(10);
     g.setOnItemSelectedListener(this);
  } 
      public class ImageAdapter extends BaseAdapter {
       private Context myContext;
         private int[] myImageIds = {
        R.drawable.bokstavslottet01,
        R.drawable.bokstavslottet02,
        R.drawable.bokstavslottet03,
        R.drawable.bokstavslottet04,
        R.drawable.bokstavslottet05,
        R.drawable.bokstavslottet06,
        R.drawable.bokstavslottet07,
        R.drawable.bokstavslottet08,
        R.drawable.bokstavslottet09,
        R.drawable.bokstavslottet10,
        R.drawable.bokstavslottet11,
        R.drawable.bokstavslottet12,
        R.drawable.bokstavslottet13
         };
      public ImageAdapter(Context c) { this.myContext = c; }
       public int getCount() {
    // TODO Auto-generated method stub
    return this.myImageIds.length;
}
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
     ImageView i = new ImageView(this.myContext);
     i.setImageResource(this.myImageIds[position]);
     /* Image should be scaled as width/height are set. */
     i.setScaleType(ImageView.ScaleType.FIT_XY);
     /* Set the Width/Height of the ImageView. */
     i.setLayoutParams(new Gallery.LayoutParams(700, 400));
     return i;
 }
 /** Returns the size (0.0f to 1.0f) of the views
  * depending on the 'offset' to the center. */
 public float getScale(boolean focused, int offset) {
         /* Formula: 1 / (2 ^ offset) */
     return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
 }
}
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + " of Antartica",             Toast.LENGTH_SHORT).show();
    }
     public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
   }
          private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2){
           return e2.getX() > e1.getX();
 }
    @Override
      public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
     int kEvent;
       if(isScrollingLeft(e1, e2)){ //Check if scrolling left
kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
       }
      else{ //Otherwise scrolling right
       kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;
     }
      onKeyDown(kEvent, null);
        return true;  
     }
 }

如下更改您的类并创建一个自定义类,该自定义类扩展了gallery并添加了fling方法,并将xml文件更改为下面的xml。

com.gallery1

public class Gallerynew extends Activity implements OnItemSelectedListener{
     public boolean Visibility=true;
  /** Called when the activity is first created. */
  @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
 g.setSpacing(10);
 g.setOnItemSelectedListener(this);
  } 
  public class ImageAdapter extends BaseAdapter {
   private Context myContext;
     private int[] myImageIds = {
    R.drawable.bokstavslottet01,
    R.drawable.bokstavslottet02,
    R.drawable.bokstavslottet03,
    R.drawable.bokstavslottet04,
    R.drawable.bokstavslottet05,
    R.drawable.bokstavslottet06,
    R.drawable.bokstavslottet07,
    R.drawable.bokstavslottet08,
    R.drawable.bokstavslottet09,
    R.drawable.bokstavslottet10,
    R.drawable.bokstavslottet11,
    R.drawable.bokstavslottet12,
    R.drawable.bokstavslottet13
     };
  public ImageAdapter(Context c) { this.myContext = c; }
   public int getCount() {
// TODO Auto-generated method stub
return this.myImageIds.length;
      }
 public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
 }
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
 ImageView i = new ImageView(this.myContext);
 i.setImageResource(this.myImageIds[position]);
 /* Image should be scaled as width/height are set. */
 i.setScaleType(ImageView.ScaleType.FIT_XY);
 /* Set the Width/Height of the ImageView. */
 i.setLayoutParams(new Gallery.LayoutParams(700, 400));
 return i;
 }
/** Returns the size (0.0f to 1.0f) of the views
 * depending on the 'offset' to the center. */
public float getScale(boolean focused, int offset) {
     /* Formula: 1 / (2 ^ offset) */
   return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
 }
 }
  public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
  // TODO Auto-generated method stub
  Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + " of  Antartica",             Toast.LENGTH_SHORT).show();
    }
 public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
   }
}

customclass:Gallery1.java

   public class Gallery1 extends Gallery {
public Gallery1(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}
public Gallery1(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}
public Gallery1(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
   }
 @Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                       float velocityY) {        
    return false;
}

}Xml文件:

android:id="@+id/gallery"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:gravity="bottom"

/>

我建议使用ViewPager。它正是你想要的,而且它在兼容包中,所以如果你针对早期的Android版本,你可以使用它。

最新更新