当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
Android图片浏览源码解读
发布时间:2010/11/10 11:48:10 来源:城市学习网 编辑:ziteng
  Android手机操作系统的应用方式灵活,简单,深受广大编程爱好者的喜爱。尤其是它的开源代码,使得我们能够方便的得到自己想要的功能需求。今天我们就为大家带来了有关Android图片浏览的相关方法。
  首先是Android图片浏览中layout xml:
  1.< ?xml version="1.0" encoding="utf-8"?>
  2.< RelativeLayout xmlns:android="http://schemas.Android.com/apk/res/Android"
  3.Android:layout_width="fill_parent"
  4.Android:layout_height="fill_parent">
  5.< ImageSwitcher Android:id="@+id/switcher"
  6.Android:layout_width="fill_parent"
  7.Android:layout_height="fill_parent"
  8.Android:layout_alignParentTop="true"
  9.Android:layout_alignParentLeft="true"
  10./>
  11.< Gallery Android:id="@+id/gallery"
  12.Android:background="#55000000"
  13.Android:layout_width="fill_parent"
  14.Android:layout_height="60dp"
  15.Android:layout_alignParentBottom="true"
  16.Android:layout_alignParentLeft="true"
  17.Android:gravity="center_vertical"
  18.Android:spacing="16dp"
  19./>
  20.< /RelativeLayout>
  layout里面用到了前面所说的两个控件,ImageSwitcher用啦显示全图,Gallery用来显示缩略图。着重看看ImageSwitcher,在ImageSwitcher1中需要实现ViewSwitcher.ViewFactory这个接口,这个接口里有个方法makeView,这样就产生了用来显示图片的view. ImageSwitcher调用过程是这样的,首先要有一个Factory为它提供一个View,然后ImageSwitcher就可以初始化各种资源了。注意在使用一个ImageSwitcher之前,一定要调用setFactory方法,要不setImageResource这个方法会报空指针异常。
  下面是Android图片浏览代码:
  1.package com.zx.imageswitcher;
  2.import Android.app.Activity;
  3.import Android.content.Context;
  4.import Android.os.Bundle;
  5.import Android.view.View;
  6.import Android.view.ViewGroup;
  7.import Android.view.animation.AnimationUtils;
  8.import Android.widget.AdapterView;
  9.import Android.widget.BaseAdapter;
  10.import Android.widget.Gallery;
  11.import Android.widget.ImageSwitcher;
  12.import Android.widget.ImageView;
  13.import Android.widget.ViewSwitcher;
  14.import Android.widget.Gallery.LayoutParams;
  15.public class ImageSwitcherTest extends Activity implements
  16.AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory{
  17.private ImageSwitcher mSwitcher;
  18.private Integer[] mThumbIds = {
  19.R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
  20.R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
  21.R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
  22.R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
  23.private Integer[] mImageIds = {
  24.R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
  25.R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
  26.R.drawable.sample_6, R.drawable.sample_7};
  27./** Called when the activity is first created. */
  28.@Override
  29.public void onCreate(Bundle savedInstanceState) {
  30.super.onCreate(savedInstanceState);
  31.setContentView(R.layout.main);
  32.mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
  33.mSwitcher.setFactory(this);
  34.mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
  35.Android.R.anim.fade_in));
  36.mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  37.Android.R.anim.fade_out));
  38.Gallery g = (Gallery) findViewById(R.id.gallery);
  39.g.setAdapter(new ImageAdapter(this));
  40.g.setOnItemSelectedListener(this);
  41.}
  42./* [NextPage]   43.* override for ViewSwitcher.ViewFactory#makeView()
  44.*/
  45.public View makeView() {
  46.ImageView i = new ImageView(this);
  47.i.setBackgroundColor(0xFF000000);
  48.i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  49.i.setLayoutParams(new ImageSwitcher.LayoutParams
  (LayoutParams.FILL_PARENT,
  50.LayoutParams.FILL_PARENT));
  51.return i;
  52.}
  53./*
  54.* override for
  55.* AdapterView.OnItemSelectedListener#onItemSelected()
  56.*/
  57.public void onItemSelected(AdapterView parent,
  View v, int position, long id) {
  58.mSwitcher.setImageResource(mImageIds[position]);
  59.}
  60./*
  61.* override for AdapterView.OnItemSelectedListener
  #onNothingSelected()
  62.*/
  63.public void onNothingSelected(AdapterView< ?> arg0) {
  64.// TODO Auto-generated method stub
  65.}
  66.public class ImageAdapter extends BaseAdapter {
  67.public ImageAdapter(Context c) {
  68.mContext = c;
  69.}
  70.public int getCount() {
  71.return mThumbIds.length;
  72.}
  73.public Object getItem(int position) {
  74.return position;
  75.}
  76.public long getItemId(int position) {
  77.return position;
  78.}
  79.public View getView(int position, View convertView,
  ViewGroup parent) {
  80.ImageView i = new ImageView(mContext);
  81.i.setImageResource(mThumbIds[position]);
  82.i.setAdjustViewBounds(true);
  83.i.setLayoutParams(new Gallery.LayoutParams(
  84.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  85.i.setBackgroundResource(R.drawable.picture_frame);
  86.return i;
  87.}
  88.private Context mContext;
  89.}
  90.}
  从Android图片浏览的代码中看到还实现了AdapterView.OnItemSelectedListener,这样就需要重写onItemSelected()方法,然后在该方法中:mSwitcher.setImageResource(mImageIds[position]);这样就实现了图片在ImageSwitcher中的切换。
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved