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

被折叠的 条评论
为什么被折叠?



