package com.xunpige.myapplication.ui; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.PopupWindow; import android.widget.TextView; import com.lidroid.xutils.ViewUtils; import com.lidroid.xutils.view.annotation.ViewInject; import com.xunpige.myapplication.R; import com.xunpige.myapplication.constant.Constant; import com.xunpige.myapplication.utils.SPUtils; import java.io.File; import java.io.FileOutputStream; /** * Created by Administrator on 2015/12/31 0031. */ public class InformationActivity extends Activity { @ViewInject(R.id.tv_title) private TextView tv_title; @ViewInject(R.id.ib_back) private ImageButton ib_back; @ViewInject(R.id.civ_info_head) private de.hdodenhof.circleimageview.CircleImageView civ_info_head; @ViewInject(R.id.parent) private LinearLayout parent; @ViewInject(R.id.btn_save) private Button btn_save; Boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_information); ViewUtils.inject(this); initViews(); } private void initViews() { flag = SPUtils.getBoolean(InformationActivity.this, "selectedHead"); if (flag) { Bitmap bitmap1 = BitmapFactory.decodeFile(Constant.LOCAL_HEAD_IMG_PATH); civ_info_head.setImageBitmap(bitmap1); } tv_title.setText("完善资料"); ib_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(InformationActivity.this, RegisterActivity.class)); } }); civ_info_head.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //选择用户头像 new PopupWindows(InformationActivity.this, parent); } }); btn_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(InformationActivity.this, MainActivity.class)); } }); } public class PopupWindows extends PopupWindow { public PopupWindows(Context mContext, View parent) { super(mContext); View view = View.inflate(mContext, R.layout.item_popupwindows, null); view.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_ins)); LinearLayout ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup); ll_popup.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.push_bottom_in_2)); setWidth(ViewGroup.LayoutParams.FILL_PARENT); setHeight(ViewGroup.LayoutParams.FILL_PARENT); setBackgroundDrawable(new BitmapDrawable()); setFocusable(true); setOutsideTouchable(true); setContentView(view); showAtLocation(parent, Gravity.BOTTOM, 0, 0); update(); Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera); Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo); Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel); // 拍照 bt1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //图片名字 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/xunpige", "headImg.jpg"))); startActivityForResult(intent, 2); dismiss(); } }); // 从相册中选择 bt2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { pickPhoto(); dismiss(); } }); // 取消 bt3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { dismiss(); } }); } } // 选择图片 private void pickPhoto() { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, 1); } // 调用系统内置的裁剪功能 把结果返回去 public void startPhotoZoom(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); } // 结果码的判断 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 1: if (data == null) { return; } startPhotoZoom(data.getData()); break; case 2: File temp = new File(Environment.getExternalStorageDirectory() + "/xunpige/avatar" + "/headImg.jpg"); startPhotoZoom(Uri.fromFile(temp)); break; case 3: // 监听用户的选择 if (data == null) { return; } Bundle extras = data.getExtras(); if (extras == null) { return; } if (data != null) { // 保存图片到指定的文件夹中,跳转到路径页面 不需要记录 在图片编辑页面做这项操作 setPicToView(data); } break; default: break; } super.onActivityResult(requestCode, resultCode, data); } private void setPicToView(Intent data) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap bitmap = extras.getParcelable("data"); // 把图片保存到本地并显示 if (bitmap == null) { return; } Drawable drawable = new BitmapDrawable(null, bitmap); civ_info_head.setImageDrawable(drawable); //把bitmap图片保存到本地 File fileImage = new File(Constant.LOCAL_HEAD_IMG_DIR); if (!fileImage.exists()) { fileImage.mkdirs(); } File imgHead = new File(Constant.LOCAL_HEAD_IMG_PATH); FileOutputStream fos; try { fos = new FileOutputStream(imgHead); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } SPUtils.put(InformationActivity.this, "selectedHead", true); } } }