android-gallery-头像选择器效果
2011-05-09 12:54
555 查看
/**
*
* 测试gallery gallery头像选择器效果
*/
public class GalleryTestActivity extends Activity implements
OnItemSelectedListener, ViewFactory {
Gallery gallery;// xml中的组件Gallery
ImageAdapater imageAdapater;// 自定义的ImageAdapater
private ImageSwitcher imageSwitcher;// xml中的组件ImageSwitcher
// 资源drawable
private int[] resIds = new int[] { R.drawable.head0, R.drawable.head1,
R.drawable.head2, R.drawable.head3 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 加载Gallery组件
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapater = new ImageAdapater(this);
gallery.setAdapter(imageAdapater);
gallery.setOnItemSelectedListener(this);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
// 设置ImageSwitcher组件的工厂对象
imageSwitcher.setFactory(this);
// 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
}
int mGalleryItemBackground;
public class ImageAdapater extends BaseAdapter {
private Context mContext;
public ImageAdapater(Context mContext) {
this.mContext = mContext;
// 获得Gallery组件的属性
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
}
@Override
public int getCount() {
return resIds.length;// Integer.MAX_VALUE;//return 改成后面的可实现循环
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView = new ImageView(mContext);
// 设置当前图像的图像(position为当前图像列表的位置)
imageView.setImageResource(resIds[position % resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(90, 90));
// 设置Gallery组件的背景风格//系统默认的带边框的,入想去掉,可在onItemSelected()处理
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
int selectPos;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
imageSwitcher.setImageResource(resIds[position % resIds.length]);
selectPos = position;
for (int i = 0; i < parent.getChildCount(); i++) {
if (i == position) {
view.setBackgroundResource(R.drawable.headseletor);// 选中效果
} else {
parent.getChildAt(i).setBackgroundResource(R.drawable.dialogbg);// 透明背景
// 让默认的选框不可见
}
if (position == parent.getCount() - 1) {
view.setBackgroundResource(R.drawable.headseletor);//
}
}
// imageAdapater.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
};
// ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
// 来显示图像
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
}
//////////////////////////////////////////////////////////布局相关/////////////////////////////////////////////////////////////////
path /GalleryTest/res/layout/main.xml
<?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"
>
<!-- Gallery 组件-->
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="40px"
android:spacing="5px"
style="@drawable/selector_head"/>
<!-- ImageSwitcher 组件 用于展示Gallery选中的资源显示-->
<ImageSwitcher android:id="@+id/imageswitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="40px" />
</LinearLayout>
path /GalleryTest/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- 定义 Gallery的属性-->
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
*
* 测试gallery gallery头像选择器效果
*/
public class GalleryTestActivity extends Activity implements
OnItemSelectedListener, ViewFactory {
Gallery gallery;// xml中的组件Gallery
ImageAdapater imageAdapater;// 自定义的ImageAdapater
private ImageSwitcher imageSwitcher;// xml中的组件ImageSwitcher
// 资源drawable
private int[] resIds = new int[] { R.drawable.head0, R.drawable.head1,
R.drawable.head2, R.drawable.head3 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 加载Gallery组件
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapater = new ImageAdapater(this);
gallery.setAdapter(imageAdapater);
gallery.setOnItemSelectedListener(this);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
// 设置ImageSwitcher组件的工厂对象
imageSwitcher.setFactory(this);
// 设置ImageSwitcher组件显示图像的动画效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
}
int mGalleryItemBackground;
public class ImageAdapater extends BaseAdapter {
private Context mContext;
public ImageAdapater(Context mContext) {
this.mContext = mContext;
// 获得Gallery组件的属性
TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = typedArray.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
}
@Override
public int getCount() {
return resIds.length;// Integer.MAX_VALUE;//return 改成后面的可实现循环
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView = new ImageView(mContext);
// 设置当前图像的图像(position为当前图像列表的位置)
imageView.setImageResource(resIds[position % resIds.length]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(90, 90));
// 设置Gallery组件的背景风格//系统默认的带边框的,入想去掉,可在onItemSelected()处理
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
int selectPos;
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// 选中Gallery中某个图像时,在ImageSwitcher组件中放大显示该图像
imageSwitcher.setImageResource(resIds[position % resIds.length]);
selectPos = position;
for (int i = 0; i < parent.getChildCount(); i++) {
if (i == position) {
view.setBackgroundResource(R.drawable.headseletor);// 选中效果
} else {
parent.getChildAt(i).setBackgroundResource(R.drawable.dialogbg);// 透明背景
// 让默认的选框不可见
}
if (position == parent.getCount() - 1) {
view.setBackgroundResource(R.drawable.headseletor);//
}
}
// imageAdapater.notifyDataSetChanged();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
};
// ImageSwitcher组件需要这个方法来创建一个View对象(一般为ImageView对象)
// 来显示图像
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(0xFF000000);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
return imageView;
}
}
//////////////////////////////////////////////////////////布局相关/////////////////////////////////////////////////////////////////
path /GalleryTest/res/layout/main.xml
<?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"
>
<!-- Gallery 组件-->
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_marginTop="40px"
android:spacing="5px"
style="@drawable/selector_head"/>
<!-- ImageSwitcher 组件 用于展示Gallery选中的资源显示-->
<ImageSwitcher android:id="@+id/imageswitcher"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="40px" />
</LinearLayout>
path /GalleryTest/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- 定义 Gallery的属性-->
<declare-styleable name="Gallery1">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
相关文章推荐
- Android 仿iOS头像选择效果
- 学习Android之第七个小程序头像选择(自定义对话框、Gallery、ImageSwitcher)
- 头像选择效果(ImageButton,Gallery,ImageSwitcher)
- Android仿京东金融首页头像效果
- android开发中注册的时候,从A跳到B选择头像,再返回到A界面,除了B界面新增的数据,A界面原先的信息依然存在
- Android 滑动效果基础篇(四)—— Gallery + GridView
- Android开发 点击在本地选择换图片(头像)并且保存,实例总结。
- Android UI控件之Gallery实现循环效果总结
- 让Android自带的Gallery实现多点缩放,拖动和边界回弹效果,效果流畅
- android 头像组合效果实现
- Android Gallery3D效果 教程 案例 代码
- Android 广告效果实现(gallery+小圆点)
- android高仿微信UI点击头像显示大图片效果
- Android的gallery实现无限循环和降低选择频率
- android 选择头像上传
- android Gallery 实现循环播放的效果
- Android开发实现自定义日历、日期选择控件效果
- Android自定义AvatarImageView实现头像显示效果
- Android调用摄像头拍照和从相册中选择(上传、更换头像)
- Android实现从相册选择或者直接拍照来设置圆形的头像CircleImageView