ImageView rounded corner and iosimageview rounded corner

Source: Internet
Author: User

ImageView rounded corner and iosimageview rounded corner

Activity_main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:CustomImageView="http://schemas.android.com/apk/res/com.example.customview05imageview"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <com.example.customview05imageview.RoundImageView            android:layout_width="wrap_content"            android:layout_margin="5dp"            android:layout_height="wrap_content"            android:src="@drawable/icon" />                <ImageView          android:id="@+id/imageview"            android:layout_margin="5dp"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/icon" />        <com.example.customview05imageview.view.CustomImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="10dp"            CustomImageView:src="@drawable/icon"            CustomImageView:type="circle" />        <com.example.customview05imageview.view.CustomImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="10dp"            CustomImageView:borderRadius="10dp"            CustomImageView:src="@drawable/icon"            CustomImageView:type="round" />        <com.example.customview05imageview.view.CustomImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_margin="10dp"            CustomImageView:borderRadius="20dp"            CustomImageView:src="@drawable/icon"            CustomImageView:type="round" />    </LinearLayout></ScrollView>


MainActivity

Package com. example. customview05imageview; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. canvas; import android. graphics. paint; import android. graphics. porterDuff. mode; import android. graphics. porterduxfermode; import android. graphics. rect; import android. graphics. rectF; import android. graphics. drawable. bitmapDrawable; import android. graphics. drawable. drawable; import android. OS. bundle; import android. widget. imageView; public class MainActivity extends Activity {private ImageView imageview; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); imageview = (ImageView) findViewById (R. id. imageview); Drawable drawable = getResources (). getDrawable (R. drawable. icon); BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; Bitmap bitmap = bitmapDrawable. getBitmap (); Bitmap roundCorner = toRoundCorner (bitmap, 30); imageview. setImageBitmap (roundCorner);} public static Bitmap toRoundCorner (Bitmap bitmap, int pixels) {// create bitmap object Bitmap output = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Config. ARGB_8888); // creates a Paint brush and Canvas canvas Canvas = new Canvas (output); final paint Paint = new Paint (); final int color = 0xff0000242; // create a rectangle-position final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight (); final RectF rectF = new RectF (rect); // rounded final float roundPx = pixels; paint. setAntiAlias (true); // transparent canvas. drawARGB (0, 0, 0, 0); paint. setColor (color); canvas. drawRoundRect (rectF, roundPx, roundPx, paint); paint. setXfermode (new porterduduxfermode (Mode. SRC_IN); canvas. drawBitmap (bitmap, rect, rect, paint); return output ;}}
CustomImageView

Package com. example. customview05imageview. view; import android. content. context; import android. content. res. typedArray; import android. graphics. bitmap; import android. graphics. bitmap. config; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. paint; import android. graphics. porterDuff; import android. graphics. porterduxfermode; import android. graphics. rectF; import Droid. util. attributeSet; import android. util. typedValue; import android. view. view; import com. example. customview05imageview. r; public class CustomImageView extends View {private int type; private static final int TYPE_CIRCLE = 0; private static final int TYPE_ROUND = 1; private Bitmap mSrc; private int mRadius; private int mWidth; private int mHeight;/*** initialize some custom parameters */public mimimageview (Context contex T, AttributeSet attrs) {this (context, attrs, 0);} public CustomImageView (Context context) {this (context, null);} public CustomImageView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. customImageView, defStyle, 0); int n =. getIndexCount (); // The traversal property for (int I = 0; I <n; I ++) {int attr =. getIndex (I); s Witch (attr) {case R. styleable. customImageView_src: mSrc = BitmapFactory. decodeResource (getResources (),. getResourceId (attr, 0); break; case R. styleable. customImageView_type: // The default value is Circle -- 0 type =. getInt (attr, 0); break; case R. styleable. customImageView_borderRadius: // a function that is converted to a standard size. The default value is 10 DIPmRadius =. getDimensionPixelSize (attr, (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 10f, getRe Sources (). getDisplayMetrics (); break;}. recycle ();}/*** calculate the height and width of the control */@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {/*** parameter: they indicate the space available for the control and the metadata setting width for the Space Description */int specMode = MeasureSpec. getMode (widthMeasureSpec); int specSize = MeasureSpec. getSize (widthMeasureSpec);/*** MeasureSpec. EXACTLY is the exact size. When we specify the layout_width or layout_height of the control as a specific value, such as andorid: layout_width = "50di P "* or FILL_PARENT is the case where the control size has been determined and the size is precise. */If (specMode = MeasureSpec. EXACTLY) // match_parent {mWidth = specSize;} else {// width int desireByImg = getPaddingLeft () + getPaddingRight () + mSrc. getWidth (); if (specMode = MeasureSpec. AT_MOST) // wrap_content {/*** at this time, as long as the control size does not exceed the maximum size allowed by the parent control */mWidth = Math. min (desireByImg, specSize);} elsemWidth = desireByImg;}/***** set height */specMode = MeasureSpec. getMode (heightMeasureSpec); specSize = MeasureSpe C. getSize (heightMeasureSpec); if (specMode = MeasureSpec. EXACTLY) // match_parent, accurate {mHeight = specSize;} else {int desire = getPaddingTop () + getPaddingBottom () + mSrc. getHeight (); if (specMode = MeasureSpec. AT_MOST) // wrap_content {mHeight = Math. min (desire, specSize);} elsemHeight = desire;} setMeasuredDimension (mWidth, mHeight);}/*** draw */@ Overrideprotected void onDraw (Canvas canvas) {swi Tch (type) {case TYPE_CIRCLE: int min = Math. min (mWidth, mHeight); // create a new Bitmap with the scaled BitmapmSrc = Bitmap. createScaledBitmap (mSrc, min, min, false); // draw a canvas at a certain position. drawBitmap (createCircleImage (mSrc, min), 0, 0, null); break; case TYPE_ROUND: canvas. drawBitmap (createRoundConerImage (mSrc), 0, 0, null); break;}/*** draw a circular image */private Bitmap createCircleImage (Bitmap source, int min) {final Paint Paint = new Paint (); paint. setAntiAlias (true); // This function creates a Bitmap target = Bitmap with a specific width, height, and color format. createBitmap (min, min, Config. ARGB_8888); // generate a Canvas canvas of the same size = new Canvas (target); // first draw the circle-original center xy coordinate and radius canvas. drawCircle (min/2, min/2, min/2, paint); // draw the intersection of two layers. Show upper layer paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_IN); // draw the image canvas. drawBitmap (source, 0, 0, paint); return target;}/*** add rounded corners based on the source image */private Bitmap createRoundConerImage (Bitmap source) {final Paint paint = new Paint (); paint. setAntiAlias (true); Bitmap target = Bitmap. createBitmap (mWidth, mHeight, Config. ARGB_8888); Canvas canvas = new Canvas (target); RectF rect = new RectF (0, 0, source. getWidth (), source. getHeight (); canvas. drawRoundRect (rect, mRadius, mRadius, paint); paint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_IN); canvas. drawBitmap (source, 0, 0, paint); return target ;}}
RoundImageView

Package com. example. customview05imageview; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. porterDuff; import android. graphics. porterduxfermode; import android. graphics. rectF; import android. util. attributeSet; import android. widget. imageView; public class RoundImageView extends ImageView {public RoundImageView (Context context, AttributeSet attrs) {super (context, attrs); init ();} public RoundImageView (Context context) {super (context); init ();} private void init () {maskPaint. setAntiAlias (true); maskPaint. setXfermode (new porterduxfermode (PorterDuff. mode. SRC_IN); // zonePaint. setAntiAlias (true); zonePaint. setColor (Color. RED); // float density = getResources (). getDisplayMetrics (). density; rect_adius = rect_adius * density;} @ Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) {super. onLayout (changed, left, top, right, bottom); int w = getWidth (); int h = getHeight (); roundRect. set (0, 0, w, h);} private final RectF roundRect = new RectF (); private float rect_adius = 20; private final Paint maskPaint = new Paint (); private final Paint zonePaint = new Paint (); @ Override public void draw (Canvas canvas) {canvas. saveLayer (roundRect, zonePaint, Canvas. ALL_SAVE_FLAG); // generate a red rounded rectangle canvas. drawRoundRect (roundRect, rect_adius, rect_adius, zonePaint); // canvas. saveLayer (roundRect, maskPaint, Canvas. ALL_SAVE_FLAG); super. draw (canvas); canvas. restore ();}}




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.