效果图展示:
dialog代码
public class ActionSheetDialog {
private Context context;
private Dialog dialog;
private TextView txt_title;
private TextView txt_cancel;
private LinearLayout lLayout_content;
private ScrollView sLayout_content;
private boolean showTitle = false;
private List<SheetItem> sheetItemList;
private Display display;
public ActionSheetDialog(Context context) {
this.context = context;
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
display = windowManager.getDefaultDisplay();
}
public ActionSheetDialog builder() {
// 获取Dialog布局
View view = LayoutInflater.from(context).inflate(
R.layout.toast_view_actionsheet, null);
// 设置Dialog最小宽度为屏幕宽度
view.setMinimumWidth(display.getWidth());
// 获取自定义Dialog布局中的控件
sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content);
lLayout_content = (LinearLayout) view
.findViewById(R.id.lLayout_content);
txt_title = (TextView) view.findViewById(R.id.txt_title);
txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);
txt_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 定义Dialog布局和参数
dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
dialog.setContentView(view);
Window dialogWindow = dialog.getWindow();
dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.x = 0;
lp.y = 0;
dialogWindow.setAttributes(lp);
return this;
}
public ActionSheetDialog setTitle(String title) {
showTitle = true;
txt_title.setVisibility(View.VISIBLE);
txt_title.setText(title);
return this;
}
public ActionSheetDialog setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
public ActionSheetDialog setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}
/**
*
* @param strItem
* 条目名称
* @param color
* 条目字体颜色,设置null则默认蓝色
* @param listener
* @return
*/
public ActionSheetDialog addSheetItem(String strItem, SheetItemColor color,
OnSheetItemClickListener listener) {
if (sheetItemList == null) {
sheetItemList = new ArrayList<SheetItem>();
}
sheetItemList.add(new SheetItem(strItem, color, listener));
return this;
}
/** 设置条目布局 */
private void setSheetItems() {
if (sheetItemList == null || sheetItemList.size() <= 0) {
return;
}
int size = sheetItemList.size();
// TODO 高度控制,非最佳解决办法
// 添加条目过多的时候控制高度
if (size >= 7) {
WindowManager.LayoutParams params = (WindowManager.LayoutParams) sLayout_content
.getLayoutParams();
params.height = display.getHeight() / 2;
sLayout_content.setLayoutParams(params);
}
// 循环添加条目
for (int i = 1; i <= size; i++) {
final int index = i;
SheetItem sheetItem = sheetItemList.get(i - 1);
String strItem = sheetItem.name;
SheetItemColor color = sheetItem.color;
final OnSheetItemClickListener listener = sheetItem.itemClickListener;
TextView textView = new TextView(context);
textView.setText(strItem);
textView.setTextSize(18);
Resources resources = context.getResources();
float fPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 44, resources.getDisplayMetrics());
textView.setHeight(Math.round(fPx));
textView.setGravity(Gravity.CENTER);
// 背景图片
if (size == 1) {
if (showTitle) {
textView.setBackgroundResource(R.drawable.shape_actionsheet_bottom);
} else {
textView.setBackgroundResource(R.drawable.shape_actionsheet_single);
}
} else {
if (showTitle) {
if (i >= 1 && i < size) {
textView.setBackgroundResource(R.drawable.shape_actionsheet_middle);
} else {
textView.setBackgroundResource(R.drawable.shape_actionsheet_bottom);
}
} else {
if (i == 1) {
textView.setBackgroundResource(R.drawable.shape_actionsheet_top);
} else if (i < size) {
textView.setBackgroundResource(R.drawable.shape_actionsheet_middle);
} else {
textView.setBackgroundResource(R.drawable.shape_actionsheet_bottom);
}
}
}
// 字体颜色
if (color == null) {
textView.setTextColor(Color.parseColor(SheetItemColor.Blue .getName()));
} else {
textView.setTextColor(Color.parseColor(color.getName()));
// textView.setBackgroundResource(R.drawable.ic_launcher);
}
// 高度
float scale = context.getResources().getDisplayMetrics().density;
int height = (int) (45 * scale + 0.5f);
textView.setLayoutParams(new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT, height));
// 点击事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(index);
dialog.dismiss();
}
});
lLayout_content.addView(textView);
}
}
public void show() {
setSheetItems();
dialog.show();
}
public interface OnSheetItemClickListener {
void onClick(int which);
}
public class SheetItem {
String name;
OnSheetItemClickListener itemClickListener;
SheetItemColor color;
public SheetItem(String name, SheetItemColor color,
OnSheetItemClickListener itemClickListener) {
this.name = name;
this.color = color;
this.itemClickListener = itemClickListener;
}
}
public enum SheetItemColor {
Blue("#037BFF"), Red("#FD4A2E"),black("#000000");
private String name;
SheetItemColor(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}
相应地方调用此dialog,并打开相机和图库
new ActionSheetDialog(this)
.builder()
.setCanceledOnTouchOutside(true)
.addSheetItem("拍照", ActionSheetDialog.SheetItemColor.black, new ActionSheetDialog.OnSheetItemClickListener() {
@Override
public void onClick(int which) {
Intent intent = new Intent();
// 指定开启系统相机的Action
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_DEFAULT);
// 根据文件地址创建文件
File file = new File(FILE_PATH);
if (file.exists()) {
file.delete();
}
// 把文件地址转换成Uri格式
Uri uri = Uri.fromFile(file);
// 设置系统相机拍摄照片完成后图片文件的存放地址
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent,1);
}
})
.addSheetItem("照片库", ActionSheetDialog.SheetItemColor.black, new ActionSheetDialog.OnSheetItemClickListener() {
@Override
public void onClick(int which) {
Intent i = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android的图库
startActivityForResult(i, 2);
}
}).show();
在onActivityResult中完成拍照或选中图片之后的操作
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("1111111", "系统相机拍照完成,resultCode="+resultCode);
switch (requestCode){
case 1:
switch (resultCode) {
case Activity.RESULT_OK: {
/*开始进行图片识别*/
startActivityForResult(CropUtils.invokeSystemCrop(Uri.fromFile(new File(FILE_PATH))), 3);
}
break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
case 2:
switch (resultCode) {
case Activity.RESULT_OK: {
/*从图库获取图片*/
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
startActivityForResult(CropUtils.invokeSystemCrop(Uri.fromFile(new File(picturePath))), 3);
}
break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
case 3:
switch (resultCode) {
case Activity.RESULT_OK: {
if (data != null){
String path = CropUtils.getPath();
}
}
break;
case Activity.RESULT_CANCELED:// 取消
break;
}
break;
}
}
CropUtils中方法为图片保存以及调用系统裁剪功能,具体如下:
public class CropUtils {
private static String mFile;
public static String getPath() {
//resize image to thumb
if (mFile == null) {
mFile = Environment.getExternalStorageDirectory() + "/" + ".jpg";
}
return mFile;
}
/**
* 调用系统照片的裁剪功能
*/
public static Intent invokeSystemCrop(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 800);
intent.putExtra("outputY", 800);
intent.putExtra("return-data", false);
intent.putExtra("scale", true);
File out = new File(getPath());
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
return intent;
}
}