AndroidGUI21:ImageSwitcher常用技巧

ImageSwitcher ViewGroup 的派生类, ViewGroup View 的派生类。

 

ImageSwitcher 很简单见,但在使用它的时候,有一个地方必须要注意,否则就会出现 NullPointerException 。具体情况,请见下面的例子。

 

在这个例子中,我们将大致要重复做一遍 AndroidGUI19 Gallery 常用技巧中的提到的事情。也就是说,这次我们还要用到 Gallery ,不过这次不再用 ImageView 来显示大图了,而是改用 ImageSwitcher 来代替它。

 

1.     创建一个 Android Project ,修改 main.xml 使之如下:

<? xml version = "1.0" encoding = "utf-8" ?>

< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"

    android:orientation = "vertical"

    android:layout_width = "fill_parent"

    android:layout_height = "fill_parent"

    >

   

    < ImageSwitcher

    android:id = "@+id/imageswitcher"

    android:layout_width = "match_parent"

    android:layout_height = "wrap_content"

    android:layout_alignParentTop = "true"

    />

 

         < Gallery

                   android:id = "@+id/gallery"

                   android:layout_width = "fill_parent"

                   android:layout_height = "wrap_content"

                   android:gravity = "center_vertical"

                   android:spacing = "10px"

                   android:layout_marginTop = "10px"

                   android:layout_alignParentBottom = "true"

         />

         <!--

                   android:layout_alignParentBottom="true"

                   就是让 Gallery 位于屏幕底端, LinearLayout 不存在这个属性

         -->

        

</ RelativeLayout >

 

2.     把项目需要用到的图片文件拖入 res/drawable-mdpi 文件夹下,如下图所示

AndroidGUI21:ImageSwitcher常用技巧_第1张图片

 

3.     实现一个自定义的 Adapter: ImageAdapter ,代码如下:

package com.pat.gui;

 

import android.content.Context;

import android.graphics.Color;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageView;

 

public class ImageAdapter extends BaseAdapter

{

         private Context ctx;

        

         private int images[] =

         {

                   R.drawable.lrt01,

                   R.drawable.lrt02,

                   R.drawable.lrt03,

                   R.drawable.lrt04,

                   R.drawable.lrt05,

                   R.drawable.lrt06,

                   R.drawable.lrt07,

                   R.drawable.lrt08,

                   R.drawable.lrt09

         };

        

         public ImageAdapter(Context ctx)

         {

                   this.ctx = ctx;

         }

        

         //@Override

         public int getCount()

         {

                   return images.length;

         }

 

         //@Override

         public Object getItem(int position)

         {

                   return images[position];

         }

 

         //@Override

         public long getItemId(int position)

         {

                   return images[position];

         }

 

         //@Override

         public View getView(int position, View convertView, ViewGroup parent)

         {

                   ImageView view;

                   view = (ImageView)convertView;

                   if(view == null)

                   {

                            view = new ImageView(ctx);

                   }

                  

                   view.setImageResource(images[position]);

                   view.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

                   view.setLayoutParams(new Gallery.LayoutParams(60, 60));

                   view.setBackgroundColor(Color.LTGRAY);

                  

                   return view;

         }

 

}

 

4.     实现 Activity 所对应的代码,使之如下 ( 注意其中的粗体字部分 )

package com.pat.gui;

 

import android.app.Activity;

import android.os.Bundle;

import android.util.DisplayMetrics;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

import android.widget.AdapterView;

import android.widget.Gallery;

import android.widget.ImageSwitcher;

import android.widget.ImageView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.FrameLayout.LayoutParams;

import android.widget.ViewSwitcher.ViewFactory;

 

public class ControlImageSwitcher extends Activity

implements

OnItemSelectedListener, ViewFactory

{

         private Gallery gallery;

         private ImageSwitcher imageswitcher;

         private ImageAdapter adapter;

         private DisplayMetrics dm;

        

    //@Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题

        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;  // 全屏

        this.getWindow().setFlags(flag, flag);

       

        setContentView(R.layout.main);

       

        // 得到屏幕尺寸

        dm = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(dm);

       

        gallery = (Gallery)this.findViewById(R.id.gallery);

        imageswitcher = (ImageSwitcher)this.findViewById(R.id.imageswitcher);

       

        // 下面这句必须要,不然会使 setImageSource 这个方法指向空指针

        imageswitcher.setFactory(this);

       

        adapter = new ImageAdapter(this);

       

        gallery.setAdapter(adapter);

 

        gallery.setOnItemSelectedListener(this);

    }

 

         //@Override

         public void onItemSelected(AdapterView<?> parent, View view, int position, long id)

         {

                   int imageID = (int) adapter.getItemId(position);

                   imageswitcher.setImageResource(imageID);

         }

 

         //@Override

         public void onNothingSelected(AdapterView<?> parent)

         {

                   imageswitcher.setImageResource((int) adapter.getItemId(0));

         }

 

         //@Override

         // 接口 ViewFactory 中规定必须实现的方法,返回的 View 就是 ImageSwitcher 用于显示图片的 vie w

         public View makeView()

         {

                   ImageView iv = new ImageView(this);

                   // 设置背景颜色

                   iv.setBackgroundColor(0xFF646464);

                   iv.setScaleType(ImageView.ScaleType.FIT_CENTER);

                   iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, dm.heightPixels - 70));

                   // 上面语句中 -70 的目的是应为, ImageSwitcher 下面要显示 Gallery ,我们在 ImageAdapter 中设定了

                   // view.setLayoutParams(new Gallery.LayoutParams(60, 60));

                   // 即高度是 60pixels ,在加在 main.xml Gallery 有下面这样的属性:

                   // android:layout_marginTop="10px"

                   // 因此加起来一共刚好是 70px

                  

                   return iv;

         }

}

 

运行结果:

AndroidGUI21:ImageSwitcher常用技巧_第2张图片

让下面的 Gallery 中的图片滚动,上面的 ImageSwitcher 会显示 Gallery 在屏幕上处于中间 ( 水平方向 ) 位置的那副图片:

AndroidGUI21:ImageSwitcher常用技巧_第3张图片

你可能感兴趣的:(android,object,layout,url,encoding,behavior)