3.PopupWindow 、拍照、裁剪

实现这样的效果

圆角图片的自定义控件直接拷进来,和com一个等级

想要弹出内容可以使用悬浮窗

layout_pupup

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pop_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/btn_style_alert_dialog_background"
    android:gravity="center_horizontal"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btn_take_photo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="20dip"
        android:background="@drawable/btn_style_alert_dialog_button"
        android:text="拍照"
        android:textStyle="bold" />
    <Button
        android:id="@+id/btn_pick_photo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="5dip"
        android:background="@drawable/btn_style_alert_dialog_button"
        android:text="从相册选择"
        android:textStyle="bold" />
    <Button
        android:id="@+id/btn_cancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dip"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:layout_marginTop="15dip"
        android:background="@drawable/btn_style_alert_dialog_cancel"
        android:text="取消"
        android:textColor="#ffffff"
        android:textStyle="bold" />
</LinearLayout>

 activity_main:view是阴影

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context=".MainActivity" >
    <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示对话框的形式" />
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/civ"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:src="@drawable/man"
        app:border_color="#ccc"
        app:border_width="2dp" >
    </de.hdodenhof.circleimageview.CircleImageView>
    <View
        android:id="@+id/viewMask"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#20000000"
        android:visibility="gone" >
    </View>
</RelativeLayout>

push_bottom_in

<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <translate
        android:duration="200"
        android:fromYDelta="100%p"
        android:toYDelta="0"        
     />   
     <alpha
	android:fromAlpha="0.0"
	android:toAlpha="1.0"
	android:duration="200"
	/>     
</set>

   push_bottom_in2

<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
   <alpha
        android:startOffset="200"
		android:fromAlpha="0.0"
		android:toAlpha="1.0"
		android:duration="200"
	/>       
</set>

  push_bottom_out

<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
 <alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="200"
    />  
</set>

styles

<resources>
    <style name="AppTheme" parent="android:Theme.Light" />
    <style name="AnimBottom" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>
    <style name="PopupAnimation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
    </style>
</resources>

  SelectPhotoPopupWindow

public class SelectPhotoPopupWindow extends PopupWindow {
    private Button btn_take_photo, btn_pick_photo, btn_cancel;
    public SelectPhotoPopupWindow(Context context, OnClickListener onClickListener) {
        View contentView = View.inflate(context, R.layout.layout_pupup, null);
        /*
         * View contentView = View.inflate(MainActivity.this, R.layout.file_item_pop, null);
                int width = ViewGroup.LayoutParams.MATCH_PARENT;
                int height = itemView.getHeight();
                System.out.println("height:" + height);
                popupWindow = new PopupWindow(contentView, width, height);
                /*点击popupWindow范围以外的地方,让popupWindow消失*/
        //                popupWindow.setOutsideTouchable(true);
        //                popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //         */
        this.setContentView(contentView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        /**点击popupWindow范围以外的地方,让popupWindow消失*/
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        //找到对应的控件
        btn_take_photo = (Button) contentView.findViewById(R.id.btn_take_photo);
        btn_pick_photo = (Button) contentView.findViewById(R.id.btn_pick_photo);
        btn_cancel = (Button) contentView.findViewById(R.id.btn_cancel);
        
        btn_take_photo.setOnClickListener(onClickListener);
        btn_pick_photo.setOnClickListener(onClickListener);
        btn_cancel.setOnClickListener(onClickListener);
        //加入动画
        this.setAnimationStyle(R.style.AnimBottom);
    }
}

MainActivity

public class MainActivity extends Activity implements OnClickListener {
    protected static final int CODE_TAKE_PHOTO = 100;
    protected static final int CODE_PICK_PHOTO = 101;
    private static final int CODE_ZOOM_PHOTOT = 102;
    private String sdCardPath = Environment.getExternalStorageDirectory().getAbsolutePath();
    private File tempFile = new File(sdCardPath + "/" + "tempFile.jpg");
    private View root;
    private SelectPhotoPopupWindow selectPhotoPopupWindow;
    private View viewMask;
    private CheckBox cb;
    private CircleImageView civ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewMask = findViewById(R.id.viewMask);
        root = findViewById(R.id.rl_root);
        cb = (CheckBox) findViewById(R.id.cb);
        civ = (CircleImageView) findViewById(R.id.civ);
        civ.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cb.isChecked()) {
                    PhotoUtilChange.getPhotoDialog(MainActivity.this, CODE_TAKE_PHOTO, CODE_PICK_PHOTO, tempFile);
                } else {
                    selectPhotoPopupWindow = PhotoUtilChange.getPicPopupWindow(MainActivity.this, MainActivity.this,
                            root);
                    AnimationUtils.showAlpha(viewMask);
                }
            }
        });
        viewMask.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectPhotoPopupWindow != null) {
                    selectPhotoPopupWindow.dismiss();
                    AnimationUtils.hideAlpha(viewMask);
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
        selectPhotoPopupWindow.dismiss();
        switch (v.getId()) {
        case R.id.btn_cancel:
            Toast.makeText(getApplicationContext(), "cancle", 0).show();
            AnimationUtils.hideAlpha(viewMask);
            break;
        case R.id.btn_take_photo://拍照
            //1.发起拍照的intent
            PhotoUtilChange.takePhoto(MainActivity.this, CODE_TAKE_PHOTO, tempFile);
            break;
        case R.id.btn_pick_photo://从相册选择
            //1.发起从相册选择的intent
            PhotoUtilChange.pickPhoto(MainActivity.this, CODE_PICK_PHOTO, tempFile);
            break;
        default:
            break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
        case CODE_TAKE_PHOTO://拍照
            //2.处理拍照的结果-->去裁剪
            PhotoUtilChange.onPhotoFromCamera(MainActivity.this, CODE_ZOOM_PHOTOT, tempFile.getAbsolutePath(), 1, 1);
            break;
        case CODE_PICK_PHOTO://从相册选择
            //2.处理从相册选择的结果-->去裁剪
            PhotoUtilChange.onPhotoFromPick(MainActivity.this, CODE_ZOOM_PHOTOT, tempFile.getAbsolutePath(), data, 50,
                    50);
            break;
        case CODE_ZOOM_PHOTOT://裁剪
            //3.裁剪完成
            Bitmap zoomBitMap = PhotoUtilChange.getZoomBitMap(data, MainActivity.this);
            //4.修改头像
            civ.setImageBitmap(zoomBitMap);
            break;
        default:
            break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

PhotoUtilChange

/***
 * billy修改版
 * 头像上传工具类 调用 getPhoto 在onactivityResult 调用
 * onPhotoFromCamera
 * onPhotoFromPick
 */
public class PhotoUtilChange {
	/**
	 * 因为处理不同
	 * 
	 * @param takePhotoCode
	 *            Uri originalUri = data.getData();
	 *            image=ImageUtil.getBitmapFromUrl(originalUri.toString());
	 ********************************************************************************** 
	 * @param pickPhotoCode
	 *            Bundle extras = data.getExtras(); image = (Bitmap)
	 *            extras.get("data");
	 * @param tempFile
	 *            拍照时的临时文件 需要zoom时
	 * **/
	public static boolean getPhotoDialog(final Activity activity, final int takePhotoCode, final int pickPhotoCode,
			final File tempFile) {
		final CharSequence[] items = { "相册", "拍照" };
		AlertDialog dlg = new AlertDialog.Builder(activity).setTitle("选择图片")
				.setItems(items, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int item) {
						if (item == 1) {
							Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
							getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
							activity.startActivityForResult(getImageByCamera, takePhotoCode);
						} else {
							Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
							getImage.addCategory(Intent.CATEGORY_OPENABLE);
							getImage.setType("image/jpeg");
							activity.startActivityForResult(getImage, pickPhotoCode);
						}
					}
				}).create();
		dlg.show();
		return true;
	}
	public static SelectPhotoPopupWindow getPicPopupWindow(Context context, OnClickListener itemsOnClick,
			View viewAttach) {
		//实例化SelectPicPopupWindow
		SelectPhotoPopupWindow menuWindow = new SelectPhotoPopupWindow(context, itemsOnClick);
		//显示窗口
		menuWindow.showAtLocation(viewAttach, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); //设置layout在PopupWindow中显示的位置
		return menuWindow;
	}
	public static boolean takePhoto(Activity activity, int takePhotoCode, File tempFile) {
		Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
		getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));
		activity.startActivityForResult(getImageByCamera, takePhotoCode);
		return true;
	}
	public static boolean pickPhoto(Activity activity, int imageCode, File tempFile) {
		Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
		getImage.addCategory(Intent.CATEGORY_OPENABLE);
		getImage.setType("image/jpeg");
		activity.startActivityForResult(getImage, imageCode);
		return true;
	}
	/**
	 * 拍照获取图片的方式 用于切割的图片大小被限制在500,500
	 * 
	 * @param context
	 * @param zoomCode
	 * @param temppath
	 *            拍照前生成的临时路劲
	 * @return 新的路劲
	 */
	public static String onPhotoFromCamera(final Activity context, final int zoomCode, final String temppath,
			final int aspectX, final int aspectY) {
		try {
			Bitmap btp = getLocalImage(new File(temppath), 1000, 1000);
			compressImage(btp, new File(temppath + "temp.jpg"), 30);
			photoZoom(context, Uri.fromFile(new File(temppath + "temp.jpg")), Uri.fromFile(new File(temppath)),
					zoomCode, aspectX, aspectY);
		} catch (Exception e) {
			Toast.makeText(context, "图片加载失败", 1000).show();
		}
		return temppath;
	}
	/**
	 * 图片切割完调用 如果还需要 Bitmap 调用getLocalImage
	 * 
	 * @param path
	 * @param rw
	 * @param rh
	 * @param compress
	 * @return
	 */
	public static File onPhotoZoom(String path, int rw, int rh, int compress) {
		File f = new File(path);
		Bitmap btp = PhotoUtilChange.getLocalImage(f, rw, rh);
		compressImage(btp, f, compress);
		return f;
	}
	/**
	 * 相册获取图片,用于切割的图片大小被限制在500,500
	 * 
	 * @param context
	 * @param zoomCode
	 * @param temppath
	 *            希望生成的路劲
	 * @param data
	 */
	public static void onPhotoFromPick(final Activity context, final int zoomCode, final String temppath,
			final Intent data, final int aspectX, final int aspectY) {
		try {
			Bitmap btp = checkImage(context, data);
			compressImage(btp, new File(temppath + "temp.jpg"), 30);
			PhotoUtilChange.photoZoom(context, Uri.fromFile(new File(temppath + "temp.jpg")),
					Uri.fromFile(new File(temppath)), zoomCode, aspectX, aspectY);
		} catch (Exception e) {
			Toast.makeText(context, "图片加载失败", 1000).show();
		}
	}
	/**
	 * data 中检出图片
	 * 
	 * @param activity
	 * @param data
	 * @return
	 */
	public static Bitmap checkImage(Activity activity, Intent data) {
		Bitmap bitmap = null;
		try {
			Uri originalUri = data.getData();
			String path = getRealPathFromURI(activity, originalUri);
			File f = activity.getExternalCacheDir();
			String pp = f.getAbsolutePath();
			if (path.indexOf(pp) != -1) {
				path = path.substring(path.indexOf(pp), path.length());
			}
			bitmap = getLocalImage(new File(path), 1000, 1000);
		} catch (Exception e) {
		} finally {
			return bitmap;
		}
	}
	/**
	 * 通过URI 获取真实路劲
	 * 
	 * @param activity
	 * @param contentUri
	 * @return
	 */
	public static String getRealPathFromURI(Activity activity, Uri contentUri) {
		Cursor cursor = null;
		String result = contentUri.toString();
		String[] proj = { MediaStore.Images.Media.DATA };
		cursor = activity.managedQuery(contentUri, proj, null, null, null);
		if (cursor == null)
			throw new NullPointerException("reader file field");
		if (cursor != null) {
			int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
			cursor.moveToFirst();
			result = cursor.getString(column_index);
			if (Integer.parseInt(Build.VERSION.SDK) < 14) {
				cursor.close();
			}
		}
		return result;
	}
	/**
	 * 图片压缩 上传图片时建议compress为30
	 * 
	 * @param bm
	 * @param f
	 */
	public static void compressImage(Bitmap bm, File f, int compress) {
		if (bm == null)
			return;
		File file = f;
		try {
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();
			OutputStream outStream = new FileOutputStream(file);
			bm.compress(android.graphics.Bitmap.CompressFormat.JPEG, compress, outStream);
			outStream.flush();
			outStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 由本地文件获取希望大小的文件
	 * 
	 * @param f
	 * @return
	 */
	public static Bitmap getLocalImage(File f, int swidth, int sheight) {
		File file = f;
		if (file.exists()) {
			try {
				file.setLastModified(System.currentTimeMillis());
				FileInputStream in = new FileInputStream(file);
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inJustDecodeBounds = true;
				BitmapFactory.decodeStream(in, null, options);
				int sWidth = swidth;
				int sHeight = sheight;
				int mWidth = options.outWidth;
				int mHeight = options.outHeight;
				int s = 1;
				while ((mWidth / s > sWidth * 2) || (mHeight / s > sHeight * 2)) {
					s *= 2;
				}
				options = new BitmapFactory.Options();
				options.inSampleSize = s;
				options.inPreferredConfig = Bitmap.Config.RGB_565;
				options.inPurgeable = true;
				options.inInputShareable = true;
				try {
					// 4. inNativeAlloc 属性设置为true,可以不把使用的内存算到VM里
					BitmapFactory.Options.class.getField("inNativeAlloc").setBoolean(options, true);
				} catch (Exception e) {
				}
				in.close();
				// 再次获取
				in = new FileInputStream(file);
				Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
				in.close();
				return bitmap;
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			} catch (Error e) {
				System.gc();
				return null;
			}
		}
		return null;
	}
	/**
	 * aspectY Y对于X的比例 outputX X 的宽
	 * **/
	public static void photoZoom(Activity activity, Uri uri, Uri outUri, int photoResoultCode, int aspectX, int aspectY) {
		Intent intent = new Intent("com.android.camera.action.CROP");
		intent.setDataAndType(uri, "image/*");
		intent.putExtra("crop", "true");
		// aspectX aspectY 是宽高的比例
		if (aspectY > 0) {
			intent.putExtra("aspectX", aspectX);
			intent.putExtra("aspectY", aspectY);
		}
		intent.putExtra("scale", aspectX == aspectY);
		intent.putExtra("return-data", true);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
		intent.putExtra("noFaceDetection", true); //
		activity.startActivityForResult(intent, photoResoultCode);
	}
	/**
	 * 保存zoom之后的图片
	 * @param data zoom后的intent
	 * @param context 上下文
	 * @return
	 */
	public static Bitmap getZoomBitMap(Intent data, Context context) {
		try {
			Bundle extras = data.getExtras();
			if (extras != null) {
				Bitmap bitmap = extras.getParcelable("data");
				return bitmap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(context, "出现未知异常,请尝试其他图片", Toast.LENGTH_SHORT).show();
		}
		return null;
	}
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Android干货园

Android 获取屏幕高宽度,密度,通知栏高度,截图等常用方法

版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/lyhhj/article/details/49...

3461
来自专栏Android Note

Android - 圆形 Button 与倒计时控件

1755
来自专栏向治洪

android沉浸式状态栏的实现

在style.xml中添加 <style name="Theme.Timetodo" parent="@android:style/Theme.Holo.L...

2417
来自专栏向治洪

ViewFlipper实现多页面切换

1、屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。...

22710
来自专栏阿炬.NET

【阿炬Android笔记】01、调用VitamioBundle播放窗口

2994
来自专栏Android开发指南

6.listview显示不用条目

35910
来自专栏向治洪

android 仿qq手写板涂鸦

以前博客的链接:点击打开链接 附上关键代码: MainView.java package com.tszy.views;   import java.io....

2336
来自专栏Fish

从相机or相册获取图片并显示

这个技术应该算是十分稀松平常了,但是对于小白来说,还是要费一番功夫的。因此在这里贴上我的代码,也是为了以后用到的时候方便找。。。 package com.exa...

3027
来自专栏何俊林

Android TV开发总结(七)构建一个TV app中的剧集列表控件

前言:剧集类控件,在TV app中非常常见,今天将介绍构建一个TV app中的剧集列表控件,此控件上传到我的Github: https://github.com...

3386
来自专栏非著名程序员

Android学习笔记(一)之仿正点闹钟时间齿轮滑动的效果

看到正点闹钟上的设置时间的滑动效果非常好看,自己就想做一个那样的,在网上就开始搜资料了,看到网上有的齿轮效果的代码非常多,也非常难懂,我就决定自己研究一下,现在...

23610

扫码关注云+社区

领取腾讯云代金券