typearrary attrs 相关

本文介绍了Android中Gallery与ImageSwitcher组件的使用方法。详细讲述了如何通过Gallery展示图片,并利用ImageSwitcher实现图片切换时的动画效果。此外,还提供了具体的代码示例。

 在 实现gallery组件的应用时,会使用如下的代码。

 

            //Retrieve styled attribute information in this Context's theme
             TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);

            获取Gallery组件的属性集合。
            //Retrieve the resource identifier for the attribute at the special index.
            mGalleryItemBackground = typedArray.getResourceId(
                    R.styleable.Gallery_android_galleryItemBackground, 0);  
 

            根据属性的名称从typeArray 中获得属性值。

            give back a previously retrieved StyledAttributes, for later re-use.
            typedArray.recycle();

            该方法使得获得的属性能够应用到每一个对象上。

 

  关于imageSwitcher讲解

            imageSwitcher.setFactory(context);

            imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
            imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));

 

            imageSwitcher,顾名思义,就是图像转换器。 我们常用它来显示android 中的图片。虽然imageView也可以显示图片,

            但是ImageSwicher 具有一些特殊的功能,它本身在切换的时候我们可以加入一些动画效果。

 

             布局文件中的声明:

             <ImageSwitcher

                 android:id ="@+id/imageSwithcer"

                 android:layout_width="fill_parent"  android:layout_height="wrap_content"

              >

 

            从布局文件中得到该组:

            ImageSwitcher mSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);

            mSwticher.setFactory(context);

            如上所示,该组件应用中最重要的地方就是setFactory( ),我们要为它指定一个ViewFactory, 也就是定义它是如何它图像

            显示出来的。这个一般的做法是在使用imageSwitcher的类中实现ViewFactory接口并覆盖对应的makeView方法。

 

            public View makeView() {

             ImageView image = new ImageView(this);

             image.setMinimumHeight(200);

             image.setMinimumWidth(200);

             image.setScaleType(imageView.ScaleType.FIT_CENTER);

             image.setLayoutParams(new ImageSwitcher.layoutParams(layoutParams.FILL_PARENT,

             LayoutParams.FILL_PARENT));

             return  image;

 

            }

接下来开始添加动画效果。

mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
        android.R.anim.fade_out));

把图片显示出来,我们可以把该方法放到事件处理中,就形成了触发而发生图片转换的交互效果。

mSwitcher.setImageResource(R.drawable.icon);

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值