先上個效果圖~
思路是這樣的,功能方面:
首先這個是個左右循環的Gallery(其實是Integer.MAX_VALUE = 2147483647 這麼多的個啦,接近無限了)。
這個網上有很多,不再贅述。代碼裏面也有,可以直接下載~
然後就是Gallery的樣式,我這裏 設置成無陰影的,間距 android:spacing="0dip"。
最後就是下面的指示條了,我使用FrameLayout佈局,裏面的指示點 radiobuttion.(因爲只要一個是點亮的,用於指示當前位置,所以在一個group中)
下面是重要代碼:
佈局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="150dip" >
<com.test.AdvGallery
android:fadingEdge="none"
android:id="@+id/home_advs_gallery"
android:spacing="0dip"
android:layout_width="fill_parent"
android:layout_height="150dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="20dip"
android:layout_gravity="bottom"
android:background="#55999999"
android:gravity="center"
android:orientation="horizontal" >
<RadioGroup
android:gravity="center"
android:id="@+id/home_advs_gallery_mark"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</RadioGroup>
</LinearLayout>
</FrameLayout>
</LinearLayout>
自定義Gallery,爲了解決Gallery拖拽滑動過快:
public class AdvGallery extends Gallery {
public AdvGallery(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public AdvGallery(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
//返回false 解決Gallery拖拽滑動過快
return false;
}
@Override
public void setUnselectedAlpha(float unselectedAlpha) {
// TODO Auto-generated method stub
unselectedAlpha = 1.0f;
super.setUnselectedAlpha(unselectedAlpha);
}
adapter中的 getview方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(context);
String curr_URL = imgURL.get(position%imgURL.size());
imageView.setTag(curr_URL);
Drawable cachedImage = asyncImageLoader.loadDrawable(context,curr_URL,new ImageCallback1() {
@Override
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
ImageView imageViewByTag = (ImageView) gallery.findViewWithTag(imageUrl);
if (imageViewByTag != null && imageDrawable != null ) {
imageViewByTag.setImageDrawable(imageDrawable);
notifyDataSetChanged();
}
}
});
if (cachedImage != null) {
imageView.setImageDrawable(cachedImage);
}else{
imageView.setImageResource(R.drawable.ic_launcher);
}
// 設置邊界對齊
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//設置比例類型
// imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
main中的oncreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
_radioGroup = (RadioGroup) findViewById(R.id.home_advs_gallery_mark);
_adv_Gallery = (Gallery) findViewById(R.id.home_advs_gallery);
_advGalleryAdapter = new AdvGalleryAdapter(ADV_GalleryActivity.this,_adv_imgURL,_adv_Gallery);
_adv_Gallery.setAdapter(_advGalleryAdapter);
_adv_Gallery.setSelection(Integer.MAX_VALUE >> 1);
_adv_Gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
_radioGroup.check(arg2%_adv_imgURL.size()); //Gallery焦點圖片改變時 更改RadioGroup
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//圖片地址
_adv_imgURL.add("http://www.baidu.com/img/baidu_sylogo1.gif");
_adv_imgURL.add("http://www.iteye.com/images/logo.gif?1308833136");
_adv_imgURL.add("http://csdnimg.cn/www/images/csdnindex_logo.gif");
for(int i=0;i<_adv_imgURL.size();i++){
RadioButton rb = new RadioButton(ADV_GalleryActivity.this);
rb.setId(i);
rb.setButtonDrawable(R.drawable.adv_gallery_mark_selector);
rb.setClickable(false);
_radioGroup.addView(rb);
}
}
由於代碼比較多,放上源碼,希望大家能用到~!