成都创新互联网站制作重庆分公司

Gallery和ImageSwitcher怎么在Android中使用

Gallery和ImageSwitcher怎么在Android中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联是一家集网站建设,扶风企业网站建设,扶风品牌网站建设,网站定制,扶风网站建设报价,网络营销,网络优化,扶风网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

布局文件activity_main.xml如下:


  
  
  

MainActivity.java代码如下:

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class MainActivity extends Activity {
  private int imageId[] = new int[] { R.drawable.a, R.drawable.b,
      R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f,
      R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j,
      R.drawable.k };
  private ImageSwitcher imageSwitcher;
  private Gallery gallery;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    imageSwitcher = (ImageSwitcher) this.findViewById(R.id.imageSwitcher);
    gallery = (Gallery) this.findViewById(R.id.gallery1);
    // 设置动画效果
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in));
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out));
    imageSwitcher.setFactory(new ViewFactory() {
      @Override
      public View makeView() {
        ImageView imageView = new ImageView(MainActivity.this);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 设置保持纵横比居中
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        return imageView;
      }
    });
    GalleryAdapter adapter = new GalleryAdapter(MainActivity.this,imageId);
    gallery.setAdapter(adapter);
    gallery.setSelection(imageId.length / 2);
    gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
      @Override
      public void onItemSelected(AdapterView parent, View view,int position, long id) {
        imageSwitcher.setImageResource(imageId[position]);
      }
      @Override
      public void onNothingSelected(AdapterView arg0) {
      }
    });
  }
}

其中需要的一个适配器:

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
public class GalleryAdapter extends BaseAdapter {
  private int[] imageId;
  private Context mContext;
  /**
   * 穿入上下文和图片资源数组
   * @param mContext
   * @param imageId
   */
  public GalleryAdapter(Context mContext, int[] imageId) {
    this.mContext = mContext;
    this.imageId = imageId;
  }
  @Override
  public int getCount() {
    return imageId.length;
  }
  @Override
  public Object getItem(int position) {
    return imageId[position];
  }
  @Override
  public long getItemId(int position) {
    return position;
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView1;
    if (convertView == null) {
      imageView1 = new ImageView(mContext);
      imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
      imageView1.setLayoutParams(new Gallery.LayoutParams(180, 135));
      TypedArray typedArray = mContext
          .obtainStyledAttributes(R.styleable.Gallery);
      imageView1.setBackgroundResource(typedArray.getResourceId(
          R.styleable.Gallery_android_galleryItemBackground, 0));
      imageView1.setPadding(5, 0, 5, 0); // 设置ImageView的内边距
    } else {
      imageView1 = (ImageView) convertView;
    }
    imageView1.setImageResource(imageId[position]); // 为ImageView设置要显示的图片
    return imageView1; // 返回ImageView
  }
}

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网页题目:Gallery和ImageSwitcher怎么在Android中使用
当前路径:http://cxhlcq.com/article/gpjspp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部