Android开发学习 之 五、基本界面控件-3图片控件

 

五、基本界面控件-3图片控件

5.3 图片控件

5.3.1 ImageView


图5.3.1ImageView

 

android.widget.ImageView图片控件,继承自android.view.View,在android.widget包中。

最简单的使用方法。src设置图片路径,可引用drawable的图片。

 

Xml代码 复制代码  收藏代码
  1. <ImageView android:layout_width="wrap_content"    
  2.            android:layout_height="wrap_content"  
  3.            android:src="@drawable/tool"/>  
 

 

动态声明ImageView,设置src。

 

Java代码 复制代码  收藏代码
  1. try {   
  2.     ImageView imageView = new ImageView(this);   
  3.     InputStream inputStream = super.getAssets().open("home.png");   
  4.     imageView.setImageDrawable(Drawable.createFromStream(inputStream, "tackpic"));   
  5.     this.imageLayout.addView(imageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,   
  6.             LinearLayout.LayoutParams.WRAP_CONTENT));   
  7. catch (IOException e) {   
  8.     e.printStackTrace();   
  9. }  
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的图片。

 

Xml代码 复制代码  收藏代码
  1. <ImageButton android:layout_width="wrap_content"    
  2.              android:layout_height="wrap_content"  
  3.              android:src="@drawable/but_01"/>  
 

 

 

动态声明ImageView,设置src。

 

Java代码 复制代码  收藏代码
  1. try {   
  2.     ImageButton imageButton = new ImageButton(this);   
  3.     InputStream inputStream = super.getAssets().open("but_02.png");   
  4.     imageButton.setImageDrawable(Drawable.createFromStream(inputStream, "tackpic"));   
  5.     this.imageLayout.addView(imageButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,   
  6.             LinearLayout.LayoutParams.WRAP_CONTENT));   
  7. catch (IOException e) {   
  8.     e.printStackTrace();   
  9. }  
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代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.                 android:layout_width="fill_parent"    
  4.                 android:layout_height="fill_parent">  
  5.                    
  6.     <ImageSwitcher android:id="@+id/switcher"  
  7.                    android:layout_width="fill_parent"    
  8.                    android:layout_height="fill_parent"  
  9.                    android:layout_alignParentTop="true"    
  10.                    android:layout_alignParentLeft="true" />  
  11.   
  12.     <Gallery android:id="@+id/gallery"    
  13.              android:background="#55000000"  
  14.              android:layout_width="fill_parent"    
  15.              android:layout_height="60dp"  
  16.              android:layout_alignParentBottom="true"  
  17.              android:layout_alignParentLeft="true"    
  18.              android:gravity="center_vertical"  
  19.              android:spacing="16dp" />  
  20.   
  21. </RelativeLayout>  
 

 

 

SwitcherActivity类。

 

Java代码 复制代码  收藏代码
  1. public class SwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory {   
  2.   
  3.     private ImageSwitcher imageSwitcher;   
  4.     private Gallery gallery;   
  5.   
  6.     private ArrayList<String> imageAssetPathList = new ArrayList<String>();   
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {   
  10.         super.onCreate(savedInstanceState);   
  11.         super.setContentView(R.layout.switcher);   
  12.         this.imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);   
  13.         this.gallery = (Gallery) findViewById(R.id.gallery);   
  14.   
  15.         for (int i = 1; i <= 20; i++) {   
  16.             this.imageAssetPathList.add("images/" + i + ".jpg");   
  17.         }   
  18.   
  19.         this.imageSwitcher.setFactory(this);   
  20.         this.gallery.setAdapter(new ImageAdapter(thisthis.imageAssetPathList));   
  21.         this.gallery.setOnItemSelectedListener(this);   
  22.   
  23.     }   
  24.   
  25.     @Override  
  26.     public View makeView() {   
  27.         ImageView imageView = new ImageView(this);   
  28.         imageView.setBackgroundColor(0xFF000000);   
  29.         imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);   
  30.         imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));   
  31.         return imageView;   
  32.     }   
  33.   
  34.     @Override  
  35.     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {   
  36.         try {   
  37.             InputStream inputStream = super.getAssets().open(this.imageAssetPathList.get(position));   
  38.             imageSwitcher.setImageDrawable(Drawable.createFromStream(inputStream, "" + position));   
  39.         } catch (IOException e) {   
  40.             e.printStackTrace();   
  41.         }   
  42.     }   
  43.   
  44.     @Override  
  45.     public void onNothingSelected(AdapterView<?> parent) {   
  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类

 

Java代码 复制代码  收藏代码
  1. public class ImageAdapter extends BaseAdapter {   
  2.   
  3.     private Context content;   
  4.     private ArrayList<String> imageAssetPathList;   
  5.   
  6.     public ImageAdapter(Context content, ArrayList<String> imageAssetPathList) {   
  7.         this.content = content;   
  8.         this.imageAssetPathList = imageAssetPathList;   
  9.     }   
  10.   
  11.     @Override  
  12.     public int getCount() {   
  13.         if (this.imageAssetPathList != null) {   
  14.             return this.imageAssetPathList.size();   
  15.         } else {   
  16.             return 0;   
  17.         }   
  18.     }   
  19.   
  20.     @Override  
  21.     public Object getItem(int position) {   
  22.         return null;   
  23.     }   
  24.   
  25.     @Override  
  26.     public long getItemId(int position) {   
  27.         return 0;   
  28.     }   
  29.   
  30.     @Override  
  31.     public View getView(int position, View convertView, ViewGroup parent) {   
  32.         try {   
  33.             ImageView imageView;   
  34.             imageView = new ImageView(this.content);   
  35.             imageView.setAdjustViewBounds(true);   
  36.             imageView.setScaleType(ImageView.ScaleType.FIT_XY);   
  37.             imageView.setPadding(0000);   
  38.   
  39.             InputStream inputStream = this.content.getAssets().open(this.imageAssetPathList.get(position));   
  40.             imageView.setImageDrawable(Drawable.createFromStream(inputStream, "" + position));   
  41.   
  42.             return imageView;   
  43.         } catch (IOException e) {   
  44.             e.printStackTrace();   
  45.             return null;   
  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;
		}
	}

}
 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值