1.MainActivity
public class MainActivity extends BaseActivityPresenter<MainActivityPresenter> {
@Override
public Class<MainActivityPresenter> getGate() {
return MainActivityPresenter.class;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getGate.init(requestCode,resultCode,data);
}
}
2.MainActivityPresenter
public void init(int requestCode, int resultCode, Intent data) {
Fragment fragment = list.get(mViewPager.getCurrentItem());
if (fragment instanceof FragmentOne){
((FragmentOne)fragment).onActivity(requestCode,resultCode,data);
}
}
}
3.framentOne
public void onActivity(int requestCode, int resultCode, Intent data) {
getGate.startForResult(requestCode,resultCode,data);
}
4.FramentOnePresenter
public class FragmentOnePresenter extends AppCreateIml implements View.OnClickListener {
private ImageView photo;
private View contextView;
private Button btn_xc;
private Button btn_xj;
private PopupWindow window;
private View parent;
private String path = Environment.getExternalStorageDirectory()
+ "/head.jpg";
@Override
public int getLayoutId() {
return R.layout.fragment_fragment_one;
}
@Override
public void initData() {
//初始化控件
photo=(ImageView)get(R.id.photo);
parent = View.inflate(((MainActivity)context), R.layout.activity_main, null);
//popwindow
contextView = View.inflate(context, R.layout.layout_pop, null);
window = new PopupWindow(contextView, LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
window.setTouchable(true);
window.setFocusable(true);
window.setOutsideTouchable(true);
window.setBackgroundDrawable(new BitmapDrawable());
btn_xc=(Button)contextView.findViewById(R.id.btn_xc);
btn_xj=(Button)contextView.findViewById(R.id.btn_xj);
btn_xj.setOnClickListener(this);
btn_xc.setOnClickListener(this);
photo.setOnClickListener(this);
}
private Context context;
@Override
public void getContext(Context context) {
this.context=context;
}
@Override
public void onClick(View view) {
switch (view.getId()){
//点击图片
case R.id.photo:
window.showAtLocation(parent, Gravity.BOTTOM,0,0);
break;
//点击相册
case R.id.btn_xc:
Intent xcIntent = new Intent(Intent.ACTION_PICK);
xcIntent.setType("image/*");
((MainActivity)context).startActivityForResult(xcIntent, 89);
break;
//点击相机
case R.id.btn_xj:
Intent xjintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
xjintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
((MainActivity)context).startActivityForResult(xjintent, 99);
break;
}
}
//调用相机和相册
public void startForResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 89 && resultCode == RESULT_OK) {
crop(data.getData());
} else if (requestCode == 100 && resultCode == RESULT_OK) {
Bitmap bitmap = data.getParcelableExtra("data");
if (bitmap == null) {
Toast.makeText(context,"哈哈哈",Toast.LENGTH_LONG).show();
}
photo.setImageBitmap(bitmap);
} else if (requestCode == 99 && resultCode == RESULT_OK) {
Uri uri = Uri.fromFile(new File(path));
crop(uri);
}
}
//对相册的裁剪
private void crop(Uri data) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(data, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("output", 251);
intent.putExtra("output", 251);
intent.putExtra("return-data", true);
((MainActivity)context).startActivityForResult(intent, 100);
}
}