public class MainActivity extends AppCompatActivity {
@BindView(R.id.main_image)
ImageView mainImage;
@BindView(R.id.main_text)
TextView mainText;
private SharedPreferences sp;
private SharedPreferences.Editor edit;
private Button but1;
private Button but2;
private PopupWindow popupWindow;
private MyUtil instance = MyUtil.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
sp = getSharedPreferences("sp", Context.MODE_PRIVATE);
edit = sp.edit();
String headPath = sp.getString("headPath", null);
mainImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.pop_layout, null);
popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popupWindow.setOutsideTouchable(true);
View inflate = LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
popupWindow.showAtLocation(inflate, Gravity.BOTTOM, 0, 0);
but1 = view.findViewById(R.id.but1);
but2 = view.findViewById(R.id.but2);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(intent, 1000);
popupWindow.dismiss();
}
});
but2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "heads.jpg")));
startActivityForResult(intent, 2000);
popupWindow.dismiss();
}
});
}
});
}
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", 127);
intent.putExtra("outputY", 127);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3000);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case 1000:
cropPhoto(data.getData());
break;
case 2000:
String s2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "heads.jpg";
File file = new File(Environment.getExternalStorageDirectory() + "/heads.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(s2);
Glide.with(MainActivity.this).load(bitmap).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mainImage);
cropPhoto(Uri.fromFile(file));
loadUrl(new File(s2));
break;
case 3000:
if (data != null) {
Bundle extras = data.getExtras();
Bitmap heads = extras.getParcelable("data");
Glide.with(MainActivity.this).load(heads).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mainImage);
//setPicToView(head);
String s = Environment.getExternalStorageDirectory().getAbsolutePath() + "/heads.jpg";
loadUrl(new File(s));
}
break;
}
}
private void loadUrl(File file) {
instance.showinfo(file, new MyUtil.UtilBack() {
@Override
public void Success(String json) {
Log.i("tag1", json + "2");
Gson gson = new Gson();
MyBean myBean = gson.fromJson(json, MyBean.class);
String headPath = myBean.getHeadPath();
edit.putString("headPath", headPath);
edit.commit();
mainText.setText(headPath);
}
@Override
public void Fail(String json) {
}
});
}
}