ImageView's explanation
First, the difference between SRC and background
Background we generally understand the background, and SRC is the content, when using SRC to fill in the image is filled with the size of the picture, and will not be stretched, and background fill in the picture will be based on the size of the specified image to scale the stretch.
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "#ffffff"> <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@mipmap/ic_launcher"/> <ImageViewAndroid:layout_width= "200DP"Android:layout_height= "Wrap_content"Android:background= "@mipmap/ic_launcher"/> <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@mipmap/ic_launcher"/> <ImageViewAndroid:layout_width= "200DP"Android:layout_height= "Wrap_content"android:src= "@mipmap/ic_launcher"/></LinearLayout>
When we set the width and height are wrap_content when the background and SRC are the original image size, but when we set the height of the layout_height the difference is obviously visible, we will find the SRC attribute when the width of the picture is not so wide, At this time the picture will be automatically centered, and if the width and height settings can not meet the requirements of the picture when it is involved in the picture Scaled ScaleType property. At the same time, there are some other effects if the two are used together.
Two. Set the transparency of the picture
The difference between the previous two attributes, now let's talk about how to set the transparency of a picture, very simply, we just need to call the Setalpha method in Java code.
However, it is important to note that this method only works on the SRC attribute's control, and the parameter is of type float.
Adjustviewbounds Set whether the zoom maintains the length-to-width ratio of the original
The use of this property alone has no effect, we need to use maxheight and maxwidth together to work
MaxHeight: Is the maximum height of the ImageView
Maxewidth: Is the maximum width of the ImageView
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "#ffffff"> <ImageViewAndroid:id= "@+id/image1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@mipmap/women" /> <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margintop= "20DP"android:src= "@mipmap/women"Android:adjustviewbounds= "true"Android:maxheight= "200DP"Android:maxwidth= "200DP"/></LinearLayout>
Four, the image of the zoom ScaleType
Android:scaletype the size of the ImageView by setting the type of scaling or moving, we can also call ImageView in Java code
. Setscaletype (ImageView.ScaleType.CENTER) for zooming or moving, optional parameters:
FITXY: Zoom in and out of the image to fit the ImageView, but the aspect ratio may change.
Fitstart: The aspect ratio of the picture is scaled so that it can be imageview (one that adapts to the height or width of the ImageView), the aspect ratio does not change during the scaling change, and the scaled picture is placed in the upper-left corner of the ImageView.
Fitcenter: Same as Fitstart, just put the scaled picture in the middle of the imageview.
Fitend: Same as Fitstart, just put the scaled picture in the bottom right corner of the ImageView at the end.
Center: Keep the original size, display in the center of the ImageView, the size of the original image is larger than the size of the ImageView set, the picture needs to be cropped.
Centercrop: Maintain the original aspect scale until the ImageView is completely covered, the picture may appear incomplete.
Centerinside: Keep The original aspect scale until the ImageView can be fully displayed.
Matrix: Default value, do not change the size of the picture, from the upper left corner of the ImageView to draw the picture, the original image over ImageView back to the picture to be cropped.
The above explanation should be clear enough to try it out if you are interested.
Five, custom circular ImageView
PackageCom.example.test3;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PaintFlagsDrawFilter;ImportAndroid.graphics.Path;ImportAndroid.graphics.Rect;Importandroid.graphics.Region;ImportAndroid.util.AttributeSet;ImportAndroid.widget.ImageView;/*** Created by Coder-tu on 2016/1/7.*/ Public classRoundimageviewextendsimageview{PrivateBitmap Bitmap =Bitmapfactory.decoderesource (Getresources (), r.mipmap.women); PrivateRect rect =NewRect (); PrivatePaintflagsdrawfilter filter =NewPaintflagsdrawfilter (0, Paint.anti_alias_flag); PrivatePaint paint =NewPaint (); PrivatePath PATH =NewPath (); PublicRoundimageview (Context context) {Super(context); Init (); } PublicRoundimageview (Context context, AttributeSet attrs) {Super(context, attrs); Init (); } PublicRoundimageview (context context, AttributeSet attrs,intdefstyleattr) { Super(context, attrs, defstyleattr); Init (); } Private voidinit () {Paint.setstyle (Paint.Style.STROKE); Paint.setflags (Paint.anti_alias_flag);//anti-aliasingPaint.setantialias (true); }//passing in a bitmap object Public voidSetBitmap (Bitmap Bitmap) { This. Bitmap =bitmap; } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); if(Bitmap = =NULL){ return; } rect.set (0,0, GetWidth (), getheight ()); Canvas.save (); Canvas.setdrawfilter (filter); Path.addcircle (getwidth ()/2, GetWidth ()/2, GetHeight ()/2, Path.Direction.CCW); Canvas.clippath (path, Region.Op.REPLACE); Canvas.drawbitmap (Bitmap,NULL, rect, paint); Canvas.restore (); }}
Using in the layout file
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:background= "#ffffff"> <Com.example.test3.RoundImageViewAndroid:id= "@+id/image1"Android:layout_width= "200DP"Android:layout_height= "200DP"/></LinearLayout>
Android Basic Control Learning-----ImageView