Fragment_popupwindow_相机/相册_切换图片

package com.example.yuekao_lianxi;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;

public class FragmentTwo extends Fragment{

private Button but_huan;
private ImageView iv_huan;
private String path = Environment.getExternalStorageDirectory()+"/image.png";

@Override
public View onCreateView(LayoutInflater inflater,
		@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	
	View view = inflater.inflate(R.layout.fragmenttwo, container, false);
	
	but_huan = (Button) view.findViewById(R.id.but_huan);
	iv_huan = (ImageView) view.findViewById(R.id.iv_huan);
	
	return view;
	
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onActivityCreated(savedInstanceState);
	
	
	but_huan.setOnClickListener(new OnClickListener() {
		
		private PopupWindow popupWindow;

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			
			View inflate = View.inflate(getActivity(), R.layout.pop_view, null);
			Button but_camera = (Button) inflate.findViewById(R.id.but_camera);
			Button but_pick = (Button) inflate.findViewById(R.id.but_pick);
			Button but_qx = (Button) inflate.findViewById(R.id.but_qx);
			
			popupWindow = new PopupWindow(inflate,ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
			popupWindow.setOutsideTouchable(true);
			popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			popupWindow.showAsDropDown(but_huan, 0, 0);
			//相机
			but_camera.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
					startActivityForResult(intent, 1000);
					
					
				}
			});
			//相册
			but_pick.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					Intent intent = new Intent(Intent.ACTION_PICK);
					intent.setType("image/*");
					startActivityForResult(intent, 3000);
					
				}
			});
			//取消
			but_qx.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					popupWindow.dismiss();
					
				}
			});
			
		}
	});
	
	
	
	
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
	// TODO Auto-generated method stub
	super.onActivityResult(requestCode, resultCode, data);
	
	if(requestCode==1000&&resultCode==getActivity().RESULT_OK){
		
		Uri fromFile = Uri.fromFile(new File(path));
		crop(fromFile);
		
	}
	
	if(requestCode==2000&&resultCode==getActivity().RESULT_OK){
		Bitmap bitmap = data.getParcelableExtra("data");
		iv_huan.setImageBitmap(bitmap);
	}
	
	
	if(requestCode==3000&&resultCode==getActivity().RESULT_OK){
		
		Uri data2 = data.getData();
		crop(data2);
	}
	
	
}

public void crop(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", 250);
	intent.putExtra("outputY", 250);
	intent.putExtra("return-data", true);
	startActivityForResult(intent, 2000);
	
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值