Android ImageView 点击更换头像
首先搭建布局
主界面布局:
- <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 点击更换头像的更多相关文章
- PHP - 点击更换头像
原理: 操作流程: 首先点击头像图片,弹出选择窗口,选中其中一个则窗口推出头像更换. 效果: 主页面代码: <tr> <td>头像:</td> <td> ...
- Android ImageView点击效果
ImageView设置点击效果需要注意两点,第一个设置android:clickable="true",第二个 <item android:drawable="@d ...
- Android自定义控件实例,圆形头像(图库 + 裁剪+设置),上传头像显示为圆形,附源码
Android项目开发中经常会遇见需要实现圆角或者圆形的图片功能,如果仅仅使用系统自带的ImageView控件显然无法实现此功能,所以通过系列文章的形式由简到繁全方位的介绍一下此功能的实现,巩固一下自 ...
- Android ImageView圆形头像
转载自:http://m.oschina.net/blog/321024 Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用 ...
- 5分钟实现Android中更换头像功能
写在前面:更换头像这个功能在用户界面几乎是100%出现的.通过拍摄照片或者调用图库中的图片,并且进行剪裁,来进行头像的设置.功能相关截图如下: 下面我们直接看看完整代码吧: 1 2 3 4 5 6 7 ...
- 【转】Android ImageView圆形头像
Android ImageView圆形头像 图片完全解析 我们在做项目的时候会用到圆形的图片,比如用户头像,类似QQ.用户在用QQ更换头像的时候,上传的图片都是矩形的,但显示的时候确是圆形的. 原理: ...
- Android图片上传(头像裁切+原图原样)
下面简单铺一下代码: (一)头像裁切.上传服务器(代码) 这里上边的按钮是头像的点击事件,弹出底部的头像选择框,下边的按钮跳到下个页面,进行原图上传. ? 1 2 3 4 5 6 7 8 9 10 1 ...
- Android实现类似换QQ头像功能(图片裁剪)
现在几乎所有的App都有用户登录模块,需要设置用户头像,而关于用户头像部分无疑也是比较头疼的,目前大部分应用的头像部分会有两种方式:一种是利用系统的裁剪功能去获取用户头像,一种就是获取到图片或者照片的 ...
- Android ImageView显示本地图片
Android ImageView 显示本地图片 布局文件 <?xml version="1.0" encoding="utf-8"?> <R ...
随机推荐
- Web字体库下载及转换工具
1.字体现在网站: http://ztxz.org/ 2.Web字体在线格式转换器: http://www.freefontconverter.com/ 3.
- (转)一步一步学习PHP(3)——函数
相信每个人在学习PHP之前至少都有着一定的C语言,或者是C++/Java/C#等其他语言的基础,所以在这里也不从头开始说起,只是来谈谈PHP方法的独特之处. 1. 方法概述 首先,写一个最简单的函数, ...
- struts2与spring整合问题,访问struts2链接时,spring会负责创建Action
每次访问一次链接,spring会创建一个对象,并将链接所带的参数注入到Action的变量中(如何做到的呐) 因为: struts2的action每次访问都重新创建一个对象,那spring的ioc是怎么 ...
- 二、C# 数据类型
C#语言的基本类型包括8种整数类型.2种用于科学计算的二进制浮点类型.1种用于金融计算的十 进制浮点类型.1种布尔类型以及1种字符类型. 2.1 基本数值类型 C#中的基本数据类型都有关键字和它们关联 ...
- 通过Unity依赖注入
前言 Unity容器的思想起始于我在为Web Client Sofitware Factory项目工作的时候,微软的patterns&practices团队已经使用依赖注入的概念好几年了在那时 ...
- DOM中的node与element的区别
先看document的两个常见method. document.createTextNode Constructor: Text document.createElement Constructor: ...
- 你好,C++(40)7.1 一切指针都是纸老虎:彻底理解指针
第7章 C++世界的奇人异事 在武侠小说中,初入武林的毛头小子总是要遇到几位奇人,发生几件异事,经过高人的指点,经历一番磨炼,方能武功精进,从新手成长为高手.在C++世界,同样有诸多的奇人异事.在C+ ...
- uva 1378 A Funny Stone Game (博弈-SG)
题目链接:http://vjudge.net/problem/viewProblem.action?id=41555 把第i堆的每个石子看出一堆个数为n-i的石子,转换为组合游戏 #include & ...
- Js点餐加减数量
<button class="add-on" onclick="chgNum(1,'del')" ><i class="icon-m ...
- eval("表达式")
eval就是把字符串转成可执行代码eval("表达式");表达式被翻译成JavaScript代码执行比如eval("alert('test')");等于aler ...