android选择头像弹窗,Android App开发常用功能之用户头像选择-Go语言中文社区
前言
现在的APP基本都有个人资料的填写,基本的都有头像的选择,支持拍照和从本地相册选择,剪切圆形头像的功能,现在用个小demo实现以下。
下面看一下效果图
上代码:
主界面代码
package com.example.androidpersonal_icon;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
public class MainActivity extends Activity {
protected static final int CHOOSE_PICTURE = 0;
protected static final int TAKE_PICTURE = 1;
private static final int CROP_SMALL_PICTURE = 2;
protected static Uri tempUri;
private ImageView iv_personal_icon;
private SelectPicPopupWindow menuWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 新建一个用来存储照片的文件夹
File destDir = new File(Environment.getExternalStorageDirectory() + "/AndroidPersonal_icon");
if (!destDir.exists()) {
destDir.mkdirs();
}
iv_personal_icon = (ImageView) findViewById(R.id.iv_personal_icon);
iv_personal_icon.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 实例化SelectPicPopupWindow
menuWindow = new SelectPicPopupWindow(MainActivity.this, itemsOnClick);
// 显示窗口
menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.main),
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置
}
});
// 读取上一次剪切的照片
if (destDir.exists() && destDir.isDirectory()) {
if (destDir.list().length > 0) {
Log.d("111111111112", destDir.toString() + "/image_icon.png");
Bitmap bitmap = BitmapFactory.decodeFile(destDir.toString() + "/image_icon.png");
iv_personal_icon.setImageBitmap(bitmap);
} else {
iv_personal_icon.setBackgroundResource(R.drawable.default_personal_image);
}
}
}
// 为弹出窗口实现监听类
private OnClickListener itemsOnClick = new OnClickListener() {
public void onClick(View v) {
menuWindow.dismiss();
switch (v.getId()) {
case R.id.Layout_take_photo:
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
tempUri = Uri.fromFile(
new File(Environment.getExternalStorageDirectory() + "/AndroidPersonal_icon", "image.jpg"));
Log.d("11111111", tempUri.toString());
// 指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
break;
case R.id.Layout_pick_photo:
Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT);
openAlbumIntent.setType("image/*");
startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
break;
default:
break;
}
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) { // 如果返回码是可以用的
switch (requestCode) {
case TAKE_PICTURE:
startPhotoZoom(tempUri); // 开始对图片进行裁剪处理
break;
case CHOOSE_PICTURE:
startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理
break;
case CROP_SMALL_PICTURE:
if (data != null) {
setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
}
break;
}
}
}
/**
* 裁剪图片方法实现
*
* @param uri
*/
protected void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
tempUri = uri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}
/**
* 保存裁剪之后的图片数据
*
* @param
*
* @param picdata
*/
protected void setImageToView(Intent data) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
photo = Utils.toRoundBitmap(photo, tempUri); // 这个时候的图片已经被处理成圆形的了
iv_personal_icon.setImageBitmap(photo);
uploadPic(photo);
}
}
private void uploadPic(Bitmap bitmap) {
// 上传至服务器
// ... 可以在这里把Bitmap转换成file,然后得到file的url,做文件上传操作
// 注意这里得到的图片已经是圆形图片了
// bitmap是没有做个圆形处理的,但已经被裁剪了
String imagePath = Utils.savePhoto(bitmap,
Environment.getExternalStorageDirectory().getAbsolutePath() + "/AndroidPersonal_icon", "image_icon");
Log.d("imagePath", imagePath + "");
if (imagePath != null) {
// 拿着imagePath上传了
// ...
}
}
}
圆形头像剪切代码:
package com.example.androidpersonal_icon;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.util.Log;
public class Utils {
/**
* Save image to the SD card
*
* @param photoBitmap
* @param photoName
* @param path
*/
public static String savePhoto(Bitmap photoBitmap, String path,
String photoName) {
String localPath = null;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File photoFile = new File(path, photoName + ".png");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100,
fileOutputStream)) { // 转换完成
localPath = photoFile.getPath();
fileOutputStream.flush();
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
fileOutputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return localPath;
}
/**
* 转换图片成圆形
*
* @param bitmap
* 传入Bitmap对象
* @param tempUri
* @return
*/
public static Bitmap toRoundBitmap(Bitmap bitmap, Uri tempUri) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float roundPx;
float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
if (width <= height) {
roundPx = width / 2;
left = 0;
top = 0;
right = width;
bottom = width;
height = width;
dst_left = 0;
dst_top = 0;
dst_right = width;
dst_bottom = width;
} else {
roundPx = height / 2;
float clip = (width - height) / 2;
left = clip;
right = width - clip;
top = 0;
bottom = height;
width = height;
dst_left = 0;
dst_top = 0;
dst_right = height;
dst_bottom = height;
}
Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect src = new Rect((int) left, (int) top, (int) right,
(int) bottom);
final Rect dst = new Rect((int) dst_left, (int) dst_top,
(int) dst_right, (int) dst_bottom);
final RectF rectF = new RectF(dst);
paint.setAntiAlias(true);// 设置画笔无锯齿
canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
paint.setColor(color);
// 以下有两种方法画圆,drawRounRect和drawCircle
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);//
// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
canvas.drawCircle(roundPx, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452
canvas.drawBitmap(bitmap, src, dst, paint); // 以Mode.SRC_IN模式合并bitmap和已经draw了的Circle
return output;
}
}
ok,大功告成,最后别忘了在清单文件中添加读写sd可权限,不然得不到imagePath
android选择头像弹窗,Android App开发常用功能之用户头像选择-Go语言中文社区相关推荐
- Android App开发常用功能之用户头像选择
前言 现在的APP基本都有个人资料的填写,基本的都有头像的选择,支持拍照和从本地相册选择,剪切圆形头像的功能,现在用个小demo实现以下. 下面看一下效果图 上代码: 主界面代码 package co ...
- android广告页白屏_Android 启动页面与广告页面的实现-Go语言中文社区
在我们APP的开发过程中,启动页面是绕不开的,广告页面说不定,但是不得不说,这两个界面都是经常要用到的.接下来我记录一下我的实现过程.项目架构为MVP. 那么先看看我们的需求和流程:(当然这也是可以根 ...
- windows远程android传输文件,电脑(Linux/Windows)使用SSH远程登录安卓(Android)手机实现无线传输和管理文件(图文详解)-Go语言中文社区...
电脑(Linux/Windows系统)使用SSH远程登录安卓(Android)手机实现无线传输和管理文件(图文详解) 温馨提示 本文只针对安卓(Android)手机!iPhone或者WP的手机用户,请 ...
- Android app开发常用图标网站
Android app开发常用图标网站 Android app开发小白的开发android app时需要使用各种图标,下面介绍一些常用的Android app图标的网站,可免费下载图标使用,希望对大家 ...
- Android开发常用功能 https://www.2cto.com/kf/201611/561847.html
Android开发常用功能 2016-11-03 09:43:58 来源:EvanJames的专栏 收藏 我要投稿 1.定时器的实现 (1)采用Handler的postDelay ...
- Android课程设计本地游戏厅app开发(已开源)
Android课程设计本地游戏厅app开发(已开源) 见链接
- 智慧公交app开发的功能主要有哪些
公交出行市场拥有庞大的用户基数,根据相关数据统计得出,我国每天的公交出行的人数高达2.8亿.因此我们可以看出公交出行市场拥有庞大的挖掘潜力,也就是说,智慧公交App开发市场同样潜力巨大.而智慧公交Ap ...
- 二手交易APP开发主要功能有哪些?(二)
二手交易APP开发主要功能有哪些?(二) 在线服务:二手交易手机应用软件.小程序的在线服务功能,为买家与卖家提供互相咨询了解与解答的途径,促进买卖家之间交流互动. 在线预约:通过在线预约功能,提供卖家 ...
- 二手交易APP开发主要功能有哪些?(一)
二手交易APP开发主要功能有哪些?随着我们生活水平的提高以及智能手机的普及,把二手交易与互联网有效结合已成为二手市场新的机会,比如二手车.生活闲置物交易.家电换购.旧物换钱等等.但有很多企业商家有这个 ...
最新文章
- 高并发:RocketMQ 削峰实战
- 2-5-PerformingMountsUnmounts
- 在Fabric ChainCode中导入第三方包(以状态机为例)
- 使用jenkins自动化打包部署Vue项目。详细教程。
- 解决MongoDB 日志文件过大,清理后还占用很大磁盘空间的问题
- 【Linux高级驱动】如何分析并移植网卡驱动
- 通过一个实际例子学习SAP UI5的控件绘制和渲染
- 3年前的一个小项目经验,分享给菜鸟兄弟们(公文收发小软件:前期需求调查部分)...
- linux中权限减少,Linux中权限管理
- python等待10秒_Python交互性编程案例干货分享~| 数字猜谜 | 倒数日App | “拍10秒”测反应速度...
- COSCon'19 开源治理论坛——技术之外,那些更重要的事
- Java开源框架 iBase4J 搭建笔记
- 触摸精灵之keepScreen
- 开篇记(好记性不如烂笔头)
- 批处理切换host文件
- Unity_ClickToShow_FadeInAndOut
- 【宏基因组学】微生物宏基因组学论文摘要集锦
- 猿创征文|国产数据库[StarRocks]实战学习
- Druid以及监控界面的使用
- 「1.8W字」2020不可多得的 TS 学习指南
热门文章
- ubtunu打开firefox_如何在Firefox(在Lubuntu中)中打开“apt”链接?
- java怎么引入html文件路径_如何在public_html中读取文件但在域外?使用相对路径...
- html5语义化 兼容,HTML5语义化标签,兼容性问题
- js 判断数据是否为空
- python多线程编程(8):线程的合并和后台线程
- std::map的insert和下标[]访问
- 文件系统ext3的文件大小限制
- SourceInsight配置
- [Linux] undefined reference to `itoa'
- [Redux/Mobx] 在React中你是怎么对异步方案进行选型的?