Android 基础控件之ImageSwitcher、Gallery功能的实现
2015-08-04 10:46
567 查看
本文主要介绍Android 基础控件之ImageSwitcher、Gallery功能的实现,具体操作看代码。
1.ImageShowActivity.java
2.布局文件image_show.xml
3.注意:需要在AndroidManifest.xml注册相应Activity.
1.ImageShowActivity.java
import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.Gallery.LayoutParams; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher; //显示画像效果 @SuppressWarnings("deprecation") public class ImageShowActivity extends Activity implements ViewSwitcher.ViewFactory{ private ImageSwitcher mSwitcher; private Gallery gallery; private Context mContext; private Integer[] mImageIds = { R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7,R.drawable.sample_8, R.drawable.sample_9,R.drawable.sample_10,R.drawable.sample_11}; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.image_show); setTitle("浏览图片"); mSwitcher=(ImageSwitcher)findViewById(R.id.switcher); gallery=(Gallery)findViewById(R.id.gallery); mSwitcher.setFactory(this); mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) { mSwitcher.setImageResource(mImageIds[position]); } @Override public void onNothingSelected(AdapterView<?> arg0) { } }); } public class ImageAdapter extends BaseAdapter{ public ImageAdapter(Context context){ mContext=context; } @Override public int getCount() { return mImageIds.length; } @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) { ImageView i=new ImageView(mContext); i.setImageResource(mImageIds[position]); i.setAdjustViewBounds(true); i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); i.setBackgroundResource(R.drawable.picture_frame); return i; } } @Override public View makeView() { ImageView i=new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); return i; } }
2.布局文件image_show.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageSwitcher android:id="@+id/switcher" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" /> <Gallery android:id="@+id/gallery" android:background="#ff00ff" android:layout_width="fill_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp" /> </RelativeLayout>
3.注意:需要在AndroidManifest.xml注册相应Activity.
相关文章推荐
- Android中再按一次退出的实现
- Android Studio 1.3 发布
- android webview type=file文件上传,安卓端代码
- android.os.NetworkOnMainThreadException的解决方案
- 修改Android中strings.xml文件, 动态改变数据
- Android之Activity的几种跳转方式
- android开发必备日志打印工具类
- android开发必备日志打印工具类
- android开发必备日志打印工具类
- android开发必备日志打印工具类
- 修改zxing扫描二维码区域大小
- android开发常用知识点备忘录
- android 闪屏
- Android异步消息之Looper、Handler、Message、HandlerThread的关系
- android studio编译出错——Failed to resolve: com.android.support:support-v4:22.2.0
- 一个优秀的Android应用从建项目开始
- Android 数据存储
- Android Butterknife框架
- Android FragmentTabHost使用
- GitHub Android 最火开源项目Top20