[Translate] correctly displays the ImageView of Adjustviewbounds when the APILevel is below 17
Correct the ImageView ' s adjustviewbounds behaviour on API level and below with Adjustableimageview
Original address:
http://inthecheesefactory.com/blog/correct-imageview-adjustviewbounds-with-adjustable-imageview/en
There is a need for almost every application: I want to scale a imageview so that it adapts to its parent container, what can I do? Just like this.
In fact ImageView has provided this capability, you can simply set Android:adjustviewbounds to True.
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@mipmap/ic_launcher" />
Here is the display effect:
Everything looks perfect? Actually not, if you switch your preview version to API Level17 or lower, you'll see that ImageView doesn't scale at all.
This is not a bug. The following instructions are available in the official documentation
注意:如果应用Target API level 小于等于17,adjustViewBounds会让drawable收缩到图片的边界,而不会拉伸到填充可用的测量空间。这是为了和过去的MeasureSpec 和 RelativeLayout行为保持兼容
This means that, less than or equal to API level 17, the maximum width and maximum height will be the same as the image source defined by the ANDROID:SRC, so the picture will appear as shown above.
Look at the Android platform version statistics and you will find that almost half of Android phones are less than or equal to API level 17.
Setting the Minsdkversion to 18 is obviously not a good idea to solve the problem.
Modifying ImageView's source code to make it behave consistently with API level + is obviously better, using a custom ImageView to replace the normal ImageView. The code is as follows:
ImportAndroid.content.Context;Importandroid.graphics.drawable.Drawable;ImportAndroid.util.AttributeSet;ImportAndroid.view.ViewGroup;ImportAndroid.view.ViewParent;ImportAndroid.widget.ImageView;/** * Created by Nuuneoi on 2/17/15 AD. * * Public class adjustableimageview extends ImageView { BooleanMadjustviewbounds; Public Adjustableimageview(Context context) {Super(context); } Public Adjustableimageview(context context, AttributeSet attrs) {Super(context, attrs); } Public Adjustableimageview(context context, AttributeSet attrs,intDEFSTYLEATTR) {Super(Context, attrs, defstyleattr); }@Override Public void Setadjustviewbounds(BooleanAdjustviewbounds) {madjustviewbounds = Adjustviewbounds;Super. Setadjustviewbounds (Adjustviewbounds); }@Override protected void onmeasure(intWidthmeasurespec,intHeightmeasurespec) {drawable mdrawable = getdrawable ();if(Mdrawable = =NULL) {Super. Onmeasure (Widthmeasurespec, Heightmeasurespec);return; }if(madjustviewbounds) {intMdrawablewidth = Mdrawable.getintrinsicwidth ();intMdrawableheight = Mdrawable.getintrinsicheight ();intHeightsize = Measurespec.getsize (Heightmeasurespec);intWidthsize = Measurespec.getsize (Widthmeasurespec);intHeightmode = Measurespec.getmode (Heightmeasurespec);intWidthmode = Measurespec.getmode (Widthmeasurespec);if(Heightmode = = measurespec.exactly && widthmode! = measurespec.exactly) {//Fixed Height & Adjustable Width intHeight = heightsize;intwidth = height * mdrawablewidth/mdrawableheight;if(Isinscrollingcontainer ()) setmeasureddimension (width, height);ElseSetmeasureddimension (math.min (width, widthsize), math.min (height, heightsize)); }Else if(Widthmode = = measurespec.exactly && heightmode! = measurespec.exactly) {//Fixed Width & Adjustable Height intwidth = widthsize;intHeight = width * mdrawableheight/mdrawablewidth;if(Isinscrollingcontainer ()) setmeasureddimension (width, height);ElseSetmeasureddimension (math.min (width, widthsize), math.min (height, heightsize)); }Else{Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } }Else{Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } }Private Boolean Isinscrollingcontainer() {viewparent p = getParent (); while(P! =NULL&& PinstanceofViewGroup) {if(((ViewGroup) p). Shoulddelaychildpressedstate ()) {return true; } p = P.getparent (); }return false; }}
The code works very directly, and in the Onmeasure method, if the width is fixed (stretched to the maximum allowed by the parent control), the height at the scale is calculated, and vice versa. If the Adjustableimageview object is placed in a non-scrollable container, the width height will be limited to the remaining space of the parent control, otherwise it will be infinitely enlarged.
Use Com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageView instead of ImageView in the layout XML file.
<com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:src="@mipmap/ic_launcher" />
Use the Adjustableimageview library to simplify operations
(The translator thinks that if you use eclipse or network conditions, copy the code directly into the project.)
We know to create a file, copy code, paste, reformat. Check is not correct ... These are annoying.
Using the libraries I've prepared for you to make life easier, relying on libraries currently available in Jcentor can be found once you add dependencies to the project, Adjustableimageview and Adjustableimagebutton can be used directly, and the source is uploaded to GitHub.
Here is the gradle dependency, just add the following in the Build.gradle file:
dependencies { compile ‘com.inthecheesefactory.thecheeselibrary:adjustable-imageview:1.0.0‘}
Adjustableimageview and Adjustableimagebutton can be found in the Adjustableimageview and Adjustableimagebutton packages.
You only need to use Adjustableimageview and Adjustableimagebutton to replace ImageView and ImageButton in your project.
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:layout_width = "match_parent" android:layout_height =" match_parent " android:orientation =" vertical " tools:context =; <ScrollViewandroid:layout_width="Match_parent"android:layout_height ="Match_parent"android:fillviewport="true"> <linearlayout android:layout_width="Match_parent"android:layout_ Height="Match_parent"android:orientation="vertical"> < Com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageView andr Oid:layout_width = "match_parent" android : layout_height = "wrap_content" android:a Djustviewbounds = "true" android:src= "@mipmap/ic_launcher" /> <com.inthecheesefactory.thecheeselibrary.widget.AdjustableImageViewandroid:layout_width ="Match_parent"android:layout_height="Wrap_content"android: Adjustviewbounds="true"android:src="@mipmap/ic_launcher"/> </linearlayout> </ScrollView></linearlayout>
This way, ImageView can now be zoomed in perfectly, no matter what Android version you use.
That's why we should install multiple versions of the SDK on our computers, rather than just the latest one. Once you want your Android studio to preview your layout in different versions. You will have to install that version of SDK Platform, otherwise it will not appear as a choice in your preview panel. I recommend that you install each SDK Platform starting with API level 14. Although the loss of a bit of hard disk space, but the return is worthwhile.
[Translate] correctly displays the ImageView of the adaptive boundary when the API level is below 17