会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
andriod2012
博客园
首页
新随笔
联系
订阅
管理
自定义对话框
代码: 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); }
posted @
2012-03-23 10:49
andriod2012
阅读(
265
) 评论(
0
)
编辑
收藏
举报
刷新页面
返回顶部
公告