最近在项目中用到图片轮播,试了Gallery,ViewFlipper,ViewPager,感觉Gallery最符合需求,但是Gallery的系统边框很难看,项目中要求用自己的背景图片。
下面来看一下使用Gallery实现图片轮播
运行效果:

布局文件:
-
<FrameLayout
-
-
-
-
-
>
-
-
-
-
-
/>
-
-
-
-
-
-
-
-
>
-
-
-
-
/>
-
-
-
-
-
-
/>
-
-
-
-
-
-
-
/>
-
</FrameLayout>
其中, android:fadingEdge="none"消除图片两边的阴影。使用FrameLayout在底部显示小圆点
-
publicclassextends
private
privateint;
-
private
-
privatestaticfinalint;
-
-
publicvoid
super
].setImageDrawable(getBaseContext().getResources()
-
newthis
new
, );
-
privatevoid
new
privatenew
-
publicvoid
new
;
-
* 开一个线程执行耗时操作
-
*/
privatenew
-
publicvoid
super
switch
case:
-
break
default
break
* 设置小圆点显示,position会一直增加,如果要循环显示图片,需要对position取余,否则数组越界
-
*/
privatenew
-
publicvoid
intlong
int
if) {
-
].setImageDrawable(getBaseContext()
-
if)) {
-
].setImageDrawable(getBaseContext()
-
if) {
-
]
-
-
publicvoid
-
* 点击事件,点击图片进入SecondActivity
-
*/
privatenew
-
publicvoidint
long
new
thisclass
}
ImageAdapter类,重写android.widget.BaseAdapter,用于描述图像信息。
-
publicclassextends
private
privateint
privatestaticfinalint;
-
public
this
-
publicint
return
-
-
publicint
return
-
publiclongint
return
-
publicint
new
new
new
, , , );
-
-
return
}
如果用系统背景,可以这样写
-
int
private
public
// 获得Gallery组件的属性
);
-
}
在getview中设置
-
imageView.setBackgroundResource(mGalleryItemBackground);
Gallery组件属性信息定义在res\values\attrs.xml
-
<?xml version= encoding=?>
-
>
-
/>
-
</resources>
android gallery 自定义边框+幻灯片
原文:http://www.cnblogs.com/hjtdlx/p/3942290.html