老方法先上图:
使用说明:
该对话框使用纯代码加XML编写不含任何图片,简单且不复杂。网上的很多对话框啊大多都是有图片,所以任何一个资源下载不完整的话可能就会导致无法使用。该对话框的结构是一个自定义对话框类加上较多的XML,这里不给出xml的代码只给出自定义对话框类的代码(因为xml实在太多了,懒得写)
这是类MyDialog.java的代码(本对话框中唯一的类),代码如下:
package com.example.mydialogupdate;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* 使用说明:
* 要想改对话框圆角的大小去background_shape.xml里面改,
* 同时还要到left_button_shape_depend_resource.xml和right_button_shape_depend_resource.xml里面去修改
* 如果想使用一个按钮的话除了上述的xml文件外,额外还要到single_button_shape_depend_resource.xml里面去改;
* 要想改背景颜色也是一样的
*
* 使用提示:
* 本类提供方法setTitleSize()改标题字号和setMessageSize()改内容字号
* setMessagePadding()方法改内容上下左右边距
* setTitleHeight()和setBottomHeight()分别设置标题和底部按钮的高度
* 其他的想改字体颜色的到代码里面去看就行了,在方法createAllView()里面自行修改,方法就懒得提供了
* 本类全代码书写,依赖较多的xml,少一个就没法正常运行,提供两种构造方法,一个参数的原生IOS风格,两个和三个参数的可以自定义
* 背景图片,如果设置了radius还可以虚化,(暂时只支持低度虚化 所以半径不得超过25),3个参数的构造方法最后一个参数是图片截取的位置 ,
* 默认情况下图片是经过压缩处理之后适应对话框大小的 ,如果出现图片拉伸 非常严重的情况可以考虑添加第3个参数 直接对图片进行裁剪
* 裁剪位置暂时只支持上中下 。
*/
public class MyDialog extends Dialog {
public static final int TOP = 123;
public static final int MIDDLE = 124;
public static final int BOTTOM = 125;
private String title = "提示";
private int type = 0;
private String message = "这是一个弹窗";
private Context context;
private float radius = 0;
private float cornerRadius = 35;//这里设置圆角要和xml里的圆角半径一致,不然显示效果不佳
private boolean isThereNavigateButton = false;
private boolean isTherePositiveButton = false;
private final float titleSize = 22;//标题字体大小默认值
private final float messageSize = 18;//内容字体大小默认值
private int widthPixels = 1080;
private int heightPixels = 2194;
private double scale = 0;
private int bitmapId = 0;
private TextView titleTextView;
private ImageView background;
private TextView messageTextView;
private Button navigateButton;
private Button positiveButton;
private LinearLayout linearLayout;
private ImageView imageView;
private ImageView imageView1;
private ImageView imageView2;
private LinearLayout linearLayout1;
private RelativeLayout relativeLayout;
//构造方法
public MyDialog(Context context) {
super(context, R.style.CustomDialog);//加载资源
this.context = context;
scale = context.getResources().getDisplayMetrics().scaledDensity;
createAllView();
setLayout();
}
public MyDialog(Context context, int drawableId) {
super(context, R.style.CustomDialog);
this.context = context;
bitmapId = drawableId;
scale = context.getResources().getDisplayMetrics().scaledDensity;
createAllView();
setLayout();
relativeLayout.addView(linearLayout);
}
public MyDialog(Context context, int drawableId, int type) {
super(context, R.style.CustomDialog);
this.context = context;
this.type = type;
bitmapId = drawableId;
scale = context.getResources().getDisplayMetrics().scaledDensity;
createAllView();
setLayout();
relativeLayout.addView(linearLayout);
}
// 本来想搞一个更高级的,但是发现阴影很难做就算了
// public MyDialog(Context context, float radius) {
// super(context, R.style.CustomDialog);
// this.context = context;
// this.radius = radius;
//
// }
//当调用show()方法的时候系统会自动调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载布局
if (bitmapId != 0) {
setContentView(relativeLayout);
//加载背景图片
linearLayout.post(new Runnable() {
@Override
public void run() {
setBackground();
}
});
} else {
setContentView(linearLayout);
}
//设置按钮有无
if (!isTherePositiveButton) {
positiveButton.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
if (bitmapId == 0) {
navigateButton.setBackgroundResource(R.drawable.single_button_shape);
} else {
navigateButton.setBackgroundResource(R.drawable.single_button_shape_with_bitmap);
}
}
if (!isThereNavigateButton) {
navigateButton.setVisibility(View.GONE);
imageView2.setVisibility(View.GONE);
if (bitmapId == 0) {
positiveButton.setBackgroundResource(R.drawable.single_button_shape);
} else {
positiveButton.setBackgroundResource(R.drawable.single_button_shape_with_bitmap);
}
}
if (!isThereNavigateButton && !isTherePositiveButton) {
imageView1.setVisibility(View.GONE);
linearLayout1.setVisibility(View.GONE);
}
}
//高度适配函数
private int fixedHeightValue(double value) {
if (value >= 0 && value <= 100) {
return (int) ((int) (((heightPixels / 100.0) * value) / 3.0) * scale);
} else if (value > 100) {
value = 100;
return (int) ((heightPixels / 100.0) * value);
} else {
value = 0;
return (int) ((heightPixels / 100.0) * value);
}
}
//宽度适配函数
private int fixedWidthValue(double value) {
if (value >= 0 && value <= 100) {
return (int) ((int) (((widthPixels / 100.0) * value) / 3.0) * scale);
} else if (value > 100) {
value = 100;
return (int) ((widthPixels / 100.0) * value);
} else {
value = 0;
return (int) ((widthPixels / 100.0) * value);
}
}
//加载图片
private Bitmap loadBitmapWithoutScale(int id) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bt = BitmapFactory.decodeResource(context.getResources(), id, options);
return bt;
}
//切圆角
private Bitmap getRoundCornerBitmap(Bitmap bitmap, float roundPX) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap bitmap2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap2);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, height);
final RectF rectF = new RectF(rect);
paint.setColor(color);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, roundPX, roundPX, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return bitmap2;
}
//虚化图片
private Bitmap blurBitmap(Context context, Bitmap bitmap, float radius) {
//用需要创建高斯模糊bitmap创建一个空的bitmap
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
// 初始化Renderscript,该类提供了RenderScript context,创建其他RS类之前必须先创建这个类,其控制RenderScript的初始化,资源管理及释放
RenderScript rs = RenderScript.create(context);
// 创建高斯模糊对象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 创建Allocations,此类是将数据传递给RenderScript内核的主要方 法,并制定一个后备类型存储给定类型
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//设定模糊度(注:Radius最大只能设置25.f)
if (radius <= 25.f) {
blurScript.setRadius(radius);
} else {
blurScript.setRadius(25.f);
}
// Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
// Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
rs.destroy();
return outBitmap;
}
//截取图片
private Bitmap getBackgroundBitmap(Bitmap bitmap, int type) {
int x = 0;
int y = 0;
Bitmap newBitmap = null;
//截取图片
if (type == this.TOP) {
//截取顶部
x = (int) ((bitmap.getWidth() - linearLayout.getWidth()) / 2.0);
if (x < 0) {
x = 0;
//缩放对齐
bitmap = scaleBitmap(bitmap, 1);
if (linearLayout.getHeight() > bitmap.getHeight()) {
bitmap = scaleBitmap(bitmap, 0);
}
newBitmap = Bitmap.createBitmap(bitmap, x, y, linearLayout.getWidth(), linearLayout.getHeight());
} else {
if (linearLayout.getHeight() > bitmap.getHeight()) {
//缩放图片
bitmap = scaleBitmap(bitmap, 0);
}
newBitmap = Bitmap.createBitmap(bitmap, x, y, linearLayout.getWidth(), linearLayout.getHeight());
}
} else if (type == this.MIDDLE) {
//截取中间
x = (int) ((bitmap.getWidth() - linearLayout.getWidth()) / 2.0);
y = (int) ((bitmap.getHeight() - linearLayout.getHeight()) / 2.0);
if (x >= 0 && y >= 0) {
newBitmap = Bitmap.createBitmap(bitmap, x, y, linearLayout.getWidth(), linearLayout.getHeight());
} else if (x >= 0 && y <= 0) {
y = 0;
//缩放图片
bitmap = scaleBitmap(bitmap, 0);
newBitmap = Bitmap.createBitmap(bitmap, x, y, linearLayout.getWidth(), bitmap.getHeight());
} else if (x <= 0 && y >= 0) {
x = 0;
//缩放图片
bitmap = scaleBitmap(bitmap, 1);
newBitmap = Bitmap.createBitmap(bitmap, x, y, bitmap.getWidth(), linearLayout.getHeight());
} else {
newBitmap = scaleBitmap(bitmap, 3);
}
} else if (type == this.BOTTOM) {
//截取底部
x = (int) ((bitmap.getWidth() - linearLayout.getWidth()) / 2.0);
y = bitmap.getHeight() - linearLayout.getHeight();
if (x >= 0 && y >= 0) {
newBitmap = Bitmap.createBitmap(bitmap, x, y, linearLayout.getWidth(), linearLayout.getHeight());
} else if (x >= 0 && y <= 0) {
y = 0;
//缩放高度
bitmap = scaleBitmap(bitmap, 0);
newBitmap = Bitmap.createBitmap(bitmap, x, y, linearLayout.getWidth(), bitmap.getHeight());
} else if (x <= 0 && y >= 0) {
x = 0;
//缩放宽
bitmap = scaleBitmap(bitmap, 1);
newBitmap = Bitmap.createBitmap(bitmap, x, y, bitmap.getWidth(), linearLayout.getHeight());
} else {
newBitmap = scaleBitmap(bitmap, 3);
}
} else {
newBitmap = scaleBitmap(bitmap, 3);
}
return newBitmap;
}
//缩放图片(getBackgroundBitmap函数里面要用)
private Bitmap scaleBitmap(Bitmap bt, int mode) {
float ratio = ((float) bt.getHeight()) / bt.getWidth();
Bitmap newBitmap = null;
if (mode == 0) {
int newHeight = linearLayout.getHeight();
int newWidth = (int) (newHeight / ratio);
//计算缩放比例
float scaleWidth = ((float) newWidth) / bt.getWidth();
float scaleHeight = ((float) newHeight) / bt.getHeight();
//创建矩阵,开始缩放
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
//得到新图片
newBitmap = Bitmap.createBitmap(bt, 0, 0, bt.getWidth(), bt.getHeight(), matrix, true);
} else if (mode == 1) {
int newWidth = linearLayout.getWidth();
int newHeight = (int) (newWidth * ratio);
//计算缩放比例
float scaleWidth = ((float) newWidth) / bt.getWidth();
float scaleHeight = ((float) newHeight) / bt.getHeight();
//创建矩阵,开始缩放
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
//得到新图片
newBitmap = Bitmap.createBitmap(bt, 0, 0, bt.getWidth(), bt.getHeight(), matrix, true);
} else {
int newWidth = linearLayout.getWidth();
int newHeight = linearLayout.getHeight();
//计算缩放比例
float scaleWidth = ((float) newWidth) / bt.getWidth();
float scaleHeight = ((float) newHeight) / bt.getHeight();
//创建矩阵,开始缩放
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
//得到新图片
newBitmap = Bitmap.createBitmap(bt, 0, 0, bt.getWidth(), bt.getHeight(), matrix, true);
}
return newBitmap;
}
//处理图片(要在onCreate里面调用)
private void setBackground() {
Bitmap bt = null;
if (radius != 0) {
bt = getRoundCornerBitmap(blurBitmap(context, getBackgroundBitmap(loadBitmapWithoutScale(bitmapId), type), radius), (float) (cornerRadius * scale));
} else {
bt = getRoundCornerBitmap(getBackgroundBitmap(loadBitmapWithoutScale(bitmapId), type), (float) (cornerRadius * scale));
}
background.setImageBitmap(bt);
}
//创建控件并设置控件的参数
private void createAllView() {
linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
if (bitmapId == 0) {
linearLayout.setBackgroundResource(R.drawable.background_shape);//加载原生iOS风格资源
} else {
relativeLayout = new RelativeLayout(context);
background = new ImageView(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
background.setLayoutParams(layoutParams);
relativeLayout.addView(background);
}
titleTextView = new TextView(context);
titleTextView.setHeight(fixedHeightValue(6.8));//设置标题的高度
titleTextView.setPadding(fixedWidthValue(6.25), fixedHeightValue(2.3), fixedWidthValue(6.25), 0);
titleTextView.setGravity(Gravity.CENTER);
titleTextView.setSingleLine(true);
titleTextView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
titleTextView.setFocusable(true);
titleTextView.setFocusableInTouchMode(true);
titleTextView.requestFocus();
titleTextView.setText("请设置标题");
titleTextView.setTextColor(Color.BLACK);//设置标题字体颜色
titleTextView.setTextSize(titleSize);//设置标题字体大小
titleTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//设置字体样式
imageView = new ImageView(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(fixedWidthValue(67.13), 1);
imageView.setLayoutParams(layoutParams);
messageTextView = new TextView(context);
messageTextView.setTypeface(Typeface.SANS_SERIF);
messageTextView.setText("请设置内容");
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params3.gravity = Gravity.CENTER;
messageTextView.setLayoutParams(params3);
messageTextView.setTextColor(Color.BLACK);//设置内容字体颜色
messageTextView.setTextSize(messageSize);//设置内容的字体大小
messageTextView.setPadding(fixedWidthValue(6.9), 0, fixedWidthValue(6.9), fixedHeightValue(3.6));//设置内容与四周的间距
imageView1 = new ImageView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, (int) (1 * scale));
imageView1.setLayoutParams(params);
imageView1.setBackgroundColor(0xffb1b2b2);//设置横向分割线的颜色
linearLayout1 = new LinearLayout(context);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, fixedHeightValue(6.8));
linearLayout1.setLayoutParams(params1);
linearLayout1.setOrientation(LinearLayout.HORIZONTAL);
positiveButton = new Button(context);
positiveButton.setTextColor(0xff3478f6);//设置确定按钮字体颜色
positiveButton.setText("确定");
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
positiveButton.setLayoutParams(params4);
positiveButton.setTextSize(20);//设置确定的字体大小
if (bitmapId == 0) {
positiveButton.setBackgroundResource(R.drawable.left_button_shape);//加载资源
} else {
positiveButton.setBackgroundResource(R.drawable.left_button_shape_with_bitmap);
}
imageView2 = new ImageView(context);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams((int) (1 * scale), LinearLayout.LayoutParams.MATCH_PARENT);
imageView2.setLayoutParams(params2);
imageView2.setBackgroundColor(0xffb1b2b2);//设置纵向分割线的颜色
navigateButton = new Button(context);
navigateButton.setTextColor(0xff3478f6);//设置取消按钮的字体颜色
navigateButton.setText("取消");
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1);
navigateButton.setLayoutParams(params5);
navigateButton.setTextSize(20);//设置取消的字体大小
if (bitmapId == 0) {
navigateButton.setBackgroundResource(R.drawable.right_button_shape);//加载资源
} else {
navigateButton.setBackgroundResource(R.drawable.right_button_shape_with_bitmap);
}
}
//添加布局
private void setLayout() {
linearLayout.addView(titleTextView);
linearLayout.addView(imageView);
linearLayout.addView(messageTextView);
linearLayout.addView(imageView1);
linearLayout.addView(linearLayout1);
linearLayout1.addView(positiveButton);
linearLayout1.addView(imageView2);
linearLayout1.addView(navigateButton);
}
//show
public void show() {
super.show();
}
public MyDialog setNavigateButton(String content, final View.OnClickListener listener) {
navigateButton.setText(content);
navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dismiss();
}
});
isThereNavigateButton = true;
return this;
}
public MyDialog setPositiveButton(String content, final View.OnClickListener listener) {
positiveButton.setText(content);
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(v);
dismiss();
}
});
isTherePositiveButton = true;
return this;
}
public MyDialog setTitle(String title) {
this.title = title;
if (titleTextView == null) {
System.out.println("null");
} else titleTextView.setText(title);
return this;
}
public MyDialog setMessage(String message) {
this.message = message;
messageTextView.setText(message);
return this;
}
//是否设置虚化
public MyDialog setRadius(float radius) {
this.radius = radius;
return this;
}
//后面的暂时不用看了////////////////////////////////////////////////////////////////////////////
public String getTitle() {
return title;
}
public String getMessage() {
return message;
}
//设置标题大小
public void setTitleSize(float titleSize) {
titleTextView.setTextSize(titleSize);
}
//设置内容大小
public void setMessageSize(float messageSize) {
messageTextView.setTextSize(messageSize);
}
//设置内容与四周间距
public void setMessagePadding(int top, int bottom, int left, int right) {
messageTextView.setPadding(left, top, right, bottom);
}
//设置底部按钮的高度
public void setBottomHeight(int bottomHeight) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, bottomHeight);
linearLayout1.setLayoutParams(params);
}
//设置顶部标题的高度
public void setTitleHeight(int titleHeight) {
titleTextView.setHeight(titleHeight);
}
}
xml请自行下载:
链接:h删掉ttp中文s://pan.删掉baidu.c中文om/删掉中文s/12NMIG1UJSuzRnXzeQYFMdw
提取码:sdfl
食用方法:
与Android自带的对话框类似,实例:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView bg = findViewById(R.id.bg);
bg.setImageBitmap(getBitmapWithoutScale(R.drawable.background));
bg.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
//如果只写一个参数context,那么就是原生iOS风格,后面加一张图片id就是自定义图片背景
new MyDialog(MainActivity.this, R.drawable.dialog_backgroud)
.setRadius(50)//设置图片背景是否虚化,超过25的部分无效,请随意!
.setTitle("警告")
.setMessage("电池电量过低!请充电!")
.setPositiveButton("是", new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "嗯!马上冲!", Toast.LENGTH_LONG).show();
}
})
.show();
return false;
}
});
}
xml食用方法:
下载所有xml文件拖入到你的工程目录res/drawable目录下面即可,custom_style.xml要拖到res/values目录下。建议先拖入xml,再敲类,不然IDE可能会神智不清,飘红,但是实际上运行是跑得起来的。