package com.example.paizhao;
import java.io.File;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button btn_paizhao;
private Button btn_quxiao;
private Button btn_xiangce;
private ImageView image_view;
private View contentView;
private PopupWindow window;
private View parent;
private TextView text_view;
private String path = Environment.getExternalStorageDirectory()
+ "head.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1.初始化控件
image_view = (ImageView) findViewById(R.id.image_view);
text_view = (TextView) findViewById(R.id.text_view);
// 2.创建popwindow
contentView = View
.inflate(MainActivity.this, R.layout.pop_window, null);
window = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
// 2.1初始化popwindow下的控件需要用contentView.findViewById
btn_paizhao = (Button) contentView.findViewById(R.id.btn_paizhao);
btn_quxiao = (Button) contentView.findViewById(R.id.btn_quxiao);
btn_xiangce = (Button) contentView.findViewById(R.id.btn_xiangce);
// 2.2设置popwindow
window.setTouchable(true);// 可触摸
window.setFocusable(true);// 可聚集焦点
window.setOutsideTouchable(true);// popwindow 外可点击
window.setBackgroundDrawable(new BitmapDrawable());// 背景
// 3.点击事件
text_view.setOnClickListener(this);
btn_paizhao.setOnClickListener(this);
btn_quxiao.setOnClickListener(this);
btn_xiangce.setOnClickListener(this);
}
// 3.1点击时间的方法
public void onClick(View v) {
switch (v.getId()) {
case R.id.text_view:// 点击设置头像字体时触发popwinow
window.showAsDropDown(text_view);
break;
case R.id.btn_paizhao:// 点击拍照
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 创建意图对象
startActivityForResult(intent, 99);
window.dismiss();// 关闭popwindow
break;
case R.id.btn_xiangce:
Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setType("image/*");// 设置类型
startActivityForResult(intent2, 100);
window.dismiss();// 关闭popwindow
break;
case R.id.btn_quxiao:
window.dismiss();//取消直接关闭popwindow
break;
default:
break;
}
}
//4.判断返回的结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// 如果返回99代表是点击的拍照
if (requestCode == 99 && resultCode == RESULT_OK) {
Uri uri = Uri.fromFile(new File(path));
crop(uri);//调用裁剪的方法,要先把剪裁的方法写好
}
//返回100点击相册,调用裁剪的方法
if (requestCode == 100 && resultCode == RESULT_OK) {
crop(data.getData());//调用裁剪的方法
}
//返回101裁剪过后
if (requestCode == 101 && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
image_view.setImageBitmap(bitmap);//剪裁后设置头像
}
}
//4.1裁剪的方法
private void crop(Uri uri) {
//固定的背就行了
Intent intent = new Intent("com.android.camera.action.CROP");
//一个setdataAndType
intent.setDataAndType(uri, "image/*");
//6个putExtra
intent.putExtra("crop", "true");
//这两个差不多背一下单词就行了
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
//这两个差不多背一下单词就行了
intent.putExtra("outputX", 251);
intent.putExtra("outputY", 251);
intent.putExtra("return-data", true);
startActivityForResult(intent, 101);
}
}