android-only 2 rounded corners of the ImageView

Source: Internet
Author: User

This example is for an example of converting a picture two right angles to rounded corners:



1. Customize the 3 properties of the width of the high, rounded value:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable    name= "Myphotoview" >        <attr name= "width" format= "Dimension" ></attr>        <attr name= "height" format= "Dimension" > </attr>        <attr name= "Radus" format= "Dimension" ></attr>    </declare-styleable></ Resources>

2. Using in the referenced layout

<relativelayout     xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:bc= "http// Schemas.android.com/apk/res/org.lean "    android:layout_width=" match_parent "    android:layout_height=" match _parent "  ><org.lean.myphotoview    android:id=" @+id/photo_iv "     android:layout_width=" Wrap_ Content "    android:layout_height=" wrap_content "    android:layout_margintop=" 20DP "    android:layout_ marginleft= "60DP"    bc:width= "160DP"    bc:height= "240DP"    bc:radus= "10DP"/></relativelayout >

3. Set two layers in the Java code. Here are 2 pictures that were drawn first as the target layer, then set up a mixed render draw, and then the drawn layer becomes the source layer.

The src_in mode draws the source layer (while hiding the target layer) where the two intersect, and the effect of the drawing is affected by the transparency of the target image.


Package Org.lean;import Android.annotation.suppresslint;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.bitmap;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.porterduff;import Android.graphics.porterduffxfermode;import Android.graphics.rect;import Android.graphics.rectf;import Android.graphics.drawable.bitmapdrawable;import Android.util.attributeset;import android.widget.ImageView;/** * Redraw ImageView * * @author Lean */public class Myphotoview extends Imageview{private Bitmap mimagebtp;private Rect mimagesrc Rect, mimagedesrect;private rect mbtmrect;private float mtotalwidth;private float mtotalheight;private float mRadus; Public Myphotoview (Context context,int with,int height,float Radus) {super (context); mtotalwidth=with;mtotalheight= Height;mradus=radus;} Public Myphotoview (context context, AttributeSet Attrs) {Super (context, attrs); TypedArray array = Context.obtainstyledattribuTES (Attrs, r.styleable.myphotoview); mtotalwidth = array.getdimensionpixelsize (r.styleable.myphotoview_width, 0); Mtotalheight = array.getdimensionpixelsize (r.styleable.myphotoview_height, 0); Mradus = Array.getDimensionPixelSize ( R.styleable.myphotoview_radus, 0); Array.recycle ();} @Overrideprotected void onsizechanged (int w, int h, int oldw, int oldh) {super.onsizechanged (W, H, OLDW, OLDH); mimagedesre Ct=new rect (0, 0, (int) mtotalwidth, (int) mtotalheight), Mbtmrect=new rect (0, 0, (int) mtotalwidth, (int) (mtotalheight+ Mradus));} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Paint paint=new Paint (paint.anti_alias_flag); Porterduffxfermode xfermode=new Porterduffxfermode (PorterDuff.Mode.SRC_IN); Canvas.drawcolor (color.white); int Savecount=canvas.savelayer (0,0, Mtotalwidth,mtotalheight,paint,canvas.all_save_flag); if (mImageBtp!=null) { Paint.setcolor (Color.White), Canvas.drawroundrect (New RECTF (Mbtmrect), Mradus, Mradus, paint);p Aint.setxfermode ( Xfermode); Canvas.drawbitmap (MIMAGEBTP, Mimagesrcrect,mimagedesrect,paint);p aint.setxfermode (null);} Canvas.restoretocount (Savecount);} @Overridepublic void Setimagebitmap (Bitmap bm) {Super.setimagebitmap (BM); Mimagebtp=bm;recalculaterect ();} private void Recalculaterect () {mimagesrcrect=new Rect (0, 0,mimagebtp.getwidth (), Mimagebtp.getheight ()); Invalidate ( );} @Overridepublic void Setimageresource (int resId) {super.setimageresource (resId); mimagebtp= (bitmapdrawable) Getresources (). getdrawable (ResId)). Getbitmap (); Recalculaterect ();}}





android-only 2 rounded corners of the ImageView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.