Android中Gallery來顯示圖片

來源:互聯網
上載者:User

public class MainActivity extends Activity {private Gallery gallery;private LayoutInflater inflater;private ImageSwitcher imageSwitcher;private int res[] = new int[] { R.drawable.ic_launcher, R.drawable.an01,R.drawable.an02, R.drawable.an03, R.drawable.an01, R.drawable.an02,R.drawable.an03 };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);/** * 系統中有沒有合適的適配器 */gallery = (Gallery) findViewById(R.id.gallery1);// 擷取ImageView控制項imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);// 初始化一次工廠即可imageSwitcher.setFactory(new ImageSwitcher.ViewFactory() {// 設定圖片@Overridepublic View makeView() {ImageView imageView = new ImageView(MainActivity.this);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);return imageView;}});// 加入動畫imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.fade_in));imageSwitcher.setOutAnimation(MainActivity.this,android.R.anim.fade_out);List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();for (int i = 0; i < res.length; i++) {Map<String, Object> map = new HashMap<String, Object>();map.put("imageView", res[i]);data.add(map);}SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,R.layout.activity_cell, new String[] { "imageView" },new int[] { R.id.imageView1 });gallery.setAdapter(simpleAdapter);// 註冊事件gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {// 擷取圖片資源int imageResId = res[position % res.length];// 設定imageSwithcer組件資源imageSwitcher.setImageResource(imageResId);}});}/*public class MyAdapter extends BaseAdapter {@Overridepublic int getCount() {// TODO Auto-generated method stubreturn Integer.MAX_VALUE;}@Overridepublic Object getItem(int position) {return res[position % res.length];}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View v = inflater.inflate(R.layout.activity_gallery, null);ImageView iv = (ImageView) v.findViewById(R.id.imageView1);int imageResId = res[position % res.length];System.out.println(position + "-------" + imageResId);iv.setImageResource(imageResId);return v;}}*/}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <Gallery        android:id="@+id/gallery1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="0dp" />    <ImageSwitcher        android:id="@+id/imageSwitcher1"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1" >    </ImageSwitcher></LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView        android:id="@+id/imageView1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/an01" /></LinearLayout>

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.