Android-only 2 rounded imageviews and android-ImageView

Source: Internet
Author: User

Android-only 2 rounded imageviews and android-ImageView

This example is an example of converting two right angles of an image into a rounded corner:



1. Customize the width and height of the three attributes and the rounded corner 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. Use it 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 Java code. Here there are two pictures first drawn called the target layer, then set the mixed rendering to draw, and then the drawn layer becomes the source layer.

In SRC_IN mode, the source layer is drawn at the intersection of the two layers (and the target layer is hidden at the same time), and the rendering effect is affected by the transparency of the corresponding place 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. porterduxfermode; import android. graphics. rect; import android. graphics. rectF; import android. gr Aphics. drawable. bitmapDrawable; import android. util. attributeSet; import android. widget. imageView;/*** re-painted ImageView ** @ author Lean */public class MyPhotoView extends ImageView {private Bitmap mImageBtp; private Rect mImageSrcRect, 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 = ar Ray. 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); mImageDesRect = 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 pain T = new Paint (Paint. ANTI_ALIAS_FLAG); porterduxfermode xfermode = new porterduxfermode (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); paint. setXfermode (xfermode); canvas. drawBitmap (mImageBtp, mImageSrcRect, mImageDesRect, paint); paint. 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 ();}}





Related Article

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.