6.1.6 Gallery结合案例详解

news/2024/7/7 15:21:38 标签: android, encoding, layout, integer, class, object
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views"> Gallery(相册)控件是个很不错的图片查看控件,屏幕中有一个图片列表,Gallery类的继承关系如下:
java.lang.Object
   ↳ class="tags" href="/tags/ANDROID.html" title=android>android.view.View
     ↳ class="tags" href="/tags/ANDROID.html" title=android>android.view.ViewGroup
       ↳ class="tags" href="/tags/ANDROID.html" title=android>android.widget.AdapterView<T extends class="tags" href="/tags/ANDROID.html" title=android>android.widget.Adapter>
         ↳ class="tags" href="/tags/ANDROID.html" title=android>android.widget.AbsSpinner
           ↳ class="tags" href="/tags/ANDROID.html" title=android>android.widget.Gallery
这个Gallery案例,可以用手滑动Gallery,当用户点击某个图片弹出一个Toast,如6-11图:
 


6-11 Gallery控件使用效果图
程序代码请参考代码清单6-9:
【代码清单6-9】 chapter6_5/src/com/work/GalleryActivity.java
public class GalleryActivity extends Activity {
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.class="tags" href="/tags/LAYOUT.html" title=layout>layout.main);

        Gallery g = (Gallery) findViewById(R.id.gallery);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
       
        registerForContextMenu(g);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add(R.string.gallerytext);
    }
   
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Toast.makeText(this, "Longpress: " + info.position, Toast.LENGTH_SHORT).show();
        return true;
    }

    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
       
        public ImageAdapter(Context c) {
            mContext = c;
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
            mGalleryItemBackground = a.getResourceId(
                    R.styleable.Gallery1_class="tags" href="/tags/ANDROID.html" title=android>android_galleryItemBackground, 0);
            a.recycle();
        }

        public int getCount() {
            return mImageIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {       
ImageView imageView;
imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(136, 88));
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
        }

        private Context mContext;

        private Integer[] mImageIds = {
                R.drawable.beijing,
                R.drawable.changsha,
                R.drawable.chengdu,
                R.drawable.chongqing,
                R.drawable.haerbing,
                R.drawable.jinan,
                R.drawable.jiujiang,
                R.drawable.kunming,
                R.drawable.nanjing
        };
    }
}
代码的关键的地方是实现BaseAdapter适配器类——ImageAdapter,其中关键是getView()实现。在convertView为null时候实例化控件,imageView.setLayoutParams(new GridView.LayoutParams(136, 88)是设置一个单元格中图片的大小是136×88像素。imageView.setScaleType(ImageView.ScaleType.FIT_XY) 缩放图片使用FILL方式。imageView.setImageResource(mImageIds[position])为图片控件设置图片。
在布局文件/chapter6_5/res/class="tags" href="/tags/LAYOUT.html" title=layout>layout/main.xml中添加Gallery控件:
<?xml version="1.0" class="tags" href="/tags/ENCODING.html" title=encoding>encoding="utf-8"?>
<Gallery xmlns:class="tags" href="/tags/ANDROID.html" title=android>android="http://schemas.class="tags" href="/tags/ANDROID.html" title=android>android.com/apk/res/class="tags" href="/tags/ANDROID.html" title=android>android" class="tags" href="/tags/ANDROID.html" title=android>android:id="@+id/gallery"
class="tags" href="/tags/ANDROID.html" title=android>android:class="tags" href="/tags/LAYOUT.html" title=layout>layout_width="match_parent"
class="tags" href="/tags/ANDROID.html" title=android>android:class="tags" href="/tags/LAYOUT.html" title=layout>layout_height="wrap_content"
/>
本例中设置图片的背景样式是边框样式,如图6-12。
 


图6-12 图片背景样式
imageView.setBackgroundResource(mGalleryItemBackground)语句就是设定样式的,成员变量mGalleryItemBackground是在ImageAdapter的构造方法中初始化的。
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(
R.styleable.Gallery1_class="tags" href="/tags/ANDROID.html" title=android>android_galleryItemBackground, 0);
a.recycle();
}
mGalleryItemBackground是与galleryItemBackground背景资源绑定的id值,这个id对应的galleryItemBackground属性就是设定带有边框的背景样式。
此外还要在chapter6_5/res/values/目录下面创建一个attrs.xml文件:
<?xml version="1.0" class="tags" href="/tags/ENCODING.html" title=encoding>encoding="utf-8"?>
<resources>
    <declare-styleable name="Gallery1">
        <attr name="class="tags" href="/tags/ANDROID.html" title=android>android:galleryItemBackground" />
    </declare-styleable>
</resources>
这是一个自定义控件属性的xml文件。但是在Android1.0时候没有这么麻烦,而是如下方式实现:
public ImageAdapter(Context c) {
        mContext = c;
        TypedArray a = obtainStyledAttributes(class="tags" href="/tags/ANDROID.html" title=android>android.R.styleable.Theme);
        mGalleryItemBackground = a.getResourceId(
                class="tags" href="/tags/ANDROID.html" title=android>android.R.styleable.Theme_galleryItemBackground, 0);
        a.recycle();
}
class="tags" href="/tags/ANDROID.html" title=android>android.R.styleable.Theme_galleryItemBackground 属性在Android1.0是可以访问的,而在Android1.0之后就不能访问了,而要通过本例的方式获得galleryItemBackground的id值。
                                                                   出自《Android开发案例驱动教程》

http://www.niftyadmin.cn/n/1037136.html

相关文章

java 方法的基本概念及其方法的基本定义和使用

首先 我们需要了解方法是干什么的 比如你早上起床 刷牙 洗脸 这种每天都要做的事程序里也会有 比如 我们需要修改一个用户信息 首先我们要查询这个用户的信息 那么我们就可以把这个查询用户信息的过程 封装成一个方法 发放就是一块独立的代码块进行封装 然后要用到这段代码时直…

F. Kate and imperfection(思维)

题目 题意&#xff1a; 给定一个1…n的集合&#xff0c;要求分别求出长为2,3,4…n的子集&#xff0c;使得每个子集中任意两个数的gcd的最大值尽可能的小。     2≤n≤5⋅1052≤n≤5⋅10^52≤n≤5⋅105 分析&#xff1a; 这题就是一道思维题&#xff0c;经过灵光一闪后发现…

6.4 Android国际化和本地化

何谓国际化和本地化呢&#xff1f;就是在资源文件夹res内建立不同国家语言的文件&#xff0c;这些国家语言的文件命名是有规定的&#xff0c;具体参见表6-1。当用户设置手机的语言时&#xff0c;程序能根据用户选择的语言情况&#xff0c;而加载相对应的语言文件。用户感受到是…

7.2 LinearLayout布局详解

LinearLayout线性布局&#xff0c;线性布局是所有布局中最常用的&#xff0c;它可以让其中的子元素垂直或水平的方式排列&#xff08;通过排列方向的设置&#xff09;。通常复杂的布局都是在LinearLayout布局中嵌套而成的。 下面看一个LinearLayout的例子&#xff0c;这个例子中…

java 方法传递参数,定义返回值

方法可以从调用他的位置传递参数 比如 我们要做两位数的相加 我们可以把相加的两位数当成参数 从外面传给方法 在方法里写将两个数相加的操作 参考代码如下 public class index {public static void main(String args[]) {int naint 1;mint(naint, 100);}public static void …

E. Physical Education Lessons(set区间操作)

题目1 题意&#xff1a; 给定一个n&#xff0c;表示一共有n天的工作日。现在有两个操作&#xff0c;操作1将区间[l,r]全部变为工作日&#xff0c;操作2将区间[l,r]全部变为非工作日。一共有q次操作&#xff0c;要求输出每次操作后的工作日天数。     1≤n≤109,1≤q≤3⋅1…

7.3 RelativeLayout布局详解

RelativeLayout相对布局&#xff0c; 允许子元素指定他们相对于其它元素或父元素的位置&#xff08;通过ID 指定&#xff09;。因此&#xff0c;可以以左右对齐、上下对齐、置于屏幕中央等形式来排列元素。相对布局在实际应用中比较常用。图7-13所示是垂直方向上的应用。图7-13…

java 断点调试 Debug

在开发过程中我们经常会遇到一些奇葩问题 想找原因就需要对程序执行的每一个过程进行观察 那么我们就需要给程序打上断点 例如 我们想给12行代码加上断点&#xff0c;那就点击一下指定位置 也就是对应代码的前面 然后就会如下图 前面出现这个红点 断点就算打好了 然后我们在文件…