android开发实现换头像功能教程
程序员文章站
2022-10-02 15:23:20
android开发实现换头像功能教程。因为想让图片圆形显示 所以用了 SimpleDrawView 也可以换成ImageView
具体代码如下:
private Simpl...
android开发实现换头像功能教程。因为想让图片圆形显示 所以用了 SimpleDrawView 也可以换成ImageView
具体代码如下:
private SimpleDraweeView mSdvs; private Bitmap head;// 头像Bitmap private static String path = "/sdcard/myHead/";// sd路径 @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.loginfragment, null); initView(view); return view; } private void initView(View view) { mLogin = (TextView) view.findViewById(R.id.login); mLogin.setOnClickListener(this); mSdvs = (SimpleDraweeView) view.findViewById(R.id.sdvs); mSdvs.setOnClickListener(this); Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg");// 从SD卡中找头像,转换成Bitmap if (bt != null) { @SuppressWarnings("deprecation") Drawable drawable = new BitmapDrawable(bt);// 转换成drawable mSdvs.setImageDrawable(drawable); } else { /** * 如果SD里面没有则需要从服务器取头像,取回来的头像再保存在SD中 * */ } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.login: //跳转到登陆页面 Intent intent = new Intent(getActivity(), MainActivity.class); getActivity().startActivity(intent); break; case R.id.sdvs: showTypeDialog();//更换头像 break; } } private void showTypeDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final AlertDialog dialog = builder.create(); View view = View.inflate(getActivity(), R.layout.dialog_select_photo, null); TextView tv_select_gallery = (TextView) view.findViewById(R.id.tv_select_gallery); TextView tv_select_camera = (TextView) view.findViewById(R.id.tv_select_camera); tv_select_gallery.setOnClickListener(new View.OnClickListener() {// 在相册中选取 @Override public void onClick(View v) { Intent intent1 = new Intent(Intent.ACTION_PICK, null); intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent1, 1); dialog.dismiss(); } }); tv_select_camera.setOnClickListener(new View.OnClickListener() {// 调用照相机 @Override public void onClick(View v) { Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "head.jpg"))); startActivityForResult(intent2, 2);// 采用ForResult打开 dialog.dismiss(); } }); dialog.setView(view); dialog.show(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (resultCode ==RESULT_OK) { cropPhoto(data.getData());// 裁剪图片 } break; case 2: if (resultCode == RESULT_OK) { File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg"); cropPhoto(Uri.fromFile(temp));// 裁剪图片 } break; case 3: if (data != null) { Bundle extras = data.getExtras(); head = extras.getParcelable("data"); if (head != null) { /** * 上传服务器代码 */ setPicToView(head);// 保存在SD卡中 mSdvs.setImageBitmap(head);// 用ImageView显示出来 } } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } /** * 调用系统的裁剪功能 * * @param uri */ public void cropPhoto(Uri 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, 3); } private void setPicToView(Bitmap mBitmap) { String sdStatus = Environment.getExternalStorageState(); if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用 return; } FileOutputStream b = null; File file = new File(path); file.mkdirs();// 创建文件夹 String fileName = path + "head.jpg";// 图片名字 try { b = new FileOutputStream(fileName); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件 } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { // 关闭流 b.flush(); b.close(); } catch (IOException e) { e.printStackTrace(); } } }