【Android基礎知識】使用Gallery和ImageSwitcher實現圖片輪播效果
阿新 • • 發佈:2019-01-25
使用Gallery和ImageSwitcher實現滑動Gallery,切換ImageSwitcher的圖片。
佈局檔案
ImageAdapter.java<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context="com.example.gallerydemo.MainActivity" > <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ImageSwitcher android:id="@+id/image_switcher" android:layout_marginTop="60dp" android:layout_width="match_parent" android:layout_height="wrap_content"> </ImageSwitcher> </LinearLayout>
MainActivity.javapublic class ImageAdapter extends BaseAdapter{ private int[] res; private Context context; public ImageAdapter(int []res,Context context){ this.res = res; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return res.length; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return res[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 image = new ImageView(context); image.setBackgroundResource(res[position%res.length]); image.setLayoutParams(new Gallery.LayoutParams(200,150)); image.setScaleType(ScaleType.FIT_XY); return image; } }
public class MainActivity extends Activity implements OnItemSelectedListener,ViewFactory{ private int[] res = {R.drawable.item1,R.drawable.item2,R.drawable.item3, R.drawable.item4,R.drawable.item5,R.drawable.item6, R.drawable.item7,R.drawable.item8,R.drawable.item9, R.drawable.item10,R.drawable.item11,R.drawable.item12}; private Gallery gallery; private ImageAdapter adapter; private ImageSwitcher is; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); gallery = (Gallery)findViewById(R.id.gallery); is = (ImageSwitcher)findViewById(R.id.image_switcher); adapter = new ImageAdapter(res, this); gallery.setAdapter(adapter); gallery.setOnItemSelectedListener(this); is.setFactory(this); is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // 使用ImageSwitcher is.setBackgroundResource(res[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } @Override public View makeView() { // TODO Auto-generated method stub ImageView image = new ImageView(this); image.setScaleType(ScaleType.FIT_CENTER); return image; } }