zoukankan      html  css  js  c++  java
  • android中Gallery与ImageSwicther解析

    1.Gallery

              Gallery控件一般是用于显示图像列表,因此也称为是画廊控件, Gallery只能水平显示一行,而且支持水平滑动效果。也就是说,单击、选中或者拖动Gallery中的图像, Gallery图像中的列表会根据不同的情况向左向右移动,直到显示到最后的一个图像为止。

    2.ImageSwicther

        ImageSwitcher控件可以用在不同的图像之间切换,其中切换的过程可以采用动画的方法,如淡入淡出的效果。
    ImageSwitcher需要一个图像工厂(ViewFactory)来创建用于显示图像的ImageView对象,因此我们需要一个实现android.widget.ViewSwitcher.ViewFactory接口的类。
     
      使用ImageSwitcher时,基本步骤:
       1.必须implements ViewSwitcher.ViewFactory
       2.调用setFactory方法:imageSwitcher.setFactory(this)
       3.重写函数 makeView(),做如下处理:(此方法在进入该Activity时就会调用)
     
      4.在需要显示图片的地方写如下代码
     
    以下具体列子如下:
     在main.xml文件中:
     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3    android:layout_height="fill_parent"
     4    android:layout_width="fill_parent">
     5     <ImageSwitcher 
     6         android:id="@+id/imageswitcher"
     7         android:layout_width="fill_parent"
     8         android:layout_height="fill_parent">
     9     </ImageSwitcher>
    10     <Gallery 
    11         android:id="@+id/gallery"
    12         android:layout_width="fill_parent"
    13         android:layout_height="wrap_content"
    14         android:layout_alignParentBottom="true"/>
    15 </RelativeLayout>

    在java文件中:

     1 public class MainActivity extends Activity implements ViewFactory,OnItemSelectedListener{
     2 
     3 private Gallery gallery;
     4 private ImageSwitcher is;
     5 private int galleryItemBackGroundId;
     6 private int imageId[]={R.drawable.gril1,R.drawable.gril2,R.drawable.gril3,R.drawable.gril4,R.drawable.gril5,
     7           R.drawable.gril6,R.drawable.gril7,R.drawable.gril8,R.drawable.gril9,R.drawable.gril10,
     8           R.drawable.gril11};
     9     @Override
    10     protected void onCreate(Bundle savedInstanceState) {
    11         super.onCreate(savedInstanceState);
    12         requestWindowFeature(Window.FEATURE_NO_TITLE);  //去除标题
    13         setContentView(R.layout.main);
    14         
    15         gallery = (Gallery) findViewById(R.id.gallery);
    16         gallery.setAdapter(new MyGalleryAdapter(MainActivity.this));
    17         gallery.setOnItemSelectedListener(this);  //设置选项监听用setOnItemSelectedListener
    18         
    19         is = (ImageSwitcher) findViewById(R.id.imageswitcher);
    20         is.setFactory(this);
    21    22         is.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
    23         is.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
    24         
    25     }
    26 
    27     @Override
    28     public boolean onCreateOptionsMenu(Menu menu) {
    29         // Inflate the menu; this adds items to the action bar if it is present.
    30         getMenuInflater().inflate(R.menu.main, menu);
    31         return true;
    32     }
    33 
    34     class MyGalleryAdapter extends BaseAdapter{
    35 
    36         private Context context;
    37     
    38         public MyGalleryAdapter(Context context) {
    39             this.context = context;
    40             TypedArray typed = obtainStyledAttributes(R.styleable.Gallery);   //获得属性集合
    41             galleryItemBackGroundId = typed.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0);
    42         }
    43 
    44         @Override
    45         public int getCount() {
    46             // TODO 自动生成的方法存根
    47             return Integer.MAX_VALUE;  //为了实现画廊中图片循环效果,才设置return_Integer.Max_VALUE,其实可以return imageId.length;
    48         }
    49 
    50         @Override
    51         public Object getItem(int position) {
    52             // TODO 自动生成的方法存根
    53             return imageId[position];
    54         }
    55 
    56         @Override
    57         public long getItemId(int position) {
    58             // TODO 自动生成的方法存根
    59             return position;
    60         }
    61 
    62         @Override
    63         public View getView(int position, View convertView, ViewGroup parent) {
    64             // TODO 自动生成的方法存根
    65             ImageView imageView = new ImageView(context); 
    66             imageView.setImageResource(imageId[position%imageId.length]);  设置图片到view中
    67             imageView.setScaleType(ScaleType.FIT_XY);
    68             imageView.setLayoutParams(new Gallery.LayoutParams(156, 100)); 
    69             imageView.setBackgroundResource(galleryItemBackGroundId);  //设置imageview 背景
    70             return imageView;
    71         }
    72         
    73     }
    74 
    75     @Override
    76     public View makeView() {
    77         ImageView imageView  = new ImageView(MainActivity.this);
    78         imageView.setAdjustViewBounds(true);   //是否保持宽高比。需要与maxWidth、MaxHeight一起使用,否则单独使用没有效果
    79         imageView.setScaleType(ScaleType.FIT_XY);
    80         imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    81         
    82         return imageView;
    83     }
    84 
    85     @Override
    86     public void onItemSelected(AdapterView<?> parent, View view, int position,      //画廊(Gallery)选中的项
    87             long id) {
    88         // TODO 自动生成的方法存根
    89         
    90         is.setImageDrawable(getResources().getDrawable(imageId[position%imageId.length]));
    91     }
    92 
    93     @Override
    94     public void onNothingSelected(AdapterView<?> parent) {
    95         // TODO 自动生成的方法存根
    96         
    97     }
    98 }

    运行效果:

     
  • 相关阅读:
    GhostBSD 3.0RC3,基于GNOME的FreeBSD
    Nagios 3.4.3 发布,企业级监控系统
    Jolokia 1.0.6 发布, JMX远程访问方法
    微软希望开发人员不要使 WebKit 成为新版 IE6
    Kwort Linux 3.5 正式版发布
    EJDB 1.0.24 发布,嵌入式 JSON 数据库引擎
    Pale Moon 15.3 Firefox“苍月”优化版发布
    Galera Load Balancer 0.8.1 发布
    SmartSVN V7.5 正式发布
    PostgresQL建立索引如何避免写数据锁定
  • 原文地址:https://www.cnblogs.com/SoulCode/p/5397392.html
Copyright © 2011-2022 走看看