2.4.12 画廊视图
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageSwitcher android:id="@+id/imageSwitcher1" android:layout_width="320dp" android:layout_height="320dp" > </ImageSwitcher> <Gallery android:id="@+id/gallery1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:unselectedAlpha="0.6" android:spacing="3pt"/> </LinearLayout>
package com.moon.hello2; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import android.R.anim; import android.R.integer; import android.app.Activity; import android.content.res.TypedArray; import android.database.DataSetObserver; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.animation.AnimationUtils; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.DatePicker; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.Gallery; import android.widget.GridView; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.RatingBar; import android.widget.SimpleAdapter; import android.widget.RatingBar.OnRatingBarChangeListener; import android.widget.SeekBar; import android.widget.DatePicker.OnDateChangedListener; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TabHost; import android.widget.TextView; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; import android.widget.ViewSwitcher.ViewFactory; public class MainActivity extends Activity { private final int[] imgs = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7, R.drawable.a8 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); final ImageSwitcher imageSwitcher = (ImageSwitcher)findViewById(R.id.imageSwitcher1); imageSwitcher.setFactory(new ViewFactory() { @Override public View makeView() { // TODO Auto-generated method stub ImageView imageView = new ImageView(MainActivity.this); imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); imageView.setBackgroundColor(0xff0000); imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); return imageView; } }); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); Gallery gallery = (Gallery)findViewById(R.id.gallery1); BaseAdapter baseAdapter = new BaseAdapter() { @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ImageView imageView = new ImageView(MainActivity.this); imageView.setImageResource(MainActivity.this.imgs[position%imgs.length]); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setLayoutParams(new Gallery.LayoutParams(75, 100)); return imageView; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public int getCount() { // TODO Auto-generated method stub return MainActivity.this.imgs.length; } }; gallery.setAdapter(baseAdapter); gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0, View view, int position, long arg3) { // TODO Auto-generated method stub imageSwitcher.setImageResource(imgs[position%imgs.length]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
本篇文章首发自魔芋红茶的博客https://www.cnblogs.com/Moon-Face/
请尊重其他人的劳动成功,转载请注明。