自定义dialog和截屏

news/2024/5/20 12:05:50

自定义对话框

代码: MyDialog.java类

public class MyDialog extends Dialog
{
/**
* 当前上下文
*/
public Context mContext = null;

/**
* 显示的视图
*/
public View layout = null;

/**
* 宽度
*/
public int width = 0;

/**
* 高度
*/
public int height = 0;

/**
* 当前屏幕的宽度
*/
public int screenWidth = 0;

/**
* 当前屏幕的高度
*/
public int screenHeight = 0;

/**
* 水平偏移量
*/
public int offSet_x = 0;

/**
* 垂直偏移量
*/
public int offSet_y = 0;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);

LinearLayout myLayout = new LinearLayout(mContext);
myLayout.setOrientation(LinearLayout.VERTICAL);

LinearLayout.LayoutParams lpWW = new LinearLayout.LayoutParams(width - 6, LinearLayout.LayoutParams.WRAP_CONTENT);// height
                                                                                                                 // -
                                                                                                                 // 6);
lpWW.setMargins(0, 0, 0, 0);
lpWW.gravity = Gravity.CENTER;

myLayout.addView(layout, lpWW);
setContentView(myLayout);
}

/**
* MyDialog构造器

* @param act
*/
public MyDialog(Activity act)
{
super(act);
mContext = act;
}

/**
* 设置对话框大小

* @param width
* @param height
*/
public void setDialogSize(int w, int h)
{
this.width = w;
this.height = h;
Window win = getWindow();
WindowManager.LayoutParams wl = win.getAttributes();

final Display display = getWindow().getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight() - 25;

offSet_x = (screenWidth - width) / 2;
offSet_y = (screenHeight - height) / 2;

wl.width = w;
wl.height = h;
wl.x = 0;
wl.y = 0;
// if(screenWidth > screenHeight)
// {
// wl.y = 0;
// }else
// {
// wl.y = 70;
// }
win.setAttributes(wl);

LinearLayout.LayoutParams lpWW = new LinearLayout.LayoutParams(width - 6, LinearLayout.LayoutParams.WRAP_CONTENT);// height
                                                                                                                 // -
                                                                                                                 // 6);
lpWW.setMargins(0, 0, 0, 0);
lpWW.gravity = Gravity.CENTER;

if (layout != null)
{
layout.setLayoutParams(lpWW);
}
}

/**
* 重设对话框大小

* @param width
* @param height
*/
public void resetDialogSize(int w, int h)
{
this.width = w;
this.height = h;
Window win = getWindow();
WindowManager.LayoutParams wl = win.getAttributes();

final Display display = getWindow().getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();

offSet_x = (screenWidth - width) / 2;
offSet_y = (screenHeight - height) / 2;

wl.width = w;
wl.height = h;
wl.x = 0;
wl.y = 0;

// if(screenWidth > screenHeight)
// {
// wl.y = 0;
// }else
// {
// wl.y = 70;
// }

win.setAttributes(wl);

LinearLayout.LayoutParams lpWW = new LinearLayout.LayoutParams(width - 6, LinearLayout.LayoutParams.WRAP_CONTENT);// height
                                                                                                                 // -
                                                                                                                 // 6);
lpWW.setMargins(0, 0, 0, 0);
lpWW.gravity = Gravity.CENTER;

if (layout != null)
{
layout.setLayoutParams(lpWW);
}
}

/**
* 设置对话框位置

* @param x
* @param y
*/
public void setDialogPosition(int x, int y)
{
Window win = getWindow();
WindowManager.LayoutParams wl = win.getAttributes();
int x_t = wl.x + x;
int y_t = wl.y + y;

if (x_t < -offSet_x)
{
x_t = -offSet_x;
} else if (x_t > offSet_x)
{
x_t = offSet_x;
}

if (y_t < -offSet_y)
{
y_t = -offSet_y;
} else if (y_t > offSet_y)
{
y_t = offSet_y;
}

wl.x = x_t;
wl.y = y_t;
win.setAttributes(wl);
}

/**
* 设置图片资源

* @param resID
*/
@SuppressWarnings("deprecation")
public void setBackground(int resID)
{
Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), resID);
bitmap = bitmapResize(bitmap, width, height);

setBackground(new BitmapDrawable(bitmap));
}

/**
* 设置背景颜色

* @param resID
*/
public void setBackgroundColor(int resID)
{
Window win = this.getWindow();
win.setBackgroundDrawableResource(resID);
}

/**
* 设置背景

* @param drawable
*/
public void setBackground(Drawable drawable)
{
Window win = this.getWindow();
win.setBackgroundDrawable(drawable);
}

/**
* 设置View
*/
public void setMyContentView(View v)
{
layout = v;
if (layout != null)
{
layout.setOnTouchListener(viewTouchListener);
}
}

/**
* 改变背景图片的大小

* @param b
* @param w
* @param h
* @return
*/
private Bitmap bitmapResize(Bitmap b, int w, int h)
{
if (b == null)
{
return null;
}

int oldW = b.getWidth();
int oldH = b.getHeight();

if (oldW == w && oldH == h)
{
return b;
}
//
float scaleWidth = ((float) w) / oldW;
float scaleHeight = ((float) h) / oldH;
//
Matrix matrix = new Matrix();
//
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resized = Bitmap.createBitmap(b, 0, 0, oldW, oldH, matrix, true);

return resized;
}

private int x_old = 0;
private int y_old = 0;
private int x_new = 0;
private int y_new = 0;
private int x_gap = 0;
private int y_gap = 0;

private boolean isTouchTitle = false;

@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
x_old = (int) event.getRawX();
y_old = (int) event.getRawY();
break;
}
case MotionEvent.ACTION_MOVE:
{
x_new = (int) event.getRawX();
y_new = (int) event.getRawY();

x_gap = x_new - x_old;
y_gap = y_new - y_old;

if (isTouchTitle)
{
if (Math.abs(x_gap) > 5 || Math.abs(y_gap) > 5)
{
setDialogPosition((x_gap), (y_gap));

x_old = x_new;
y_old = y_new;
}
}

break;
}
case MotionEvent.ACTION_UP:
{
x_new = (int) event.getRawX();
y_new = (int) event.getRawY();

x_gap = x_new - x_old;
y_gap = y_new - y_old;

if (isTouchTitle)
{
setDialogPosition((x_gap), (y_gap));
}

isTouchTitle = false;
break;
}
}

return super.onTouchEvent(event);
}

/**
* 当前视图触摸监听事件
*/
public OnTouchListener viewTouchListener = new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
int y = (int) event.getY();
if (y < 70)
{
isTouchTitle = true;
}
break;
}
case MotionEvent.ACTION_MOVE:
{
break;
}
case MotionEvent.ACTION_UP:
{
break;
}
}
return false;
}
};
}


使用

/**
 *  show back dialog
 */
private void showBackDialog()
{
// show dialog to confirm
myBackDialog = new MyDialog(this);
LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.back_comform_layout, null);
int size = 0;
if (screenWidth > screenHeight)
{
size = screenWidth;
} else
{
size = screenHeight;
}

// 设置对话框大小
myBackDialog.setDialogSize((int) (screenWidth * 0.9), (int) (size * 0.38));
myBackDialog.setMyContentView(layout);
myBackDialog.show();
ImageButton title_icon = (ImageButton) layout.findViewById(R.id.title_icon);


title_icon.setBackgroundResource(R.drawable.back_info);

TextView title_text = (TextView) layout.findViewById(R.id.title_text);
title_text.setTextSize(Common.TITLE_SIZE);
title_text.setTextColor(Color.WHITE);
title_text.setText(R.string.message_title);

// ==============content
// layout==============================================
TextView context_Tv = (TextView) layout.findViewById(R.id.content_text);
context_Tv.setTextSize(Common.TITLE_SIZE);
context_Tv.setTextColor(Color.WHITE);
context_Tv.setText(R.string.back_context);

// ==============bottom
// layout==============================================
LinearLayout bottom_layout = (LinearLayout) layout.findViewById(R.id.bottom_layout);
bottom_layout.setBackgroundColor(Color.WHITE);

Button ok = (Button) layout.findViewById(R.id.button_Ok);
Button cancel = (Button) layout.findViewById(R.id.button_Cancel);

ok.setText(R.string.button_ok);
cancel.setText(R.string.button_cancel);

ok.setTextColor(Color.BLACK);
cancel.setTextColor(Color.BLACK);

ok.setTextSize(Common.BUTTON_SIZE);
cancel.setTextSize(Common.BUTTON_SIZE);

ok.setOnClickListener(buttonClickListener);
cancel.setOnClickListener(buttonClickListener);
}


android截屏

private void printScreen(boolean save) {//截屏
View view = this.getWindow().getDecorView();//this是当前的Activity
// if (view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
Calendar c = Calendar.getInstance();
String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
// }
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
imagePath = ConstValue.MY_ALBUM_DIR + "/" + date + ".jpg";//路径
writePhotoJpg(bmp, imagePath);
// FileSaveAsync myAsync = new FileSaveAsync(bmp, imagePath, true);
// myAsync.execute();
}

保存到sd卡下的方法:(记得加上写入sd卡的权限)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>

public void writePhotoJpg(Bitmap data, String pathName) {
File file = new File(pathName);
try {
file.createNewFile();
// BufferedOutputStream os = new BufferedOutputStream(
// new FileOutputStream(file));


FileOutputStream os = new FileOutputStream(file);
data.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
MyDebug.i("writePhotoJpg");


} catch (Exception e) {
e.printStackTrace();
}
}


public void writePhotoPng(Bitmap data, String pathName) {
File file = new File(pathName);
try {
file.createNewFile();
FileOutputStream os = new FileOutputStream(file);
// BufferedOutputStream os = new BufferedOutputStream(
// new FileOutputStream(file));
data.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
MyDebug.i("writePhotoPng");


} catch (Exception e) {
e.printStackTrace();
}
}

另外几种方法,没仔细测试过:仅供参考
//抓屏的方式生成照片
  public static Bitmap printScreen(Context context) {
View view = ((Activity) context).getWindow().getDecorView();
// if (view.isDrawingCacheEnabled()) {
view.setDrawingCacheEnabled(true);
// Calendar c = Calendar.getInstance();
// String date = c.get(Calendar.YEAR) + "-" + (c.get(Calendar.MONTH) + 1) + "-" + c.get(Calendar.DAY_OF_MONTH) + "  " + c.get(Calendar.HOUR_OF_DAY) + "-" + c.get(Calendar.MINUTE) + "-" + c.get(Calendar.SECOND);
// }
view.buildDrawingCache();
Bitmap bmp = view.getDrawingCache();
return bmp;
/*imagePath = ConstValue.MY_ALBUM_DIR + "/" + date + ".jpg";
ImageFile.writePhotoJpg(bmp, imagePath);*/
// FileSaveAsync myAsync = new FileSaveAsync(bmp, imagePath, true);
// myAsync.execute();
}
         public static Bitmap catchScreen(View v1) {
       v1.setDrawingCacheEnabled(true);
       Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
       v1.setDrawingCacheEnabled(false);
       return bitmap;
          }

http://www.niftyadmin.cn/n/4472232.html

相关文章

Android APP全面屏适配技术要点

全面屏的概念 为什么先要解释一下全面屏&#xff0c;因为这个词在现在来讲就是一个伪命题。全面屏字面意思就是手机的正面全部都是屏幕&#xff0c;100%的屏占比。但是现在推出所谓“全面屏”手机的厂商没有一个能达到全面的。 那么下面来说一下Android开发领域对全面屏的理解和…

android UI进阶之自定义组合控件

今天和大家分享下组合控件的使用。很多时候android自定义控件并不能满足需求&#xff0c;如何做呢&#xff1f;很多方法&#xff0c;可以自己绘制一个&#xff0c;可以通过继承基础控件来重写某些环节&#xff0c;当然也可以将控件组合成一个新控件&#xff0c;这也是最方便的一…

2.3 打包、压缩命令和文本编辑器VI

1. 打包和压缩命令随着压缩技术的发展&#xff0c;Linux环境下提供的压缩指令和格式开始变多。为了便于用户区分不同压缩文件使用的不同压缩技术&#xff0c;进而使用合适的指令进行操作&#xff0c;一般使用后缀标识文件在压缩或打包过程中所使用的压缩技术。*.Z // compress程…

程序员怎样渡过中年危机?

真的是危机&#xff1f; 直接上图&#xff0c;这特么哪里是危机&#xff0c;明明是金矿啊。直接用人民币直接消除了所谓的中年危机&#xff1f; 但是&#xff0c;你说我明明感觉很焦虑&#xff0c;很烦躁&#xff0c;一眼就看到底了&#xff0c;我一辈子就是一个程序员&#xf…

图片上动态添加文字

android上在图片上动态添加文字的方法&#xff1a; [java] view plaincopy public Bitmap drawTextToBitmap(Context gContext, int gResId, String gText) { Resources resources gContext.getResources(); float scale resources.getDisplayMetrics().de…

BZOJ3718[PA2014]Parking——树状数组

题目描述 你的老板命令你将停车场里的车移动成他想要的样子。停车场是一个长条矩形&#xff0c;宽度为w。我们以其左下角顶点为原点&#xff0c;坐标轴平行于矩形的边&#xff0c;建立直角坐标系。停车场很长&#xff0c;我们可以认为它一直向右边伸展到无穷远处。车都是边平行…

Android控件之Chronometer(定时器)

Chronometer是一个简单的定时器&#xff0c;你可以给它一个开始时间&#xff0c;并以此定时&#xff0c;或者如果你不给它一个开始时间&#xff0c;它将会使用你的时间通话开始。默认情况下它会显示在当前定时器的值的形式“分&#xff1a;秒”或“H&#xff1a;MM&#xff1a;…

shell之删除elasticsearch30天以前的索引

在elasticsearch的运维工作中&#xff0c;由于es每天会产生大量的日志&#xff0c;如果一直保存不进行删除的话&#xff0c;再大的磁盘空间也会不够用&#xff0c;由此需要删除满足条件的index&#xff0c;从而释放磁盘空间&#xff1b;我们公司的es要求只保留30天的日志即可&a…