如何进行图库图像默认情况下从左向右交换

时间:2014-02-07 09:26:42

标签: android gallery image-gallery

我有图片库。一开始,我必须从右向左交换以查看更多图像。我希望图像库默认从左到右开始淹没

public View getView(int index, View view, ViewGroup viewGroup)
{
    ImageView img = new ImageView(mContext);
    ShowImgsActivity.pagNumber.setText(index+"");
    img.setImageResource(ShowImgsActivity.typedImgs.getResourceId((ShowImgsActivity.typedImgs.length()-1)-index, -1));
    img.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    img.setScaleType(ImageView.ScaleType.FIT_XY);

1 个答案:

答案 0 :(得分:2)

您可以将ViewPager用作图库。它可让您左右滑动以查看普及图和下一张图像。

这是Java类,布局文件和适配器。

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImageAdapter adapter = new ImageAdapter(this);
viewPager.setAdapter(adapter);
}

}

布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

适配器:

public class ImageAdapter extends PagerAdapter {
Context context;
private int[] GalImages = new int[] {
R.drawable.one,
R.drawable.two,
R.drawable.three
};
ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
return GalImages.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}

您可以询问是否有任何进一步的查询。查看鳍状肢是另一种选择。

快乐编码:)

参考:here