王立平--Gallery:实现图片的左右滑动
Posted cynchanpin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了王立平--Gallery:实现图片的左右滑动相关的知识,希望对你有一定的参考价值。
<span style="font-size:18px;color:#330033;">package com.main; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; @SuppressWarnings("deprecation") public class MainActivity extends Activity { Gallery gallery; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery = (Gallery)findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.pointToPosition(0, 0); } class ImageAdapter extends BaseAdapter{ private Context context; //图片源数组 private Integer[] imageInteger={ R.drawable.ic_launcher, R.drawable.bg3, R.drawable.ic_launcher, R.drawable.bg3, R.drawable.ic_launcher, R.drawable.bg3, R.drawable.ic_launcher, R.drawable.bg3 }; public ImageAdapter(Context c){ context = c; } @Override public int getCount() { return imageInteger.length; } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(imageInteger[position]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); return imageView; }} }</span>-----------------------------------------------------------------------------------------
<span style="font-size:18px;color:#330033;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:gravity="fill_vertical" android:spacing="10dp" android:background="#0000FF" android:padding="0dp" /> </RelativeLayout> </span>
以上是关于王立平--Gallery:实现图片的左右滑动的主要内容,如果未能解决你的问题,请参考以下文章
android:使用gallery和imageSwitch制作可左右循环滑动的图片浏览器
Android新手入门2016(15)--Gallery画廊