Android头像选择器
2016-12-29 10:12
309 查看
之前的项目中有做过头像上传的功能,这次又有用到,就整理一下。
public class MainActivity extends Activity { private ImageView ivHead; private Bitmap head; private static String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ZKT/"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initDialog(); } private void initView() { ivHead = (ImageView) findViewById(R.id.imageView1); Bitmap bt = BitmapFactory.decodeFile(path + "head.jpg"); if (bt != null) { Drawable drawable = new BitmapDrawable(bt); ivHead.setImageDrawable(drawable); } else { /** *从服务器获取 * */ } } private void initDialog() { final View view = getLayoutInflater().inflate(R.layout.selectdialog, null); final Dialog dialog = new Dialog(MainActivity.this); // 取消dialog的title栏 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); ivHead.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.setContentView(view, new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); Window window = dialog.getWindow(); window.setWindowAnimations(R.style.anim); WindowManager.LayoutParams wl = window.getAttributes(); wl.x = 0; wl.y = getWindowManager().getDefaultDisplay().getHeight(); wl.width = LayoutParams.MATCH_PARENT; wl.height = LayoutParams.WRAP_CONTENT; dialog.onWindowAttributesChanged(wl); dialog.setCanceledOnTouchOutside(true); view.findViewById(R.id.btn_photochoose_cancle).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); view.findViewById(R.id.btn_photochoose_photobox).setOnClickListener(new 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(); } }); view.findViewById(R.id.btn_photochoose_take).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri .fromFile(new < f235 /span>File(Environment .getExternalStorageDirectory(), "head.jpg"))); startActivityForResult(intent2, 2); dialog.dismiss(); } }); dialog.show(); } }); } protected 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) { /** * 上传到服务器 */ saveImage(head); // 设置给imageview ivHead.setImageBitmap(head); } } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } //裁剪图片 public void cropPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 150); intent.putExtra("outputY", 150); intent.putExtra("return-data", true); startActivityForResult(intent, 3); } //保存图片到本地 private void saveImage(Bitmap mBitmap) { String sdStatus = Environment.getExternalStorageState(); if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { return; } FileOutputStream b = null; File file = new File(path); if (!file.exists()) { 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(); } } } }
这是弹出,弹回dialog的动画代码:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="0" android:fromYDelta="1000" android:toXDelta="0" android:toYDelta="0" /> </set>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="300" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="1000" /> </set>
这是头像选择的dialog的布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00000000" android:gravity="bottom" android:orientation="vertical" > <Button android:id="@+id/btn_photochoose_photobox" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:paddingBottom="10dip" android:paddingTop="10dip" android:text="图库" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="0.5dip" android:background="#DAD9DB" /> <Button android:id="@+id/btn_photochoose_take" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:paddingBottom="10dip" android:paddingTop="10dip" android:text="拍照" android:textSize="16sp" /> <TextView android:layout_width="match_parent" android:layout_height="0.5dip" android:background="#DAD9DB" /> <Button android:id="@+id/btn_photochoose_cancle" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:paddingBottom="10dip" android:paddingTop="10dip" android:text="取消" android:textSize="16sp" /> </LinearLayout>
Mainactivity的布局很简单,只有一个ImageView。希望能对初学者有帮助
相关文章推荐
- android开发中注册的时候,从A跳到B选择头像,再返回到A界面,除了B界面新增的数据,A界面原先的信息依然存在
- 学习Android之第七个小程序头像选择(自定义对话框、Gallery、ImageSwitcher)
- Android调用手机相机与手机图库选择用户头像
- android头像选择(拍照,相册,裁剪)
- android 选择头像上传
- android-gallery-头像选择器效果
- Android拍照或选择本地图片并剪裁图片设置成头像
- Android开发之头像上传(包含拍照,从相册选择图片,裁剪等)
- android头像选择(拍照,相册,裁剪)
- Android头像选择(手机和相册)
- android 头像选择以及裁剪
- android从摄像头或者图库选择图片,并进行裁剪,可以用来用户头像处理
- 朝花夕拾-android 从手机选择图片或拍照设置头像
- Cocos2d-x 3.x 头像选择,本地相册图片+图片编辑(Android、IOS双平台)
- Android头像上传/拍照/从相册选择Demo(个人信息模块会用到)
- android开发 通过拍照或选择本地图片并剪裁图片设置成头像
- Android 设置用户头像 -》拍照/相册选择+裁剪 实现
- Android 选择并裁剪头像
- Android开发--通过相册或拍照选择头像
- Android版本微信头像剪裁与系统剪裁之间的选择