
正文
android gallery 自定义边框+幻灯片
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
最近在项目中用到图片轮播,试了Gallery,ViewFlipper,ViewPager,感觉Gallery最符合需求,但是Gallery的系统边框很难看,项目中要求用自己的背景图片。
下面来看一下使用Gallery实现图片轮播
运行效果:
布局文件:
[java]
view
plaincopy
plaincopy
- <FrameLayout
- >
- />
- >
- />
- />
- />
- </FrameLayout>
其中, android:fadingEdge="none"消除图片两边的阴影。使用FrameLayout在底部显示小圆点
[java]
view
plaincopy
plaincopy
-
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
intif) {
- ].setImageDrawable(getBaseContext()
- if)) {
- ].setImageDrawable(getBaseContext()
- if) {
- ]
- publicvoid
- * 点击事件,点击图片进入SecondActivity
-
*/
privatenew -
publicvoidint
long
new
thisclass}
ImageAdapter类,重写android.widget.BaseAdapter,用于描述图像信息。
[java]
view
plaincopy
plaincopy
-
publicclassextends
private
privateintprivatestaticfinalint;
-
public
this -
publicint
return -
publicint
return -
publiclongint
return -
publicint
newnew
new
, , , ); -
return
}
如果用系统背景,可以这样写
[java]
view
plaincopy
plaincopy
-
int
privatepublic
// 获得Gallery组件的属性
);
- }
在getview中设置
[java]
view
plaincopy
plaincopy
- imageView.setBackgroundResource(mGalleryItemBackground);
Gallery组件属性信息定义在res\values\attrs.xml
[java]
view
plaincopy
plaincopy
- <?xml version= encoding=?>
- >
- />
- </resources>







