首先搭建布局

主界面布局:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"
android:orientation="vertical" > <ImageView
android:id="@+id/image_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/blank" /> <Button
android:id="@+id/select_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/image_btn"
android:layout_centerHorizontal="true"
android:text="选择头像" />
</LinearLayout> <TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ll"
android:stretchColumns="1" > <TableRow> <TextView android:text="用户名:" /> <EditText />
</TableRow> <TableRow> <TextView android:text="密码:" /> <EditText />
</TableRow> <TableRow> <TextView android:text="确认密码:" /> <EditText />
</TableRow> <TableRow> <TextView android:text="E-mail地址:" /> <EditText />
</TableRow>
</TableLayout> </RelativeLayout>

DialogActivity布局

 <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:orientation="horizontal" > <ImageView
android:id="@+id/image_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img01" /> <ImageView
android:id="@+id/image_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img02" /> <ImageView
android:id="@+id/image_03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img03" /> <ImageView
android:id="@+id/image_04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img04" /> <ImageView
android:id="@+id/image_05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img05" /> <ImageView
android:id="@+id/image_06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img06" /> <ImageView
android:id="@+id/image_07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img07" /> <ImageView
android:id="@+id/image_08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img08" /> <ImageView
android:id="@+id/image_09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:src="@drawable/img09" /> </GridLayout>

AndroidManifest.xml中注册活动

  <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DialogActivity"
android:label="选择头像"
android:theme="@android:style/Theme.Dialog"> </activity>
</application>

MainActivity主活动加载布局,点击事件,接收返回的结果

 import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView; public class MainActivity extends LifeCycleActivity { Button select;
ImageView showPic; public static final int SELECT_PIC = 1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); select = (Button) findViewById(R.id.select_btn);
showPic = (ImageView) findViewById(R.id.image_btn); select.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
DialogActivity.class);
startActivityForResult(intent, SELECT_PIC);
}
}); }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_PIC && resultCode == RESULT_OK) {
int imgid = data.getIntExtra("image", -1);
//更换图片
if(imgid!=-1){
showPic.setImageResource(imgid);
}
}
} }

DialogActivity活动加载布局,返回数据

这里使用两种方式,一种是使用数据,一种是使用反射

 import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView; public class DialogActivity extends LifeCycleActivity { ImageView[] iv = new ImageView[9]; // int[] ids = { R.id.image_01, R.id.image_02, R.id.image_03, R.id.image_04,
// R.id.image_05, R.id.image_06, R.id.image_07, R.id.image_08,
// R.id.image_09 };
// int[] imgId = { R.drawable.img01, R.drawable.img02, R.drawable.img03,
// R.drawable.img04, R.drawable.img05, R.drawable.img06,
// R.drawable.img07, R.drawable.img08, R.drawable.img09 }; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog); //使用反射
for (int i = 0; i < iv.length; i++) {
final int finalI = i+1;
String name = "image_0"+finalI;
try {
//获取其他类
Class cls = R.id.class;
//获取类的属性,getField(name)获取属性,getInt(null)获取属性对应的值
//null代表的是静态变量,非静态变量需要传递对象
int id = cls.getField(name).getInt(null);
iv[i] = (ImageView) findViewById(id);
iv[i].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { //返回数据
Intent intent = getIntent();
Class cls2 = R.drawable.class;
try {
//内部类使用外部局部变量,需要final
int imgid2 = cls2.getField("img0"+finalI).getInt(null);
intent.putExtra("image", imgid2);
setResult(RESULT_OK, intent);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}); } catch (Exception e) {
e.printStackTrace();
}
} // for (int i = 0; i < iv.length; i++) {
// final int finalI = i;
// //给每一个ImageView找到id
// iv[i] = (ImageView) findViewById(ids[i]);
// //设置点击事件监听
// iv[i].setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// //返回数据
// Intent intent = getIntent();
// //内部类使用外部局部变量,需要final
// intent.putExtra("image", imgId[finalI]);
// setResult(RESULT_OK, intent);
// finish();
// }
// });
// }
} }

最终效果图

Android ImageView 点击更换头像的更多相关文章

  1. PHP - 点击更换头像

    原理: 操作流程: 首先点击头像图片,弹出选择窗口,选中其中一个则窗口推出头像更换. 效果: 主页面代码: <tr> <td>头像:</td> <td> ...

  2. Android ImageView点击效果

    ImageView设置点击效果需要注意两点,第一个设置android:clickable="true",第二个 <item android:drawable="@d ...

  3. Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码

    Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...

  4. Android ImageView圆形头像

    转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...

  5. 5分钟实现Android中更换头像功能

    写在前面:更换头像这个功能在用户界面几乎是100%出现的.通过拍摄照片或者调用图库中的图片,并且进行剪裁,来进行头像的设置.功能相关截图如下: 下面我们直接看看完整代码吧: 1 2 3 4 5 6 7 ...

  6. 【转】Android ImageView圆形头像

    Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用QQ更换头像的时候,上传的图片都是矩形的,但显示的时候确是圆形的. 原理: ...

  7. Android图片上传(头像裁切+原图原样)

    下面简单铺一下代码: (一)头像裁切.上传服务器(代码) 这里上边的按钮是头像的点击事件,弹出底部的头像选择框,下边的按钮跳到下个页面,进行原图上传. ? 1 2 3 4 5 6 7 8 9 10 1 ...

  8. Android实现类似换QQ头像功能(图片裁剪)

    现在几乎所有的App都有用户登录模块,需要设置用户头像,而关于用户头像部分无疑也是比较头疼的,目前大部分应用的头像部分会有两种方式:一种是利用系统的裁剪功能去获取用户头像,一种就是获取到图片或者照片的 ...

  9. Android ImageView显示本地图片

    Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...

随机推荐

  1. 创建DBLink语句

    --linkName DBLink名 --username 用户名 --password 密码 --tns TNS配置字符串 create database link &linkName co ...

  2. expdp 备份数据库-附带报错信息

    操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户.所属组 [root@Oracle11g ~]# chow ...

  3. 基于ThinkPHP+AJAX的省市区三级联动

    练习,就当练习. 省市区三级联动,样式如下图所示: 1,导入两个js文件并且导入数据库文件. 两个js文件分别是jquery-2.1.4.min.js和jquery-1.js,数据库文件,见附件. 2 ...

  4. EBS与FMW集成工作流管理器的检查

    工作流管理器的检查点(DB层面): --1:数据库job aq参数设置,建议设置job_queue_processes>=10 select p.NAME,p.DESCRIPTION,p.VAL ...

  5. 跟我学android-android常用布局介绍

    在上一章我们曾经谈到,Android平台的界面 是使用XML的方式设计的,然后在上一章我们只做了一个简单的界面,在这章,我们将介绍如何使用常用的控件设计实用的界面. Android中的视图都是继承Vi ...

  6. Oracle的卸载与安装

    今天在做一个CURD的web小应用,为后面使用ExtJS搭建一个后台.因为还没有使用过Oracle数据库,因此今天也特的地的使用oracle数据库作为后台的数据库,也当练习使用oracle. 但是今天 ...

  7. MPICH2在两台Ubuntu上安装

    本文在经过大量的实验终于不负众望成功的在两台Ubuntu 12.04上部署MPI的一个小型集群,MPICH2所用版本为mpich2-1.4.1,下载地址:http://www.mcs.anl.gov/ ...

  8. CMake----if与option使用小记

    在CMake中if语法比较简单,if后面括号中的参数随着CMake版本的推进,在else和endif中也可以不用写了. if(address) else() endif() 对于if语法,比较常用的就 ...

  9. SecureCRT上使用公钥登陆Linux服务器

    SecureCRT部分配置 1.首先生成公钥. 打开SecureCRT(我的版本为7.0,估计其他版本基本相同)程序,点击菜单栏的“工具”->“创建公钥”.按照步骤执行.其中一步比较重要就是选择 ...

  10. MyBatis学习笔记(3)—— 利用mybatis灌入假数据

    由于第三方厂商未能按时提供实时数据,故需要纯手动导入一些实时数据,用于统计分析.正好最近自己学习了mybatis .因此使用mybatis 配置一个select.insert 的简单操作语句,用于灌入 ...