Android自定义对话框

代码: 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);
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值