下面列出了android.widget.LinearLayout#measure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Bitmap getBitmapFromView(String title, int dotBg) {
LinearLayout llmarker = (LinearLayout) findViewById(R.id.ll_marker);
TextView markerImageView = (TextView) findViewById(R.id.tv_title);
markerImageView.setText(title);
View dot = (View) findViewById(R.id.dot_marker);
dot.setBackgroundResource(dotBg);
llmarker.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
llmarker.layout(0, 0, llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight());
llmarker.draw(canvas);
return bitmap;
}
public Bitmap getBitmapFromView(String title, int dotBg) {
LinearLayout llmarker = (LinearLayout) findViewById(R.id.ll_marker);
TextView markerImageView = (TextView) findViewById(R.id.tv_title);
markerImageView.setText(title);
View dot = (View) findViewById(R.id.dot_marker);
dot.setBackgroundResource(dotBg);
llmarker.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
llmarker.layout(0, 0, llmarker.getMeasuredWidth(), llmarker.getMeasuredHeight());
llmarker.draw(canvas);
return bitmap;
}
public ShowcaseViewBuilder addCustomView(int layoutId, int gravity) {
View view = LayoutInflater.from(mActivity).inflate(layoutId, null);
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.addView(view);
linearLayout.setGravity(Gravity.CENTER);
DisplayMetrics metrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Rect rect = new Rect();
rect.set(0, 0, metrics.widthPixels, metrics.heightPixels);
int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY);
linearLayout.measure(widthSpec, heightSpec);
mCustomView.add(linearLayout);
mCustomViewGravity.add(gravity);
mCustomViewLeftMargins.add(0f);
mCustomViewTopMargins.add(0f);
mCustomViewRightMargins.add(0f);
mCustomViewBottomMargins.add(0f);
return this;
}
public ShowcaseViewBuilder addCustomView(View view, int gravity) {
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.addView(view);
linearLayout.setGravity(Gravity.CENTER);
DisplayMetrics metrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Rect rect = new Rect();
rect.set(0, 0, metrics.widthPixels, metrics.heightPixels);
int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY);
linearLayout.measure(widthSpec, heightSpec);
mCustomView.add(linearLayout);
mCustomViewGravity.add(gravity);
mCustomViewLeftMargins.add(0f);
mCustomViewTopMargins.add(0f);
mCustomViewRightMargins.add(0f);
mCustomViewBottomMargins.add(0f);
return this;
}
public ShowcaseViewBuilder addCustomView(int layoutId, int gravity, float leftMargin, float topMargin, float rightMargin, float bottomMargin) {
View view = LayoutInflater.from(mActivity).inflate(layoutId, null);
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.addView(view);
linearLayout.setGravity(Gravity.CENTER);
DisplayMetrics metrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Rect rect = new Rect();
rect.set(0, 0, metrics.widthPixels, metrics.heightPixels);
int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY);
linearLayout.measure(widthSpec, heightSpec);
mCustomView.add(linearLayout);
mCustomViewGravity.add(gravity);
mCustomViewLeftMargins.add(leftMargin);
mCustomViewTopMargins.add(topMargin);
mCustomViewRightMargins.add(rightMargin);
mCustomViewBottomMargins.add(bottomMargin);
return this;
}
public ShowcaseViewBuilder addCustomView(View view, int gravity, float leftMargin, float topMargin, float rightMargin, float bottomMargin) {
LinearLayout linearLayout = new LinearLayout(mActivity);
linearLayout.addView(view);
linearLayout.setGravity(Gravity.CENTER);
DisplayMetrics metrics = new DisplayMetrics();
mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Rect rect = new Rect();
rect.set(0, 0, metrics.widthPixels, metrics.heightPixels);
int widthSpec = View.MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY);
int heightSpec = View.MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY);
linearLayout.measure(widthSpec, heightSpec);
mCustomView.add(linearLayout);
mCustomViewGravity.add(gravity);
mCustomViewLeftMargins.add(leftMargin);
mCustomViewTopMargins.add(topMargin);
mCustomViewRightMargins.add(rightMargin);
mCustomViewBottomMargins.add(bottomMargin);
return this;
}
private void createBottomLogoLayout() {
mLL = new LinearLayout(EventDetailActivity.this);
mLL.setBackgroundColor(ContextCompat.getColor(EventDetailActivity.this, R.color.window_background));
ViewGroup.LayoutParams LLParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
mLL.setLayoutParams(LLParams);
mLL.setOrientation(LinearLayout.HORIZONTAL);
mLL.setPadding(24, 0, 0, 0);
mLL.setGravity(Gravity.CENTER_VERTICAL);
ImageView icon = new ImageView(EventDetailActivity.this);
icon.setScaleType(ImageView.ScaleType.CENTER_CROP);
icon.setImageDrawable(ContextCompat.getDrawable(EventDetailActivity.this, R.mipmap.ic_launcher));
mLL.addView(icon);
TextView hb = new TextView(EventDetailActivity.this);
hb.setText("@心动小分队");
mLL.addView(hb);
mLL.measure(View.MeasureSpec.makeMeasureSpec(mWidth, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mHeight += mLL.getMeasuredHeight();
}
/**
* 显示底部弹出菜单
*/
private void showPopDialog() {
LayoutInflater inflater = LayoutInflater.from(this);
mCameradialog = new Dialog(this, R.style.BottomDialog);
LinearLayout root = (LinearLayout) inflater.from(this).inflate(R.layout.pop_menu,null);
root.findViewById(R.id.takePic).setOnClickListener(this);
root.findViewById(R.id.takeGallery).setOnClickListener(this);
root.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCameradialog.cancel();
}
});
mCameradialog.setContentView(root);
Window dialogWindow = mCameradialog.getWindow();
dialogWindow.setGravity(Gravity.BOTTOM);
// dialogWindow.setWindowAnimations(R.style.dialogstyle); // 添加动画
WindowManager.LayoutParams lp = dialogWindow.getAttributes(); // 获取对话框当前的参数值
lp.x = 0; // 新位置X坐标
lp.y = 0; // 新位置Y坐标
lp.width = (int) getResources().getDisplayMetrics().widthPixels; // 宽度
root.measure(0, 0);
lp.height = root.getMeasuredHeight();
lp.alpha = 9f; // 透明度
dialogWindow.setAttributes(lp);
mCameradialog.show();
}
private void createFloatView() {
wmParams = new WindowManager.LayoutParams();
mWindowManager = (WindowManager) getApplication().getSystemService(android.app.Application.WINDOW_SERVICE);
wmParams.type = LayoutParams.TYPE_SYSTEM_ALERT;// 设置window
// type为TYPE_SYSTEM_ALERT
wmParams.format = PixelFormat.RGBA_8888;// 设置图片格式,效果为背景透明
wmParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE;// 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
wmParams.gravity = Gravity.LEFT | Gravity.TOP;// 默认位置:左上角
wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
wmParams.x = (WidgetUtils.getScreenWidth(getApplicationContext()) - wmParams.width) / 2;// 设置x、y初始值,相对于gravity
wmParams.y = 10;
// 获取浮动窗口视图所在布局
LayoutInflater inflater = LayoutInflater.from(getApplication());
mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout, null);
mWindowManager.addView(mFloatLayout, wmParams);// 添加mFloatLayout
mFloatView = mFloatLayout.findViewById(R.id.float_id);
mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// 设置监听浮动窗口的触摸移动
mFloatView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth() / 2;
// Log.i(TAG, "RawX" + event.getRawX());
// Log.i(TAG, "X" + event.getX());
wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight() / 2 - 25;// 减25为状态栏的高度
// Log.i(TAG, "RawY" + event.getRawY());
// Log.i(TAG, "Y" + event.getY());
mWindowManager.updateViewLayout(mFloatLayout, wmParams);// 刷新
return false; // 此处必须返回false,否则OnClickListener获取不到监听
}
});
mFloatView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
}
});
}
private ScrollView createDropDownView() {
final LinearLayout dropdownView = new LinearLayout(getContext());
ScrollView scrollView = new ScrollView(getContext());
int clickableChildCounter = 0;
dropdownView.setOrientation(LinearLayout.VERTICAL);
int height = (int) (itemHeight * bootstrapSize);
LayoutParams childParams = new LayoutParams(LayoutParams.MATCH_PARENT, height);
for (String text : dropdownData) {
TextView childView = new TextView(getContext());
childView.setGravity(Gravity.CENTER_VERTICAL);
childView.setLayoutParams(childParams);
int padding = (int) (baselineItemLeftPadding * bootstrapSize);
childView.setPadding(padding, 0, padding, 0);
childView.setTextSize(baselineDropDownViewFontSize * bootstrapSize);
childView.setTextColor(ColorUtils.resolveColor(android.R.color.black, getContext()));
Drawable background = getContext().obtainStyledAttributes(null, new int[]{
android.R.attr.selectableItemBackground}, 0, 0)
.getDrawable(0);
ViewUtils.setBackgroundDrawable(childView, background);
childView.setTextColor(BootstrapDrawableFactory.bootstrapDropDownViewText(getContext()));
childView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dropdownWindow.dismiss();
if (onDropDownItemClickListener != null) {
onDropDownItemClickListener.onItemClick(dropdownView, v, v.getId());
}
}
});
if (Pattern.matches(SEARCH_REGEX_HEADER, text)) {
childView.setText(text.replaceFirst(REPLACE_REGEX_HEADER, ""));
childView.setTextSize((baselineDropDownViewFontSize - 2F) * bootstrapSize);
childView.setClickable(false);
childView.setTextColor(ColorUtils.resolveColor(R.color.bootstrap_gray_light,
getContext()));
}
else if (Pattern.matches(SEARCH_REGEX_SEPARATOR, text)) {
childView = new DividerView(getContext());
childView.setClickable(false);
childView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 3));
}
else if (Pattern.matches(SEARCH_REGEX_DISABLED, text)) {
childView.setEnabled(false);
childView.setId(clickableChildCounter++);
childView.setText(text.replaceFirst(REPLACE_REGEX_DISABLED, ""));
}
else {
childView.setText(text);
childView.setId(clickableChildCounter++);
}
dropdownView.addView(childView);
}
dropdownView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
dropDownViewHeight = dropdownView.getMeasuredHeight();
dropDownViewWidth = dropdownView.getMeasuredWidth();
scrollView.setVerticalScrollBarEnabled(false);
scrollView.setHorizontalScrollBarEnabled(false);
scrollView.addView(dropdownView);
cleanData();
return scrollView;
}