Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the aspect ratio of Android control

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to set the aspect ratio of Android control". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to set the aspect ratio of the Android control.

0. A problem that has been perplexing for a long time

The width and height of the Android control is proportional, which has been a requirement that I have encountered ever since I came into contact with Android. In the past, either width and height were set directly in the code, or custom controls were used. There is also an open source custom ViewGroup on the Internet, which allows its child View to easily set the ratio of width to height. However, these implementation methods are still troublesome and not intuitive enough. Until we have DataBinding, we can easily add custom properties to the control and set the aspect ratio of the control in the layout file.

1. How to realize

Bind the following method for all View via BinderAdapter, which is called when the widthHeightRatio property is set. This is a bit of an AOP, and we deal with all the View.

Public class DataBindingAdapters {/ / set height @ BindingAdapter ("widthHeightRatio") public static void setWidthHeightRatio (final View view, final float ratio) {view.getViewTreeObserver () .addOnGlobalLayoutListener (new ViewTreeObserver.OnGlobalLayoutListener () {@ Override public void onGlobalLayout () {int height = view.getHeight (); if (height > 0) {view.getLayoutParams (). Width = (int) (height * ratio) View.invalidate (); view.getViewTreeObserver (). RemoveGlobalOnLayoutListener (this);})

After we get the height of the control, we calculate the width according to the scale, and then set it to the control. OnGlobalLayoutListener is registered here because the height of the control may not have been calculated yet. After getting the height, remove the listener to avoid unnecessary calls.

Then we can set the aspect ratio directly in the layout file. The layout file must use DataBinding, that is, the outermost layout tag. The property value must be added @ {}, otherwise it will be treated as a normal property, our method will not be called, and an error will be reported when compiling because the property cannot be found. Of course, this property can only calculate the width based on the height, and if you want to calculate the height based on the width, you can add another property in the same way.

two。 A brief Analysis of principle

In fact, there are no attributes we added in the compiled layout file (the compiled layout file can be seen under build/intermediates/data-binding-layout-out). When you really set this property, you call our bound method directly in the Java code. In the Binding class automatically generated by DataBinding, you can find calls like the following.

The copy code is as follows:

DataBindingAdapters.setWidthHeightRatio (this.mboundView0, cardWidthHeightRatio)

Here is just a simple explanation, as for when the opportunity to trigger this line of code, please look forward to my follow-up article.

3. Other wonderful uses of BinderAdapter

ImageView automatically loads network pictures

@ BindingAdapter ({"android:src", "error"}) public static void setImageUrl (ImageView view, String url, @ DrawableRes int errorImage) {if (! TextUtils.isEmpty (url)) {/ / packaged image loading tool ImageManager.from (view.getContext ()) .displayImage (view, url, errorImage);} else {view.setImageResource (errorImage);}}

Set the url of the picture to be loaded directly in the layout file, and the default image to be displayed when loading fails.

At this point, I believe you have a deeper understanding of "how to set the aspect ratio of Android controls". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report