且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何一次滚动一个图像到Android图库

更新时间:2022-10-22 18:01:15

改变你的类,如下,并创建延伸画廊1 customclass并添加一扔方法和更改的XML文件下面的XML。

com.gallery1

 公共类Gallerynew扩展活动实现OnItemSelectedListener {
     公共布尔能见度= TRUE;
  / **当第一次创建活动调用。 * /
  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
画廊G =(图库论坛)findViewById(R.id.gallery);
g.setAdapter(新ImageAdapter(本));
 g.setSpacing(10);
 g.setOnItemSelectedListener(本);
  }
  公共类ImageAdapter延伸BaseAdapter {
   私人语境myContext;
     私人诠释[] = 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
     };
  公共ImageAdapter(上下文C){this.myContext = C; }
   公众诠释的getCount(){
// TODO自动生成方法存根
返回this.myImageIds.length;
      } 公共对象的getItem(INT位置){
// TODO自动生成方法存根
返回的位置;
 }众长getItemId(INT位置){
// TODO自动生成方法存根
返回的位置;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
// TODO自动生成方法存根
 ImageView的I =新ImageView的(this.myContext); i.setImageResource(this.myImageIds [位置]);
 / *为宽/高设置图片应缩放。 * /
 i.setScaleType(ImageView.ScaleType.FIT_XY);
 / *设置的ImageView的宽度/高度。 * /
 i.setLayoutParams(新Gallery.LayoutParams(700,400)); 返回我;
 }/ **返回的意见尺寸(0.0至1.0F)
 *取决于偏移到中心。 * /
公众持股量的getScale(布尔专注,诠释偏移){
     / *公式:1 /(2 ^偏移)* /
   返回Math.max(0,1.0F /(浮点)Math.pow(2,Math.abs(偏移)));
 }
 }
  公共无效onItemSelected(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
  // TODO自动生成方法存根
  Toast.makeText(getBaseContext(),您已选定的图片+(ARG2 + 1)+南极的Toast.LENGTH_SHORT).show();    }
 公共无效onNothingSelected(适配器视图<>为arg0){
// TODO自动生成方法存根   }}

customclass:Gallery1.java

 公共类Gallery1扩展库{
公共Gallery1(上下文的背景下){
    超级(上下文);
    // TODO自动生成构造函数存根
}公共Gallery1(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    // TODO自动生成构造函数存根
}公共Gallery1(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    // TODO自动生成构造函数存根
   } @覆盖
公共布尔onFling(MotionEvent E1,E2 MotionEvent,浮velocityX,
                       浮动velocityY){
    返回false;
}

}
      XML文件:

 机器人:ID =@ + ID /画廊
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =WRAP_CONTENT
   机器人:重力=底

/>

hi i am doing one app here i need to display images horizantal scrollview.each image i need to display entire page.so i am using gallery i displayed images its working fine.but when i scroll or move gallery images means that time more images scrolled at a time.but i need when i scroll images that time i need to scroll one image at a time.some one suggest use onfling method. i used that method.but i am not getting result.so any one help me where i did wrong.

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

change your class as below and create one customclass which extends gallery and add fling method and change xml file to below 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 file:

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

/>