package com.example.jingdongdemo.fragment; 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.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; import com.example.jingdongdemo.R; import com.example.jingdongdemo.bean.CommodityBean; import com.example.jingdongdemo.presenter.CommodityPresenterImp; import com.example.jingdongdemo.ui.CommodityView; import com.example.jingdongdemo.ui.activity.LoginActivity; import com.example.jingdongdemo.utils.GlideImageLoader; import com.youth.banner.Banner; import java.io.File; import java.util.ArrayList; import java.util.List; import static android.app.Activity.RESULT_OK; /** * Created by Administrator on 2018/4/17 0017. */ public class MainFragment extends Fragment { private ImageView touxiang; private TextView tv_login_zhuce; private String name; private PopupWindow window; private String path = Environment.getExternalStorageDirectory()+"/1512Q.png"; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.mainfragment, container, false); touxiang = view.findViewById(R.id.touxiang); tv_login_zhuce = view.findViewById(R.id.tv_login_zhuce); String string = tv_login_zhuce.getText().toString(); if (string.equals("登录/注册")){ tv_login_zhuce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(getActivity(), LoginActivity.class); startActivityForResult(intent,1); } }); } touxiang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // tv_login_zhuce.setText("登录/注册"); View inflate = LayoutInflater.from(getActivity()).inflate( R.layout.popup_item, null); // PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); // popupWindow.setContentView(inflate); initpopup(inflate); window = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); // 设置PopupWindow的背景 window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置PopupWindow是否能响应外部点击事件 window.setOutsideTouchable(true); // 设置PopupWindow是否能响应点击事件 window.setTouchable(true); window.showAtLocation(inflate, Gravity.CENTER,0,0); } }); return view; } private void initpopup(View inflate) { TextView xiangji = inflate.findViewById(R.id.xiangji); TextView xiangce = inflate.findViewById(R.id.xiangce); TextView quxiao = inflate.findViewById(R.id.quxiao); xiangce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //调取系统的相册 Intent.ACTION_PICK相册 Intent it = new Intent(Intent.ACTION_PICK); //设置格式 it.setType("image/*"); startActivityForResult(it, 3000); window.dismiss(); } }); xiangji.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //在Sdcard 中创建文件 存入图片 it.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path))); //1.意图 2.requestCode 请求码 startActivityForResult(it, 1000); window.dismiss(); } }); quxiao.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { window.dismiss(); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2){ Bundle b = data.getExtras(); name = b.getString("name"); tv_login_zhuce.setText(name); /* tv_login_zhuce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //tv_login_zhuce.setText("登录/注册"); } });*/ } if(requestCode == 1000 && resultCode ==RESULT_OK){ //调取裁剪功能 om.android.camera.action.CROP 裁剪的Action Intent it = new Intent("com.android.camera.action.CROP"); //得到图片设置类型 it.setDataAndType(Uri.fromFile(new File(path)), "image/*"); //是否支持裁剪 设置 true 支持 false 不支持 it.putExtra("CROP", true); //设置比例大小 1:1 it.putExtra("aspectX", 1); it.putExtra("aspectY", 1); //输出的大小 it.putExtra("outputX", 250); it.putExtra("outputY", 250); //将裁剪好的图片进行返回到Intent中 it.putExtra("return-data", true); startActivityForResult(it, 2000); } //点击完裁剪的完成以后会执行的方法 if(requestCode == 2000 && resultCode == RESULT_OK){ Bitmap bitmap = data.getParcelableExtra("data"); touxiang.setImageBitmap(bitmap); } //得到相册里的图片进行裁剪 if(requestCode == 3000 && resultCode == RESULT_OK){ //得到相册图片 Uri uri = data.getData(); //裁剪 Intent it = new Intent("com.android.camera.action.CROP"); //设置图片 以及格式 it.setDataAndType(uri, "image/*"); //是否支持裁剪 it.putExtra("crop", true); //设置比例 it.putExtra("aspectX", 1); it.putExtra("aspectY", 1); //设置输出的大小 it.putExtra("outputX", 250); it.putExtra("outputY", 250); //是否支持人脸识别 // it.putExtra("onFaceDetection", true); //返回 it.putExtra("return-data", true); startActivityForResult(it, 4000); } //2.点击裁剪完成 if(requestCode == 4000 && resultCode == RESULT_OK){ Bitmap bitmap = data.getParcelableExtra("data"); touxiang.setImageBitmap(bitmap); } }
}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:id="@+id/xiangji" android:text="相机" android:textSize="25dp" android:gravity="center" android:background="#fff"/> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:id="@+id/xiangce" android:textSize="25dp" android:gravity="center" android:text="相册" android:background="#fff"/> <TextView android:layout_width="match_parent" android:layout_height="45dp" android:id="@+id/quxiao" android:layout_marginTop="10dp" android:textSize="25dp" android:gravity="center" android:text="取消" android:background="#fff"/> </LinearLayout>