图5.3.1ImageView
android.widget.ImageView图片控件,继承自android.view.View,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
动态声明ImageView,设置src。
图5.3.2ImageButton
android.widget.ImageButton图片控件,继承自android.widget.ImageView,在android.widget包中。
最简单的使用方法。src设置图片路径,可引用drawable的图片。
动态声明ImageView,设置src。
图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
转载编辑: Fgamers 转载地址:http://disanji.net/2010/12/25/android-desktop-view-4-image/