Android dialog 通过背景bitmap的像素宽度进行定位显示

主要是记录一下,显示的Dialog背景带有尖角,尖角位置的显示;

通过Bitmap的getWidth()方法获取位图的像素宽度;然后遍历查询,通过getPixel(int x, int y)方法,判断返回的color色值,是否为0,判断尖角的位置。

贴代码;

@SuppressLint("NewApi")
public class BarHome extends AbsBarUtil {

	@BindView(id = R.id.ghlj, click = true)
	private ImageView mIv;

	private Dialog mDialog;

	private int mIvCenterX;

	private static int mArrowSelfLeftX;

	private int mBarHeight;

	private View mContent;

	private int mContentWidth;

	public BarHome() {
		super();
		mContent = LayoutInflater.from(mIv.getContext()).inflate(
				R.layout.item_pop_win_ghlj, null);
		mContent.findViewById(R.id.ll_ihu).setOnClickListener(this);
		mContent.findViewById(R.id.ll_qyy).setOnClickListener(this);
		mContent.findViewById(R.id.ll_aljk).setOnClickListener(this);
		mContent.findViewById(R.id.ll_wxgh).setOnClickListener(this);
		mContent.findViewById(R.id.ll_zfbgh).setOnClickListener(this);
		mContent.addOnLayoutChangeListener(new OnLayoutChangeListener() {

			@Override
			public void onLayoutChange(View v, int left, int top, int right,
					int bottom, int oldLeft, int oldTop, int oldRight,
					int oldBottom) {
				mContentWidth = mContent.getMeasuredWidth();
				if (mArrowSelfLeftX == 0) {
					Drawable d = mContent.getBackground();
					Bitmap bmp = ImageUtil.drawable2Bitmap(d);
					int bmpW = bmp.getWidth();
					int oarrowx = getX(bmp);
					int delta = bmpW - oarrowx;
					mArrowSelfLeftX = mContentWidth - delta;
					mDialog.getWindow().getAttributes().x = mIvCenterX
							- mArrowSelfLeftX;
					if (mDialog.isShowing()) {
						mDialog.dismiss();
					}
					mDialog.show();
				}
			}
		});
		mIv.addOnLayoutChangeListener(new OnLayoutChangeListener() {

			@Override
			public void onLayoutChange(View v, int left, int top, int right,
					int bottom, int oldLeft, int oldTop, int oldRight,
					int oldBottom) {
				int width = mIv.getMeasuredWidth() / 2;
				mIvCenterX = mIv.getLeft() + width;
				mBarHeight = getBarView().getMeasuredHeight();

				if (mDialog == null) {
					mDialog = DialogAssistant.getCustomDialog(mContent,
							mIvCenterX - mArrowSelfLeftX, mBarHeight);
					// mDialog.show();
				}
			}
		});

	}

	private String getUrl(Site site) {
		String url = null;
		if (site != null) {
			url = site.getUrl();
		}
		return url;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.actionbar_homepage1;
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		if (mDialog.isShowing()) {
			mDialog.dismiss();
		} else {
			if (R.id.ghlj == v.getId()) {
				mDialog.show();
			}
		}

		Site site = null;
		switch (v.getId()) {
		case R.id.ll_ihu:
			site = SiteUtil.getInstance().getIcallAndroid();
			SystemUtil.startApp("com.sinldo.aihu",
					"com.sinldo.aihu.ui.LauncherUI", getUrl(site));

			break;
		case R.id.ll_qyy:
			site = SiteUtil.getInstance().getBoringHospitalAndroid();
			SystemUtil.startApp("com.kingyee.kymh", "com.kingyee.kymh.KYMH",
					getUrl(site));

			break;
		case R.id.ll_aljk:
			site = SiteUtil.getInstance().getAliHealthAndroid();
			SystemUtil.startApp("com.citic21.user",
					"com.taobao.alijk.activity.GuideActivity", getUrl(site));

			break;
		case R.id.ll_wxgh:
			site = SiteUtil.getInstance().getWeChatAndroid();
			SystemUtil.startApp("com.tencent.mm",
					"com.tencent.mm.ui.LauncherUI", getUrl(site));

			break;
		case R.id.ll_zfbgh:
			site = SiteUtil.getInstance().getAliPayAndroid();
			SystemUtil.startApp("com.eg.android.AlipayGphone",
					"com.eg.android.AlipayGphone.AlipayLogin", getUrl(site));

			break;
		default:
			break;
		}
	}

	private int getX(Bitmap bmp) {
		int pos = 0;
		int len = bmp.getWidth();
		for (int i = 0; i < len; i++) {
			int color = bmp.getPixel(i, 1);
			if (color != 0) {
				pos = i;
				break;
			}
		}
		return pos;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值