Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2621698
  • 博文数量: 315
  • 博客积分: 3901
  • 博客等级: 少校
  • 技术积分: 3640
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-08 15:32
个人简介

知乎:https://www.zhihu.com/people/monkey.d.luffy Android高级开发交流群2: 752871516

文章分类

全部博文(315)

文章存档

2019年(2)

2018年(1)

2016年(7)

2015年(32)

2014年(39)

2013年(109)

2012年(81)

2011年(44)

分类: Android平台

2013-10-31 22:23:40

布局:

点击(此处)折叠或打开

  1. <RelativeLayout xmlns:android=""
  2.     xmlns:tools=""
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity" >

  10.     <ImageSwitcher
  11.         android:id="@+id/switcher"
  12.         android:layout_width="fill_parent"
  13.         android:layout_height="fill_parent"
  14.         android:layout_alignParentLeft="true"
  15.         android:layout_alignParentTop="true" >
  16.     </ImageSwitcher>

  17.     <Gallery
  18.         android:id="@+id/gallery"
  19.         android:layout_width="fill_parent"
  20.         android:layout_height="60dp"
  21.         android:layout_alignParentBottom="true"
  22.         android:layout_alignParentLeft="true"
  23.         android:background="#55000000"
  24.         android:gravity="center_vertical"
  25.         android:spacing="60dp" />

  26. </RelativeLayout>
代码:

点击(此处)折叠或打开

  1. package com.example.gallary;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.view.Menu;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.view.ViewGroup.LayoutParams;
  9. import android.view.Window;
  10. import android.view.animation.AnimationUtils;
  11. import android.widget.AdapterView;
  12. import android.widget.AdapterView.OnItemSelectedListener;
  13. import android.widget.BaseAdapter;
  14. import android.widget.Gallery;
  15. import android.widget.ImageSwitcher;
  16. import android.widget.ImageView;
  17. import android.widget.ViewSwitcher.ViewFactory;

  18. public class MainActivity extends Activity
  19. implements OnItemSelectedListener, ViewFactory
  20. {
  21.     private ImageSwitcher mSwitcher;
  22.     private Gallery gll;
  23.     private Integer[] mThumbIds =
  24.     {
  25.             R.drawable.a, R.drawable.b,
  26.             R.drawable.c, R.drawable.d,
  27.             R.drawable.e, R.drawable.f,
  28.             R.drawable.g, R.drawable.h,
  29.             R.drawable.j
  30.     };
  31.     private Integer[] mImageIds =
  32.     {
  33.             R.drawable.a, R.drawable.b,
  34.             R.drawable.c, R.drawable.d,
  35.             R.drawable.e, R.drawable.f,
  36.             R.drawable.g, R.drawable.h,
  37.             R.drawable.j
  38.     };

  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  43.         setContentView(R.layout.activity_main);
  44.         
  45.         ///< 图片切换器【在选中某个缩略图时会触发onItemSelected事件,进行资源切换】
  46.         ///< 真正关键的是makeView,这里实现了View的生成
  47.         mSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
  48.         mSwitcher.setFactory(this);
  49.         mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
  50.         mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
  51.         
  52.         ///< 画廊【ImageAdapter为图片适配器,能设置图片资源,然后返回该资源View】
  53.         ///< ImageAdapter作为适配器,承载了显示缩略图和返回缩略图对应的图片资源ID【getItemId】的功能
  54.         gll = (Gallery)findViewById(R.id.gallery);
  55.         gll.setAdapter(new ImageAdapter(this));
  56.         gll.setOnItemSelectedListener(this);
  57.     }

  58.     @Override
  59.     public boolean onCreateOptionsMenu(Menu menu) {
  60.         // Inflate the menu; this adds items to the action bar if it is present.
  61.         getMenuInflater().inflate(R.menu.main, menu);
  62.         return true;
  63.     }

  64.     @Override
  65.     public View makeView() {
  66.         // TODO Auto-generated method stub
  67.         ImageView iv = new ImageView(this);
  68.         
  69.         iv.setBackgroundColor(0xFF000000);
  70.         
  71.         iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
  72.         
  73.         iv.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
  74.                 LayoutParams.FILL_PARENT));
  75.     
  76.         return iv;
  77.     }

  78.     @Override
  79.     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
  80.             long arg3) {
  81.         // TODO Auto-generated method stub
  82.         mSwitcher.setImageResource(mImageIds[arg2]);
  83.     }

  84.     @Override
  85.     public void onNothingSelected(AdapterView<?> arg0) {
  86.         // TODO Auto-generated method stub

  87.     }
  88.     
  89.     public class ImageAdapter extends BaseAdapter
  90.     {
  91.         public ImageAdapter(Context c)
  92.         {
  93.             mContext = c;
  94.         }
  95.         
  96.         @Override
  97.         public int getCount() {
  98.             // TODO Auto-generated method stub
  99.             return mThumbIds.length;
  100.         }

  101.         @Override
  102.         public Object getItem(int arg0) {
  103.             // TODO Auto-generated method stub
  104.             return arg0;
  105.         }

  106.         @Override
  107.         public long getItemId(int arg0) {
  108.             // TODO Auto-generated method stub
  109.             return arg0;
  110.         }

  111.         @Override
  112.         public View getView(int arg0, View arg1, ViewGroup arg2) {
  113.             // TODO Auto-generated method stub
  114.             ImageView iv = new ImageView(mContext);
  115.             
  116.             iv.setImageResource(mThumbIds[arg0]);
  117.             
  118.             iv.setAdjustViewBounds(true);
  119.             
  120.             iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,
  121.                     LayoutParams.WRAP_CONTENT));
  122.             
  123.             iv.setBackgroundResource(R.drawable.j);
  124.             
  125.             return iv;
  126.         }
  127.         private Context mContext;
  128.     }
  129. }



阅读(5165) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~