5.3 图片控件
5.3.1 ImageView
图5.3.1ImageView
android.widget.ImageView图片控件,继承自android.view.View,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
动态声明ImageView,设置src。
5.3.2 ImageButton
图5.3.2ImageButton
android.widget.ImageButton图片控件,继承自android.widget.ImageView,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
动态声明ImageView,设置src。
5.3.3 ImageSwitcher和Gallery
图5.3.3 ImageSwitcher
android.widget. ImageSwitcher图片控件,继承自android.widget.ViewSwitcher(ViewGroup)。在android.widget包中。
ImageSwithcer是用来图片显示那块区域的控件,使用方法setInAnimation(Animation),setOutAnimation(Animation)设置动画。
Gallery是来控制底下那个图标索引列表索引用的。ImageAdapter继承自BaseAdapter,设置Gallery的适配器。
在layout添加ImageSwitcher和Gallery。定义 Activity,implements接口OnItemSelectedListener, ViewFactory。onCreate的时候定义要显示图片路径列表,设置Gallery的Adapter。onItemSelected事件触发 时,设置对应的图片。
Layout文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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="#55000000" 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> |
SwitcherActivity类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class SwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory { private ImageSwitcher imageSwitcher; private Gallery gallery; private ArrayList<String> imageAssetPathList = new ArrayList<String>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.switcher); this.imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher); this.gallery = (Gallery) findViewById(R.id.gallery); for (int i = 1; i <= 20; i++) { this.imageAssetPathList.add("images/" + i + ".jpg"); } this.imageSwitcher.setFactory(this); this.gallery.setAdapter(new ImageAdapter(this, this.imageAssetPathList)); this.gallery.setOnItemSelectedListener(this); } @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; } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { try { InputStream inputStream = super.getAssets().open(this.imageAssetPathList.get(position)); imageSwitcher.setImageDrawable(Drawable.createFromStream(inputStream, "" + position)); } catch (IOException e) { e.printStackTrace(); } } @Override public void onNothingSelected(AdapterView<?> parent) { } } |
ImageAdapter类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
public class ImageAdapter extends BaseAdapter { private Context content; private ArrayList<String> imageAssetPathList; public ImageAdapter(Context content, ArrayList<String> imageAssetPathList) { this.content = content; this.imageAssetPathList = imageAssetPathList; } @Override public int getCount() { if (this.imageAssetPathList != null) { return this.imageAssetPathList.size(); } else { return 0; } } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { try { ImageView imageView; imageView = new ImageView(this.content); imageView.setAdjustViewBounds(true); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setPadding(0, 0, 0, 0); InputStream inputStream = this.content.getAssets().open(this.imageAssetPathList.get(position)); imageView.setImageDrawable(Drawable.createFromStream(inputStream, "" + position)); return imageView; } catch (IOException e) { e.printStackTrace(); return null; } } } |
原文链接:http://limingnihao.javaeye.com/blog/851408
相关推荐
Android基本用户界面控件详解,主要包括Radiobutton,ListView,Spinner等控件的详细讲解
11、Android11--基本布局 12、Android12--常见控件(一) 13、Android13--常见控件(二) 14、Android14--Adapter 15、Android15--ListView 16、Android16--自定义控件 18、Android18--菜单 数据存储 待学习。 网络与...
android UI界面控件元素集,比如菜单,单选按钮、按钮文字和背景的设置、带动画的按钮、按钮事件监听的实现、checkbox进行监听、EditText编辑框、单项选择、多项选择、Menu菜单、WebView使用等。
基本控件的使用:1.TextView、EditText2. Button3.ListView4.Toggle5.RadioBtn6.CheckBox7.Spinner8.Clock9.Picker
【Android开发学习29】界面控件之列表控件(ListView)--显示图片和文本 代码
android安卓app界面效果全汇总-android界面编程-控件使用教程 源代码
Android的用户界面控件分类 定制控件 系统控件 基本控件 复杂控件
【Android开发学习28】界面控件之列表控件(ListView)--显示字符数组
Android自定义控件-EditText(可用于登陆界面)通用
android 自定义拖动控件。控件可以在界面上自由拖动位置显示。自定义图标背景颜色及默认显示的位置。
Android图片轮播控件 新框架发布,欢迎大家Star XFrame - Android快速开发框架 XFrame详细功能文档预览 现在的绝大数app都有banner界面,实现循环播放多个广告图片和手动滑动循环等功能。因为ViewPager并不支持...
列举了12种常见的android控件和基本用法,可参考博客一起学习!
android个人消费记录软件-拖控件作品.zip
手机安全卫士--Android自定义组合控件实现设置功能,界面采用Android自定义组合控件的方式实现,更多详细信息请访问 http://blog.csdn.net/qq_20889581?viewmode=contents 文明的小流氓的博客
chm格式的帮助文档,全英文版。这是第一部分(共两部分)
Android中的常用控件及其基本用法 TextView的使用方法 EditText的使用方法 Button的使用方法 Menu的使用方法 RadioGroup和RadioButton
ViewPager,Android,Fragment,Activity Android 界面控件学习
android 页面各种控件加载动画效果
《 List控件使用---SimpleAdapter使用详解(二)》:实现从本地JSON文件生成listView界面;对应博文地址:http://blog.csdn.net/harvic880925/article/details/17288687