Android特效第三篇:自定义Gallery实战(仿网易) .
2012-09-18 17:43
337 查看
Android系统提供了一个Gallery画廊控件,在项目很多时候都会用到Gallery,比如新浪首页的广告,网易看客户端首页等随处可见,今天我自己定义了一个仿网易的Gallery与大家共享。
首先请看效果图:
代码:
[java]
view plaincopyprint?
package com.jefry.gallery;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author jefry
*
*/
public class MyGallery extends FrameLayout {
List<Showing> showings = new ArrayList<Showing>();
private static final double GALLERY_IMAGE_HORIZONTAL_RATIO = 1.0;
private static final double GALLERY_IMAGE_VERTICAL_RATIO = 1.0;
private static final int GALLERY_SPACING = 2;
private Context mContext;
private PromotionImages promotionImages;
private LinearLayout mBottomLayout;
private TextView tips;
private int mPcount;
private ImageView[] icons;
public MyGallery(Context context) {
super(context);
}
public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
promotionImages = new PromotionImages(context);
LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);
layoutParams.bottomMargin = 20;
this.addView(promotionImages, layoutParams);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View indicator = inflater.inflate(R.layout.promotion_hint,null);
this.addView(indicator);
//
mBottomLayout = (LinearLayout) indicator.findViewById(R.id.promotion_index_layout);
tips = (TextView) indicator.findViewById(R.id.promotion_tip);
initBottomIcons();
}
class PromotionImages extends Gallery {
PromotionImagesAdapter promotionAdapter;
public PromotionImages(Context context) {
super(context);
this.setSpacing(GALLERY_SPACING);
promotionAdapter = new PromotionImagesAdapter(context);
this.setAdapter(promotionAdapter);
this.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
update(position);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private final void update(int selected) {
tips.setText(showings.get(selected).getText());
for (int i = 0; i < mPcount; i++) {
if (selected == i) {
icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
} else {
icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
}
}
}
}
private final void initBottomIcons() {
mPcount = showings.size();
icons = new ImageView[mPcount];
mBottomLayout.removeAllViews();
mBottomLayout.setWeightSum(mPcount);
//
for (int i = 0; i < mPcount; i++) {
icons[i] = new ImageView(mContext);
if(i == 0) icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
else icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
mBottomLayout.addView(icons[i]);
}
}
private final class PromotionImagesAdapter extends BaseAdapter {
public PromotionImagesAdapter(Context context) {
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res,
R.drawable.test2);
Showing show1 = new Showing();
show1.setBitmap(bmp);
show1.setText("jefry_1");
Bitmap bmp1 = BitmapFactory.decodeResource(res,
R.drawable.test1);
Showing show2 = new Showing();
show2.setBitmap(bmp1);
show2.setText("jefry_2");
Bitmap bmp3 = BitmapFactory.decodeResource(res,
R.drawable.test3);
Showing show3 = new Showing();
show3.setBitmap(bmp3);
show3.setText("jefry_3");
showings.add(show1);
showings.add(show2);
showings.add(show3);
}
public int getCount() {
return showings.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, final View convertView,
ViewGroup parent) {
//System.out.println("showings = " + showings.size());
//System.out.println("position=" + position);
final Showing promotion = showings.get(position);
ImageView promotionImage = (ImageView) convertView;
if (promotionImage == null) {
promotionImage = new ImageView(mContext);
System.out.println("postion=" + position + ",promotion.getBitmap()1 = "+ promotion.getBitmap());
}
int width = (int) (MyGallery.this.getWidth() * GALLERY_IMAGE_HORIZONTAL_RATIO);
int height = (int) (MyGallery.this.getHeight() * GALLERY_IMAGE_VERTICAL_RATIO);
promotionImage.setLayoutParams(new Gallery.LayoutParams(width,height));
promotionImage.setScaleType(ImageView.ScaleType.CENTER);
promotionImage.setScaleType(ImageView.ScaleType.FIT_XY);
System.out.println("postion=" + position + ",promotion.getBitmap() = "+ promotion.getBitmap());
promotionImage.setImageBitmap(promotion.getBitmap());
return promotionImage;
}
}
}
源码下载:http://download.csdn.net/detail/jefry_xdz/4521377
首先请看效果图:
代码:
[java]
view plaincopyprint?
package com.jefry.gallery;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* @author jefry
*
*/
public class MyGallery extends FrameLayout {
List<Showing> showings = new ArrayList<Showing>();
private static final double GALLERY_IMAGE_HORIZONTAL_RATIO = 1.0;
private static final double GALLERY_IMAGE_VERTICAL_RATIO = 1.0;
private static final int GALLERY_SPACING = 2;
private Context mContext;
private PromotionImages promotionImages;
private LinearLayout mBottomLayout;
private TextView tips;
private int mPcount;
private ImageView[] icons;
public MyGallery(Context context) {
super(context);
}
public MyGallery(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
promotionImages = new PromotionImages(context);
LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT);
layoutParams.bottomMargin = 20;
this.addView(promotionImages, layoutParams);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View indicator = inflater.inflate(R.layout.promotion_hint,null);
this.addView(indicator);
//
mBottomLayout = (LinearLayout) indicator.findViewById(R.id.promotion_index_layout);
tips = (TextView) indicator.findViewById(R.id.promotion_tip);
initBottomIcons();
}
class PromotionImages extends Gallery {
PromotionImagesAdapter promotionAdapter;
public PromotionImages(Context context) {
super(context);
this.setSpacing(GALLERY_SPACING);
promotionAdapter = new PromotionImagesAdapter(context);
this.setAdapter(promotionAdapter);
this.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
update(position);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private final void update(int selected) {
tips.setText(showings.get(selected).getText());
for (int i = 0; i < mPcount; i++) {
if (selected == i) {
icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
} else {
icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
}
}
}
}
private final void initBottomIcons() {
mPcount = showings.size();
icons = new ImageView[mPcount];
mBottomLayout.removeAllViews();
mBottomLayout.setWeightSum(mPcount);
//
for (int i = 0; i < mPcount; i++) {
icons[i] = new ImageView(mContext);
if(i == 0) icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus));
else icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default));
mBottomLayout.addView(icons[i]);
}
}
private final class PromotionImagesAdapter extends BaseAdapter {
public PromotionImagesAdapter(Context context) {
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res,
R.drawable.test2);
Showing show1 = new Showing();
show1.setBitmap(bmp);
show1.setText("jefry_1");
Bitmap bmp1 = BitmapFactory.decodeResource(res,
R.drawable.test1);
Showing show2 = new Showing();
show2.setBitmap(bmp1);
show2.setText("jefry_2");
Bitmap bmp3 = BitmapFactory.decodeResource(res,
R.drawable.test3);
Showing show3 = new Showing();
show3.setBitmap(bmp3);
show3.setText("jefry_3");
showings.add(show1);
showings.add(show2);
showings.add(show3);
}
public int getCount() {
return showings.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, final View convertView,
ViewGroup parent) {
//System.out.println("showings = " + showings.size());
//System.out.println("position=" + position);
final Showing promotion = showings.get(position);
ImageView promotionImage = (ImageView) convertView;
if (promotionImage == null) {
promotionImage = new ImageView(mContext);
System.out.println("postion=" + position + ",promotion.getBitmap()1 = "+ promotion.getBitmap());
}
int width = (int) (MyGallery.this.getWidth() * GALLERY_IMAGE_HORIZONTAL_RATIO);
int height = (int) (MyGallery.this.getHeight() * GALLERY_IMAGE_VERTICAL_RATIO);
promotionImage.setLayoutParams(new Gallery.LayoutParams(width,height));
promotionImage.setScaleType(ImageView.ScaleType.CENTER);
promotionImage.setScaleType(ImageView.ScaleType.FIT_XY);
System.out.println("postion=" + position + ",promotion.getBitmap() = "+ promotion.getBitmap());
promotionImage.setImageBitmap(promotion.getBitmap());
return promotionImage;
}
}
}
package com.jefry.gallery; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.FrameLayout; import android.widget.Gallery; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; /** * @author jefry * */ public class MyGallery extends FrameLayout { List<Showing> showings = new ArrayList<Showing>(); private static final double GALLERY_IMAGE_HORIZONTAL_RATIO = 1.0; private static final double GALLERY_IMAGE_VERTICAL_RATIO = 1.0; private static final int GALLERY_SPACING = 2; private Context mContext; private PromotionImages promotionImages; private LinearLayout mBottomLayout; private TextView tips; private int mPcount; private ImageView[] icons; public MyGallery(Context context) { super(context); } public MyGallery(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; promotionImages = new PromotionImages(context); LayoutParams layoutParams = new LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.FILL_PARENT); layoutParams.bottomMargin = 20; this.addView(promotionImages, layoutParams); LayoutInflater inflater = ((Activity) context).getLayoutInflater(); View indicator = inflater.inflate(R.layout.promotion_hint,null); this.addView(indicator); // mBottomLayout = (LinearLayout) indicator.findViewById(R.id.promotion_index_layout); tips = (TextView) indicator.findViewById(R.id.promotion_tip); initBottomIcons(); } class PromotionImages extends Gallery { PromotionImagesAdapter promotionAdapter; public PromotionImages(Context context) { super(context); this.setSpacing(GALLERY_SPACING); promotionAdapter = new PromotionImagesAdapter(context); this.setAdapter(promotionAdapter); this.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { update(position); } public void onNothingSelected(AdapterView<?> parent) { } }); } private final void update(int selected) { tips.setText(showings.get(selected).getText()); for (int i = 0; i < mPcount; i++) { if (selected == i) { icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus)); } else { icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default)); } } } } private final void initBottomIcons() { mPcount = showings.size(); icons = new ImageView[mPcount]; mBottomLayout.removeAllViews(); mBottomLayout.setWeightSum(mPcount); // for (int i = 0; i < mPcount; i++) { icons[i] = new ImageView(mContext); if(i == 0) icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_focus)); else icons[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.promotion_select_default)); mBottomLayout.addView(icons[i]); } } private final class PromotionImagesAdapter extends BaseAdapter { public PromotionImagesAdapter(Context context) { Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.test2); Showing show1 = new Showing(); show1.setBitmap(bmp); show1.setText("jefry_1"); Bitmap bmp1 = BitmapFactory.decodeResource(res, R.drawable.test1); Showing show2 = new Showing(); show2.setBitmap(bmp1); show2.setText("jefry_2"); Bitmap bmp3 = BitmapFactory.decodeResource(res, R.drawable.test3); Showing show3 = new Showing(); show3.setBitmap(bmp3); show3.setText("jefry_3"); showings.add(show1); showings.add(show2); showings.add(show3); } public int getCount() { return showings.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, final View convertView, ViewGroup parent) { //System.out.println("showings = " + showings.size()); //System.out.println("position=" + position); final Showing promotion = showings.get(position); ImageView promotionImage = (ImageView) convertView; if (promotionImage == null) { promotionImage = new ImageView(mContext); System.out.println("postion=" + position + ",promotion.getBitmap()1 = "+ promotion.getBitmap()); } int width = (int) (MyGallery.this.getWidth() * GALLERY_IMAGE_HORIZONTAL_RATIO); int height = (int) (MyGallery.this.getHeight() * GALLERY_IMAGE_VERTICAL_RATIO); promotionImage.setLayoutParams(new Gallery.LayoutParams(width,height)); promotionImage.setScaleType(ImageView.ScaleType.CENTER); promotionImage.setScaleType(ImageView.ScaleType.FIT_XY); System.out.println("postion=" + position + ",promotion.getBitmap() = "+ promotion.getBitmap()); promotionImage.setImageBitmap(promotion.getBitmap()); return promotionImage; } } }
源码下载:http://download.csdn.net/detail/jefry_xdz/4521377
相关文章推荐
- Android实战(橘子娱乐)-首页(第三篇 IRecyclerView自定义刷新头ClassicRefreshHeaderView和SharedPreferencesUtils)
- Android自定义线程池的编程实战
- Android基础教程之-----访Iphone 拖动相片特效Gallery的简单应用.
- Android 自定义ViewGroup 实战篇 -> 实现FlowLayout
- Android实战简易教程-第七十枪(自定义实用控制之-邮箱验证EditText)
- Android自定义Gallery控件实现3D图片浏览器
- 【Android自定义View实战】之自定义项目通用的加载等待对话框LoadingDialog
- Android特效专辑(五)——自定义圆形头像和仿MIUI卸载动画—粒子爆炸
- Android特效专辑(五)——自定义圆形头像和仿MIUI卸载动画—粒子爆炸
- 【Android实战】记录自学自定义GifView过程,详解属性那些事!【学习篇】
- android gallery 自定义边框+幻灯片
- android自定义Gallery实现手动和自动循环滚动切换图片
- Android绘图机制(三)——自定义View的三种实现方式以及实战项目操作
- Android 自定义线程池的实战
- Android 自定义View高级特效,神奇的贝塞尔曲线
- 【Android实战】Bundle传递自定义Object的ArrayList
- Android自定义控件之仿网易星球浮动小球
- [置顶] MPAndroidChart项目实战(九)——自定义带文字分段堆积柱状图
- 【Android自定义View实战】之自定义项目通用的标题栏CustomTitleBar
- 【Android细节(一)】Gallery设置自定义的背景属性