android Gallery组件实现的iPhone图片滑动效果实例
程序员文章站
2022-05-27 10:21:54
实现的效果图,可左右滑动:
一、先在将gallery标签放入:
复制代码 代码如下:
二、gallery 需要用adapter来填充,先从baseadapter中派生一个imageadapter出来
实现的效果图,可左右滑动:
一、先在将gallery标签放入:
复制代码 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</linearlayout>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</linearlayout>
二、gallery 需要用adapter来填充,先从baseadapter中派生一个imageadapter出来
复制代码 代码如下:
public class imageadapter extends baseadapter
{
private context context;
private int[] myimageids =
{ r.drawable.icon, r.drawable.carlogo_52design_09,
r.drawable.carlogo_52design_13, r.drawable.carlogo_52design_19,
r.drawable.carlogo_52design_24, r.drawable.carlogo_52design_27,
r.drawable.carlogo_52design_29, r.drawable.carlogo_52design_31,
r.drawable.carlogo_52design_34, r.drawable.carlogo_52design_36 };
public imageadapter(context context)
{
// todo auto-generated constructor stub
this.context = context;
}
@override
public int getcount()
{
// todo auto-generated method stub
return myimageids.length;
}
@override
public object getitem(int arg0)
{
// todo auto-generated method stub
return arg0;
}
@override
public long getitemid(int position)
{
// todo auto-generated method stub
return position;
}
@override
public view getview(int position, view convertview, viewgroup parent)
{
// todo auto-generated method stub
imageview i = new imageview(this.context);
i.setimageresource(this.myimageids[position]);
i.setscaletype(imageview.scaletype.fit_xy);
i.setlayoutparams(new gallery.layoutparams(120, 120));
return i;
}
}
{
private context context;
private int[] myimageids =
{ r.drawable.icon, r.drawable.carlogo_52design_09,
r.drawable.carlogo_52design_13, r.drawable.carlogo_52design_19,
r.drawable.carlogo_52design_24, r.drawable.carlogo_52design_27,
r.drawable.carlogo_52design_29, r.drawable.carlogo_52design_31,
r.drawable.carlogo_52design_34, r.drawable.carlogo_52design_36 };
public imageadapter(context context)
{
// todo auto-generated constructor stub
this.context = context;
}
@override
public int getcount()
{
// todo auto-generated method stub
return myimageids.length;
}
@override
public object getitem(int arg0)
{
// todo auto-generated method stub
return arg0;
}
@override
public long getitemid(int position)
{
// todo auto-generated method stub
return position;
}
@override
public view getview(int position, view convertview, viewgroup parent)
{
// todo auto-generated method stub
imageview i = new imageview(this.context);
i.setimageresource(this.myimageids[position]);
i.setscaletype(imageview.scaletype.fit_xy);
i.setlayoutparams(new gallery.layoutparams(120, 120));
return i;
}
}