【Android】使用Gallery组件实现图片播放预览

来源:互联网 发布:光纤分布数据接口fddi 编辑:程序博客网 时间:2024/05/21 08:58

Gallery(画廊)扩展了LayoutParams,以此提供可以容纳当前的转换信息和先前的位置转换信息的场所。

Activity

[java] view plaincopy
  1. package com.app.test01;  
  2.   
  3.   
  4.   
  5.   
  6.   
  7. import com.app.adapter.ImageAdapter;  
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.widget.AdapterView;  
  13. import android.widget.Gallery;  
  14. import android.widget.ImageView;  
  15.   
  16. public class GalleryTest extends Activity{  
  17.     Gallery gallery;  
  18.     ImageView imagedemo;  
  19.     ImageAdapter iAdapter = new ImageAdapter(this);  
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         // TODO Auto-generated method stub  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.activity_gallery);  
  25.           
  26.         gallery = (Gallery) findViewById(R.id.gallery1);  
  27.         imagedemo = (ImageView) findViewById(R.id.imagedemo);  
  28.           
  29.         gallery.setAdapter(iAdapter);  
  30.         gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  31.             @Override  
  32.             public void onItemClick(AdapterView<?> parent, View view,  
  33.                     int position, long id) {  
  34.                 // TODO Auto-generated method stub  
  35.                 Integer[] iViews = iAdapter.getmImageIds();  
  36.                 imagedemo.setImageDrawable(getResources().getDrawable(iViews[position]));  
  37.             }  
  38.         });  
  39.     }  
  40. }  

BaseAdapter适配器

[java] view plaincopy
  1. package com.app.adapter;  
  2.   
  3. import com.app.test01.R;  
  4.   
  5. import android.content.Context;  
  6. import android.content.res.TypedArray;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.Gallery;  
  11. import android.widget.ImageView;  
  12.   
  13.   
  14. public class ImageAdapter extends BaseAdapter {  
  15.     private Context mContext;  
  16.     public ImageAdapter(Context c) {  
  17.         mContext = c;  
  18.     }  
  19.   
  20.     public int getCount() {  
  21.         return mImageIds.length;  
  22.     }  
  23.   
  24.     public Object getItem(int position) {  
  25.         return position;  
  26.     }  
  27.   
  28.     public long getItemId(int position) {  
  29.         return position;  
  30.     }  
  31.   
  32.     public View getView(int position, View convertView, ViewGroup parent) {  
  33.         ImageView i = new ImageView(mContext);  
  34.         i.setImageResource(mImageIds[position]);  
  35.         i.setLayoutParams(new Gallery.LayoutParams(150,75));  
  36.         return i;  
  37.     }  
  38.   
  39.     public Integer[] getmImageIds() {  
  40.         return mImageIds;  
  41.     }  
  42.   
  43.     public void setmImageIds(Integer[] mImageIds) {  
  44.         this.mImageIds = mImageIds;  
  45.     }  
  46.   
  47.     private Integer[] mImageIds = {  
  48.             R.drawable.image01,  
  49.             R.drawable.image02,  
  50.             R.drawable.image03,  
  51.             R.drawable.image04,  
  52.             R.drawable.image05,  
  53.             R.drawable.image06,  
  54.             R.drawable.image07,  
  55.             R.drawable.image08,  
  56.             R.drawable.image09,  
  57.             R.drawable.image01,  
  58.             R.drawable.image02,  
  59.             R.drawable.image03,  
  60.             R.drawable.image04,  
  61.             R.drawable.image05,  
  62.             R.drawable.image06,  
  63.             R.drawable.image07,  
  64.             R.drawable.image08,  
  65.             R.drawable.image09,  
  66.     };  
  67. }  

XML布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Gallery  
  8.         android:id="@+id/gallery1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_marginTop="50dp" />  
  12.   
  13.     <RelativeLayout  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="match_parent"  
  16.         android:gravity="center">  
  17.           
  18.         <ImageView  
  19.             android:id="@+id/imagedemo"  
  20.             android:layout_width="wrap_content"  
  21.             android:layout_height="wrap_content"  
  22.             android:src="@drawable/image01" />  
  23.     </RelativeLayout>  
  24.   
  25. </LinearLayout>  

效果图



 
 
地址:http://download.csdn.net/category

版权声明:本文为博主原创文章,未经博主允许不得转载。

0 0