Reprinted from Http://mp.weixin.qq.com/s?__biz=MzA3NTYzODYzMg==&mid=2653578233&idx=1&sn= aea773c1e815fdef910fba28d765940b&chksm= 84b3b1feb3c438e8372850a36bdcb87fdfb1ca793793a7c9598bcc792aabbb0f417b7a32c989&mpshare=1&scene=1& Srcid=1117pji4bqtwg94d2q3dl65f#wechat_redirect Introductory
This paper mainly introduces the related important methods of ImageView, and analyzes some problems which are easily confusing or baffled from the source angle.
First, correct and rational use of imageview src and background
src : For imageview original content, storing the original size, will not be stretched;
background: For the background of ImageView, it will be stretched according to the length and width given by ImageView;
In ImageView, the SRC and background properties can be set at the same time (in order to reduce drawing, the corresponding properties can be set according to the usage scene); Because Src is in the original size, if you need to scale it, you need to use Android: Scaletyle This property (ScaleType is only valid for the SRC attribute), you can also set the transparency on the background.
Second, the correct setting of ImageView transparency
Set ImageView transparency What is correct, such as directly on the Mimageview.setalpha (100), not just? ( the answer is not sure, follow-up analysis )
ImageView There are three main ways to set the transparency:
Setalpha (@FloatRange (from=0.0, to=1.0) float Alpha) (view provided )
Setalpha (int Alpha) ( already marked as @deprecated)
Setimagealpha (int alpha) (api>=16)
Where the Setimagealpha internally calls the Setalpha (int alpha) method, the View API shows thatSetalpha is mainly for image , using Setimageview, On the one hand, it is more appropriate to distinguish the Setalpha method in the view, and to do compatibility at the same time, it can only be used in api>=16 scenes.
Also, like drawable and paint, call Setalpha (int alpha)
Before this section, ask a question: Use Mimageview.setalpha (100) directly to set the transparency of the ImageView is correct?
the correct answer is :
Why is it?
In the previous introduction, it has been pointed out that ImageView in the setalpha (int alpha) method is valid for the image, want to understand the source directly read:
(1) setalpha (int alpha) method
Fig. 1 ImageView setalpha ()
(2) through the Applycolormod method, the Imageview.setalpha (int alpha) method is realized by Drawable.setalpha (int alpha).
Fig. 2 ImageView applycolormod ()
So how did the mdrawable get it:
Figure 3 ImageView constructor gets a picture of the SRC setting
Updatedrawable (drawable D) is called in setimagedrawable (drawable D), and Mdrawable is assigned in updatedrawable (drawable D):
Figure 4 Updatedrawable () method for ImageView
Now you know why sometimes imageview.setalpha (int alpha) doesn't work.
(3) Why use the Drawable.mutate () method in Applycolormod ()
direct Reference drawable.mutate () Javadoc:
make this drawable mutable. This operation cannot is reversed. a mutable drawable is guaranteed to not share it state with A NY other drawable . This is especially useful if you need to modify the properties of drawables loaded from resources. By default, all Drawables instances loaded from the same resource share a common state; If you modify the state of one instance, all the other instances would receive the same Modification.
The above explanation is clear, a drawable if the mutate () method is used, then modifications to this drawable property ( including the transparency of the set drawable ) will not be shared.
mImageView.setBackgroundDrawable(mDrawable);mImageView.getBackground().setAlpha(100);
Is there a problem with the above code?
"There is definitely no amount of transparency in this way, no problem amount, pro-trial available". If you use mdrawable as a picture resource (using the Mutate method in colordrawable) and use it in multiple places, you will find that transparency has changed elsewhere.
The code above is correctly written:
mImageView.setBackgroundDrawable(mDrawable.mutate());mImageView.getBackground().setAlpha(100);
(4) Summary
The above analysis shows that setting imageview transparency, the pit is still quite a lot, and now it is better to use the Setalpha (float alpha) provided by view.
Third, the correct setting of the prospect of ImageView (foreground)
Sometimes the design needs to cover a layer (such as gray) on the ImageView, in the face of such requirements, to distinguish between static ImageView or asynchronous ImageView (using background back packet data).
(1) Static ImageView (this scene is very few, the design can be cut graph)
Rational use of src (foreground) and background (background) can be achieved
(2) Asynchronous ImageView
This requires the prospect of using ImageView (View provides a setforeground (drawable foreground))
Figure 5 View's Setforeground () method
Figure 6 Mforegroundinfo object creation in the view's constructor
According to the above source, if ImageView to use the Setforeground () method, must be guaranteed targetsdkversion>=23.
If this is to be used in the targetsdkversion<23 situation, it must be implemented on its own, in the AFC framework Extendimageview has taken into account this situation, has implemented the Setforefround () method.
Iv. correct use of ImageView's "Android:adjustviewbounds"
Adjustviewbounds's introduction is as follows:
Set this to true if you want the ImageView to adjust their bounds to preserve the aspect ratio of its drawable.
Note:If the application targets API level or lower, Adjustviewbounds'll allow the drawable to shrink the vie W bounds, but not grow to fill available measured space in all cases. compatibility with legacy MEASURESPEC and relativelayout behavior.
Setting the maximum height of the view, which is invalid for use alone, needs to be used with setadjustviewbounds, if you want to set the image to a fixed size, and you want to maintain the picture aspect ratio, you need the following settings:
Set Setadjustviewbounds to True;
Set MaxWidth, MaxHeight;
Set settings Layout_width and Layout_height to Wrap_content
V. Correct use of ImageView's "Android:scaletype"
As previously mentioned, ImageView's "Android:scaletype" attribute is valid for SRC, as shown, the following image needs to be scaled control, the effect is as follows:
(1) Original
(2) using Scaletype.center_corp
Design is not reasonable, if the overall picture can come down a little bit good, see once scaletype, can reach such effect only fit_xy, then try
(3) using Scaletype.fit_xy
The picture did move down, but the figure was obviously stretched.
(4) Using Scaledrawable.crop_start
The Scaledrawable class is a class that specializes in handling drawable scale in the AFC framework, and provides an additional 11 cropping method based on the scaletype of ImageView:
(1)CROP_CENTER(2)CROP_START(3)CROP_END(4)FIT_CENTER(5)FIT_START(6)FIT_END(7)MATCH_WIDTH_TOP(8)MATCH_WIDTH_BOTTOM(9)MATCH_WIDTH_CENTER(10)CENTER(11)CROP_BY_PIVOT
(5) XML Settings android:scaletype= "Fitxy" property
Setting the ScaleType property in XML
Setting the Scaledrawable.crop_start property in Java code
Seeing the code above, one might find it confusing, since the Scaledrawable.crop_start property is set in the Java code, why does the XML also set "Android:scaletype=" Fitxy ", can not set or set other properties.
The answer is no, if you want to ensure that the Scaledrawable.crop_start property is set successfully, you must set the "Android:scaletype=" Fitxy in the XML for the following reasons:
1) Set ScaleType via Scaledrawable
Setscaletype () method
2) in Updatedrawmatrix () Update the purpose width (dstwidth and Dstheight)
Updatedrawmatrix () method
As you can see, if you want the Scaledrawable.crop_start property setting to work, the getbounds () method gets must be accurate.
3) by looking at the Configbounds () method in ImageView, if you want to use drawable and vwidth in cases where Dwith and dheight (the width height of the original vheight) are not 0, Then the ImageView ScaleType must be set to Scaletype.fit_xy
Configbounds () method
Summarize:
1. When setting the transparency
Mfirstimageview.setalpha (50);
Msecondview.setalpha (+); Android:background settings are not effective
Msecondview.setimagealpha (+); Android:background settings are not effective
Msecondview.getbackground (). Setalpha (); Android:background settings are effective
Android ImageView Use posture correctly