`
mmdev
  • 浏览: 12944986 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

【android中级】之Android ImageSwitcher和Gallery 综合使用

 
阅读更多

做相册和带图片查看功能的应用都应该用得上

1.MainActivity 单击按钮时,跳转到 ImageShowActivity

Java代码 复制代码 收藏代码
  1. package com.small.photos;
  2. import com.small.photos.R;
  3. import android.widget.*;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. public class MainActivityextends Activity {
  10. OnClickListener listener0 = null;
  11. Button button0;
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.main);
  16. // 单击按钮跳转到ImageShowActivity
  17. listener0 = new OnClickListener() {
  18. public void onClick(View v) {
  19. Intent intent = new Intent(MainActivity.this,
  20. ImageShowActivity.class);
  21. startActivity(intent);
  22. }
  23. };
  24. button0 = (Button) findViewById(R.id.image_show_button);
  25. button0.setOnClickListener(listener0);
  26. }
  27. }

2. main.xml 定义入口按钮

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button android:id="@+id/image_show_button"
  8. android:text="ImageSwitcher Gallery"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content">
  11. </Button>
  12. </LinearLayout>

3.image_show.xml

ImageSwitcher是用来图片显示那块区域的控件 Gallery 是来控制底下那个图标列表索引用的

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <ImageSwitcher android:id="@+id/ImageSwitcher01"
  7. android:layout_height="fill_parent"
  8. android:layout_width="fill_parent"
  9. android:layout_alignParentTop="true"
  10. android:layout_alignParentLeft="true">
  11. </ImageSwitcher>
  12. <Gallery
  13. android:id="@+id/gallery"
  14. android:background="#55000000"
  15. android:layout_width="fill_parent"
  16. android:layout_height="60dp"
  17. android:layout_alignParentBottom="true"
  18. android:layout_alignParentLeft="true"
  19. android:gravity="center_vertical"
  20. android:spacing="16dp" />
  21. </RelativeLayout>

4.ImageShowActivity

R.drawable.sample_thumb_0 为图片的标识

图片放在res/drawable/目录下 图片名称为sample_thumb_0.gif

Java代码 复制代码 收藏代码
  1. package com.small.photos;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.view.Window;
  8. import android.view.animation.AnimationUtils;
  9. import android.widget.AdapterView;
  10. import android.widget.BaseAdapter;
  11. import android.widget.Gallery;
  12. import android.widget.ImageSwitcher;
  13. import android.widget.ImageView;
  14. import android.widget.AdapterView.OnItemSelectedListener;
  15. import android.widget.RelativeLayout.LayoutParams;
  16. import android.widget.ViewSwitcher.ViewFactory;
  17. public class ImageShowActivityextends Activityimplements ViewFactory,
  18. OnItemSelectedListener {
  19. /** Called when the activity is first created. */
  20. ImageSwitcher mSwitcher;
  21. private Integer[] mThumbIds = { R.drawable.sample_thumb_0,
  22. R.drawable.sample_thumb_1, R.drawable.sample_0, R.drawable.sample_1 };
  23. private Integer[] mImageIds = { R.drawable.sample_thumb_0,
  24. R.drawable.sample_thumb_1, R.drawable.sample_0, R.drawable.sample_1 };
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. requestWindowFeature(Window.FEATURE_NO_TITLE);
  29. setContentView(R.layout.image_show);
  30. setTitle("ImageShowActivity");
  31. mSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher01);
  32. // 系统的anim中的fade_in.xml
  33. mSwitcher.setFactory(this);
  34. mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
  35. android.R.anim.fade_in));
  36. mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
  37. android.R.anim.fade_out));
  38. Gallery g = (Gallery) findViewById(R.id.gallery);
  39. // 为缩略图浏览器指定一个适配器
  40. g.setAdapter(new ImageAdapter(this));
  41. // 响应 在缩略图列表上选中某个缩略图后的 事件
  42. g.setOnItemSelectedListener(this);
  43. }
  44. @SuppressWarnings("unchecked")
  45. public void onItemSelected(AdapterView parent, View v,int position,long id) {
  46. mSwitcher.setImageResource(mImageIds[position]);
  47. }
  48. @SuppressWarnings("unchecked")
  49. public void onNothingSelected(AdapterView parent) {
  50. }
  51. @Override
  52. public View makeView() {
  53. ImageView i = new ImageView(this);
  54. i.setBackgroundColor(0xFF000000);
  55. i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  56. i.setLayoutParams(new ImageSwitcher.LayoutParams(
  57. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
  58. return i;
  59. }
  60. public class ImageAdapterextends BaseAdapter {
  61. private Context mContext;
  62. public ImageAdapter(Context c) {
  63. mContext = c;
  64. }
  65. public int getCount() {
  66. return mThumbIds.length;
  67. }
  68. public Object getItem(int position) {
  69. return position;
  70. }
  71. public long getItemId(int position) {
  72. return position;
  73. }
  74. //getView方法动态生成一个ImageView,然后利用setLayoutParams、setImageResource、
  75. //setBackgroundResource分别设定图片大小、图片源文件和图片背景。当图片被显示到当前
  76. //屏幕的时候,这个函数就会被自动回调来提供要显示的ImageView
  77. public View getView(int position, View convertView, ViewGroup parent) {
  78. ImageView i = new ImageView(mContext);
  79. i.setImageResource(mThumbIds[position]);
  80. i.setAdjustViewBounds(true);
  81. i.setLayoutParams(new Gallery.LayoutParams(
  82. LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  83. i.setBackgroundResource(R.drawable.picture_frame);
  84. return i;
  85. }
  86. }
  87. }

5.AndroidManifest.xml 标识MainActivity为一个程序的开始

Java代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.small.photos"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <application android:label="@string/app_name">
  7. <activity android:name=".ImageShowActivity"
  8. android:label="@string/app_name">
  9. </activity>
  10. <activity android:name="MainActivity"><intent-filter><action android:name="android.intent.action.MAIN"></action>
  11. <category android:name="android.intent.category.LAUNCHER"></category>
  12. </intent-filter>
  13. </activity>
  14. </application>
  15. </manifest>

基本上就是这些了,然后启动吧!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics