Android custom ImageView Implementation fillet picture
Yesterday to give students homework, write home page, that is, chat interface, listview item in the contact person's head is rounded, the graphical interface is as follows:
Then I'll look at the specific implementation of the fillet.
So first, I think of the first implementation scenario:
1, is to ImageView define the Shape.xml file, and then use SRC to specify the component background. So when I think about this scenario, I first learned about the SRC and background properties of ImageView.
The background is stretched according to the length and width given by the ImageView component, while SRC stores the size of the original and does not stretch. SRC is the picture content (foreground), BG is the background and can be used simultaneously.
Here I see can be used at the same time, I instantly feel ah, there is a door, then I tried it:
<imageview
Android:layout_width= "100DP"
android:layout_height= "100DP"
android:background= "@drawable/shape"
android:src= "@mipmap/img_0326"/>
Here the Shape.xml file is defined as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<shape xmlns:android= "Http://schemas.android.com/apk/res/android" >
<solid android:color= "@color/colorhuise"/>
<corners android:radius= "20DP"/>
</shape>
But the implementation is not ideal: square, angular
I tested it myself and changed the background to a picture, the effect is as follows:
It's a perfect combination of a dog's head and a circle of friends ... Haha, gossip less so obviously the conclusion here is that if you set a background image, you can use it together, but it doesn't work if the background defines the shape. So this method is not going to work, let's try the next method:
2, custom ImageView realization fillet Picture:
Here we customize a ImageView, the code is as follows:
public class Roundimageview extends ImageView {
/**
* Types of pictures, round or rounded corners
*/
private int type;
public static final int type_circle = 0;
public static final int type_round = 1;
/**
* Default value of fillet size
*/
private static final int boder_radius_default = 10;
/**
* Size of rounded corners
*/
private int Mborderradius;
/**
* Paint for drawing
*/
Private Paint Mbitmappaint;
/**
* Radius of rounded corners
*/
private int Mradius;
/**
* 3x3 matrix, mainly used to reduce magnification
*/
Private Matrix Mmatrix;
/**
* Render image, use image coloring for drawing
*/
Private Bitmapshader Mbitmapshader;
/**
* Width of view
*/
private int mwidth;
Private RECTF Mroundrect;
Public Roundimageview (context context, AttributeSet Attrs) {
Super (context, attrs);
Mmatrix = new Matrix ();
Mbitmappaint = new Paint ();
Mbitmappaint.setantialias (TRUE);
TypedArray a = Context.obtainstyledattributes (Attrs,
R.styleable.roundimageview);
Mborderradius = A.getdimensionpixelsize (
R.styleable.roundimageview_borderradius, (int) typedvalue
. Applydimension (Typedvalue.complex_unit_dip,
Boder_radius_default, Getresources ()
. Getdisplaymetrics ()));//default is 10DP
Type = A.getint (R.styleable.roundimageview_type, type_circle);//CIRCLE is the default
A.recycle ();
}
Public Roundimageview (Context context) {
This (context, NULL);
}
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
/ **
* If the type is circular, force the view to change the width of the same, whichever is the smaller value
*/
if (type = = Type_circle) {
Mwidth = Math.min (Getmeasuredwidth (), Getmeasuredheight ());
Mradius = MWIDTH/2;
Setmeasureddimension (Mwidth, mwidth);
}
}
/**
* Initialize Bitmapshader
*/
private void Setupshader () {
drawable drawable = getdrawable ();
if (drawable = = null) {
Return
}
Bitmap bmp = Drawabletobitamp (drawable);
BMP as a shader, that is, to draw BMP within a specified area
Mbitmapshader = new Bitmapshader (BMP, Tilemode.clamp, Tilemode.clamp);
float scale = 1.0f;
if (type = = Type_circle) {
Get a small value of bitmap wide or high
int bsize = Math.min (Bmp.getwidth (), Bmp.getheight ());
Scale = Mwidth * 1.0f/bsize;
} else if (type = = Type_round) {
LOG.E ("TAG",
"B ' W =" + bmp.getwidth () + "," + "b ' H ="
+ bmp.getheight ());
if (! ( Bmp.getwidth () = = GetWidth () && bmp.getheight () = = GetHeight ())) {
If the width or height of the picture does not match the width and height of the view, the scale of the image needs to be scaled; the width of the scaled picture must be greater than the width of our view, so we take a large value here;
Scale = Math.max (getwidth () * 1.0f/bmp.getwidth (),
GetHeight () * 1.0f/bmp.getheight ());
}
}
Shader's transformation matrix, which we're mainly used to zoom in or out
Mmatrix.setscale (scale, scale);
Setting the transformation matrix
Mbitmapshader.setlocalmatrix (Mmatrix);
Set shader
Mbitmappaint.setshader (Mbitmapshader);
}
@Override
protected void OnDraw (canvas canvas) {
LOG.E ("TAG", "OnDraw");
if (getdrawable () = = null) {
Return
}
Setupshader ();
if (type = = Type_round) {
Canvas.drawroundrect (Mroundrect, Mborderradius, Mborderradius,
Mbitmappaint);
} else {
Canvas.drawcircle (Mradius, Mradius, Mradius, Mbitmappaint);
Drawsomething (canvas);
}
}
@Override
protected void onsizechanged (int w, int h, int oldw, int oldh) {
Super.onsizechanged (W, H, OLDW, OLDH);
The range of rounded corners of a picture
if (type = = Type_round)
Mroundrect = new RECTF (0, 0, W, h);
}
/**
* Drawable Turn bitmap
*
* @param drawable
* @return
*/
Private Bitmap Drawabletobitamp (drawable drawable) {
if (drawable instanceof bitmapdrawable) {
bitmapdrawable BD = (bitmapdrawable) drawable;
return Bd.getbitmap ();
}
int w = drawable.getintrinsicwidth ();
int h = drawable.getintrinsicheight ();
Bitmap Bitmap = Bitmap.createbitmap (W, H, Bitmap.Config.ARGB_8888);
Canvas canvas = new canvas (bitmap);
Drawable.setbounds (0, 0, W, h);
Drawable.draw (canvas);
return bitmap;
}
private static final String state_instance = "State_instance";
private static final String State_type = "State_type";
private static final String State_border_radius = "State_border_radius";
@Override
Protected Parcelable onsaveinstancestate () {
Bundle bundle = new bundle ();
Bundle.putparcelable (State_instance, Super.onsaveinstancestate ());
Bundle.putint (State_type, TYPE);
Bundle.putint (State_border_radius, Mborderradius);
return bundle;
}
@Override
protected void Onrestoreinstancestate (parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (bundle) state;
Super.onrestoreinstancestate ((Bundle) state)
. Getparcelable (State_instance));
This.type = Bundle.getint (State_type);
This.mborderradius = Bundle.getint (State_border_radius);
} else {
Super.onrestoreinstancestate (state);
}
}
public void Setborderradius (int borderradius) {
int pxval = dp2px (Borderradius);
if (This.mborderradius! = pxval) {
This.mborderradius = Pxval;
Invalidate ();
}
}
public void SetType (int type) {
if (this.type! = type) {
This.type = type;
if (this.type! = Type_round && This.type! = type_circle) {
This.type = type_circle;
}
Requestlayout ();
}
}
public int dp2px (int dpval) {
return (int) typedvalue.applydimension (Typedvalue.complex_unit_dip,
Dpval, Getresources (). Getdisplaymetrics ());
}
}
It is also more convenient when used.
<com.zhy.view.roundimageview
Android:layout_width= "300DP"
android:layout_height= "300DP"
Android:layout_margin= "10DP"
Android:scaletype= "Centercrop"
android:src= "@drawable/img"
Map:borderradius= "60DP"
Map:type= "Round" >
</com.zhy.view.RoundImageView>
Here the effect is rounded:
Here are a few other uses: depending on the type of the above, Circle is the first round effect, the second effect is implemented for round, and the arc of the corner is controlled by the Borderradius property.
3, finished the custom, we will share a github on the classic :
Https://github.com/siyamed/android-shape-imageview
How to use:
1. Add Dependencies:
Under Build.gradle (Module:app), add:
Compile ' com.github.siyamed:android-shape-imageview:[email protected] '
Note that the minimum version to be changed to 9 or more is to be used here.
2, add after the new build, reference resources
3. Direct use of the layout file:
<com.github.siyamed.shapeimageview.roundedimageview
Android:layout_width= "300DP"
android:layout_height= "300DP"
android:src= "@drawable/qiqiu"
app:siradius= "20DP"/>
The Siradius here is equivalent to the RADIUS attribute inside the shape.xml, that is, to control the fillet radian. When used here, the resource that references the app is red, and the solution is to add a reference to it in the outermost layout:
xmlns:app= "Http://schemas.android.com/apk/res-auto"
This super-simple, the effect is realized:
Here we only show you the rectangular rounded corners, the other effects can be directly to GitHub above to invoke the implementation.
The effect of the Android round angle is realized