五、基本界面控件-3图片控件
5.3 图片控件
5.3.1 ImageView
图5.3.1ImageView
android.widget.ImageView图片控件,继承自android.view.View,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tool"/>
动态声明ImageView,设置src。
try {
ImageView imageView = new ImageView(this);
InputStream inputStream = super.getAssets().open("home.png");
imageView.setImageDrawable(Drawable.createFromStream(inputStream, "tackpic"));
this.imageLayout.addView(imageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
} catch (IOException e) {
e.printStackTrace();
}
5.3.2 ImageButton
图5.3.2ImageButton
android.widget.ImageButton图片控件,继承自android.widget.ImageView,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/but_01"/>
动态声明ImageView,设置src。
try {
ImageButton imageButton = new ImageButton(this);
InputStream inputStream = super.getAssets().open("but_02.png");
imageButton.setImageDrawable(Drawable.createFromStream(inputStream, "tackpic"));
this.imageLayout.addView(imageButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
} catch (IOException e) {
e.printStackTrace();
}
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文件。
<?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类。
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类
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;
}
}
}
- 大小: 18.6 KB
- 大小: 9.8 KB
- 大小: 170.7 KB
分享到:
相关推荐
【Android开发学习29】界面控件之列表控件(ListView)--显示图片和文本 代码
【Android开发学习28】界面控件之列表控件(ListView)--显示字符数组
1、Android01--搭建Android开发环境 2、Android02--认识Activity 3、Android03--Context和Application 4、Android04--Android服务 5、Android05--Android服务通信 6、Android06--Android广播接收器 7、Android07--...
Android基本用户界面控件详解,主要包括Radiobutton,ListView,Spinner等控件的详细讲解
android安卓app开发之widget界面控件使用教程.zip
Android自定义控件-EditText(可用于登陆界面)通用
android安卓app界面效果全汇总-android界面编程-控件使用教程 源代码
ViewPager,Android,Fragment,Activity Android 界面控件学习
Android的用户界面控件分类 定制控件 系统控件 基本控件 复杂控件
基本控件的使用:1.TextView、EditText2. Button3.ListView4.Toggle5.RadioBtn6.CheckBox7.Spinner8.Clock9.Picker
android 自定义拖动控件。控件可以在界面上自由拖动位置显示。自定义图标背景颜色及默认显示的位置。
列举了12种常见的android控件和基本用法,可参考博客一起学习!
Android图片轮播控件 新框架发布,欢迎大家Star XFrame - Android快速开发框架 XFrame详细功能文档预览 现在的绝大数app都有banner界面,实现循环播放多个广告图片和手动滑动循环等功能。因为ViewPager并不支持...
android安卓app开发之漂亮的UI界面,控件_附效果图和源代码.zip-安卓动画导航栏,滑动广告
【Android开发学习27】界面控件之进度条(ProgressBar)--代码,博客主页: http://blog.csdn.net/ypist.
使用RecyclerView控件完成的一个简单的聊天界面。功能不是很健全。慎重下载哦。
android UI界面控件元素集,比如菜单,单选按钮、按钮文字和背景的设置、带动画的按钮、按钮事件监听的实现、checkbox进行监听、EditText编辑框、单项选择、多项选择、Menu菜单、WebView使用等。
全书400多页,从开发环境的安装配置到使用基本界面控件、使用移动设备功能(移动定位、 照像机等)、使用后端服务、访问数据库等都作了全面介绍,是第一本国内最全的中文版Delphi XE8技术开发教程。本书需要有一定...
Android 控件说明Android 控件说明Android 控件说明Android 控件说明Android 控件说明Android 控件说明Android 控件说明Android 控件说明Android 控件说明
Android Android Android Android 开发背景 � 计算技术、无线接入技术的发展,使嵌入式系统逐渐有能力对桌面系统常规业务进行支持。 � 谷歌长期以来奉行的移动发展战略:通过与全球各地的手机制造商和移动运营商...