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); }
}