@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); int width; int height; switch (baseWhat){ case 0://width width = getMeasuredWidth(); height = width * yAspect / xAspect; heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); break; case 1: height = getMeasuredHeight(); width = height * xAspect / yAspect; widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); break; } super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
public void setyAspect(@IntRange(from = 1) int yAspect) { this.yAspect = yAspect; requestLayout(); } }
@Override public View createView(@NonNull ViewGroup parent, int viewType) { int radius = CompatResourceUtils.getDimensionPixelSize(parent, R.dimen.space_4); AspectRatioFrameLayout aspectRatioLinearLayout = new AspectRatioFrameLayout(parent.getContext()); aspectRatioLinearLayout.setBaseWhat(0); aspectRatioLinearLayout.setxAspect(1); aspectRatioLinearLayout.setyAspect(1); aspectRatioLinearLayout.setPadding(12, 12, 12, 12); View backgroundView = new View(parent.getContext()); backgroundView.setBackground(WindowUtils.getSelectableItemBackgroundBorderless(parent.getContext())); aspectRatioLinearLayout.addView(backgroundView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); aspectRatioLinearLayout.setBackground(DynamicDrawableFactory.cornerRectangleDrawable(0xFF3F51B5, radius)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { aspectRatioLinearLayout.setElevation(radius); } aspectRatioLinearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0)); return aspectRatioLinearLayout; }
params.rightMargin = margin; params.topMargin = margin; AspectRatioFrameLayout frameLayout = new AspectRatioFrameLayout(this); frameLayout.setxAspect(4); frameLayout.setyAspect(3); setContentView(frameLayout, params); setTitleBarTitle(getClass().getSimpleName().replace("Activity", "")); recyclerView.setItemAnimator(new DefaultItemAnimator()); adapter = new LayoutManagerAdapter(); frameLayout.addView(recyclerView, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); recyclerView.setAdapter(adapter); List<ClassItem> items = new ArrayList<>();
p1.gravity = Gravity.CENTER_HORIZONTAL; p1.topMargin = 8; tvShortName = new TextView(itemView.getContext()); tvShortName.setGravity(Gravity.CENTER); tvShortName.setTextColor(0xFF333333); initViewShapeProvider(tvShortName); tvShortName.setBackgroundColor(0xFFF2F2F2); itemView.addView(tvShortName, p1); tvComponentName = new TextView(itemView.getContext()); tvComponentName.setTextColor(Color.WHITE); tvComponentName.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM); tvComponentName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11); tvComponentName.setLines(2); itemView.addView(tvComponentName, p2); p3.topMargin = 8; p3.rightMargin = 8; dotView = new DotView(itemView.getContext()); dotView.setShape(DotView.CIRCULAR); dotView.setBackgroundColor(Color.RED); itemView.addView(dotView, p3);
public void setBaseWhat(@IntRange(from = 0, to = 1) int baseWhat) { this.baseWhat = baseWhat; requestLayout(); }
public void setxAspect(@IntRange(from = 1) int xAspect) { this.xAspect = xAspect; requestLayout(); }