Android实现Gallery相册组件开发

本文详细描述了如何在AndroidAddContactActivity中使用Gallery和ImageSwitcher控件,以及如何设置图片资源和用户界面交互。作者还强调了系统化学习的重要性,鼓励IT行业人员加入学习社区共同成长。
摘要由CSDN通过智能技术生成

android:layout_width=“fill_parent”

android:layout\_height\="fill\_parent">

<Gallery android:id="@+id/img\_gallery" 

android:layout_width=“fill_parent”

android:layout_height=“110px” android:layout_marginTop=“2px”

    android:layout\_alignParentLeft\="true"></Gallery>

<ImageSwitcher

android:id=“@+id/img_switcher”

    android:layout\_width\="90px" android:layout\_height="90px"

    android:layout\_centerHorizontal\="true" android:layout\_below="@+id/img\_gallery"

    ></ImageSwitcher>



C:String文件



 



<?xml version="1.0" encoding="utf-8"?>

请选择图像




二、设置图片资源



在drawable-hdpi文件夹下放置10个要显示的图片,如下图



![](http://pic002.cnblogs.com/images/2012/68062/2012072611061932.jpg)



三、主Activity类



package cn.moon.contact;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.DialogInterface.OnClickListener;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.ViewGroup.LayoutParams;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.BaseAdapter;

import android.widget.Gallery;

import android.widget.ImageButton;

import android.widget.ImageSwitcher;

import android.widget.ImageView;

import android.widget.ImageView.ScaleType;

import android.widget.ViewSwitcher.ViewFactory;

publicclass AddContactActivity extends Activity {

ImageButton ibtnImgChoose;

AlertDialog imgChooseDialog;

Gallery gallery;

ImageSwitcher imageSwitcher;

int selectedImage;



privateint\[\] images \= { R.drawable.png0001, R.drawable.png0002,

        R.drawable.png0003, R.drawable.png0004, R.drawable.png0005,

        R.drawable.png0006, R.drawable.png0007, R.drawable.png0008,

        R.drawable.png0009, R.drawable.png0010, 

};

publicvoid onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

setContentView(R.layout.addcontact);

System.out.println(“AddContact”);

ibtnImgChoose = (ImageButton) findViewById(R.id.ibtnHead);

ibtnImgChoose.setOnClickListener(new View.OnClickListener() {

        publicvoid onClick(View v) {

initImageChoose();

            imgChooseDialog.show();

}

    });

}



privatevoid initImageChoose() {

LayoutInflater layoutInflater = LayoutInflater.from(this);

    View view \= layoutInflater.inflate(R.layout.userheadchoose, null);

    gallery \= (Gallery) view.findViewById(R.id.img\_gallery);

    gallery.setAdapter(new imageAdapter(this));

gallery.setSelection(images.length /2);

imageSwitcher = (ImageSwitcher) view.findViewById(R.id.img_switcher);

imageSwitcher.setFactory(new switcherFactory(this));

gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

        publicvoid onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {

imageSwitcher.setImageResource(images[arg2]);

            selectedImage \= arg2;

}

publicvoid onNothingSelected(AdapterView<?> arg0) {

            // TODO Auto-generated method stub

        }

    });

AlertDialog.Builder builder =new AlertDialog.Builder(this);

  builder.setTitle("请选择图像");

builder.setPositiveButton(“确认”, new OnClickListener() {

publicvoid onClick(DialogInterface dialog, int which) {

ibtnImgChoose.setImageResource(images[selectedImage]);

        }

});

    builder.setNegativeButton("取消", new OnClickListener() {



        publicvoid onClick(DialogInterface dialog, 

int which) {

}

    });

    builder.setView(view);

    imgChooseDialog \= builder.create();

}

class imageAdapter extends BaseAdapter {

private Context context;

    public imageAdapter(Context context) {

        super();

        this.context = context;

最后

目前已经更新的部分资料:



网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

…(img-JhdqnO3C-1714401203738)]
[外链图片转存中…(img-vg9pG6gC-1714401203739)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值