当前位置:操作系统 > 安卓/Android >>

Android 程式开发:(十四)显示图像 —— 14.2 ImageSwitcher

前面的一节,介绍了如何组合使用Gallery和ImageView。但是,有的时候当你在gallery中点击一个图片,你可能不希望一个图片“突然地”在imageview中显示出来。例如,你可能希望给某个图片设置一些切换动画。此时,就需要使用ImageSwitcher和Gallery一起使用。下面展示如何使用ImageSwitcher。

1. 创建一个工程,ImageSwitcher。

2. main.xml中的代码。

[html] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
     
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Images of San Francisco" />     
 
<Gallery 
    android:id="@+id/gallery1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" /> 
 
<ImageSwitcher 
    android:id="@+id/switcher1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" /> 
 
</LinearLayout> 
3. 在res/values文件夹下面新建一个文件,attrs.xml。
4. attrs.xml中的代码。

[html] 
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="Gallery1"> 
        <attr name="android:galleryItemBackground" /> 
    </declare-styleable> 
</resources> 
5. 在res/drawable-mdpi中放置一些图片。


6. ImageSwitcherActivity.java中的代码。
[java]
public class ImageSwitcherActivity extends Activity implements ViewFactory { 
    //---the images to display--- 
    Integer[] imageIDs = { 
            R.drawable.pic1, 
            R.drawable.pic2, 
            R.drawable.pic3, 
            R.drawable.pic4, 
            R.drawable.pic5, 
            R.drawable.pic6, 
            R.drawable.pic7 
    }; 
 
    private ImageSwitcher imageSwitcher; 
     
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher1); 
        imageSwitcher.setFactory(this); 
         
        /*
        淡入淡出效果
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        */ 
         
        // 左右滑动效果 
        imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
                android.R.anim.slide_in_left)); 
        imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
                android.R.anim.slide_out_right)); 
 
 
        Gallery gallery = (Gallery) findViewById(R.id.gallery1); 
        gallery.setAdapter(new ImageAdapter(this)); 
        gallery.setOnItemClickListener(new OnItemClickListener() 
        { 
            public void onItemClick(AdapterView<?> parent, 
            View v, int position, long id) 
            { 
                imageSwitcher.setImageResource(imageIDs[position]); 
            } 
        }); 
    } 
     
    public View makeView() 
    { 
        ImageView imageView = new ImageView(this); 
        imageView.setBackgroundColor(0xFF000000); 
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
        imageView.setLayoutParams(new 
                ImageSwitcher.LayoutParams( 
                        LayoutParams.FILL_PARENT, 
                        LayoutParams.FILL_PARENT)); 
        return imageView; 
    } 
 
    public class ImageAdapter extends BaseAdapter 
    { 
        private Context context; 
        private int itemBa

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,