Android程式設計-圓形圖片的實現

來源:互聯網
上載者:User

標籤:

在android中,google只提供了對圖片的圓形操作,而沒有實現對圖片的圓形操作,所以我們無法實現上述操作,在此我們將使用架構進行設計(下述架構為as編寫):

https://github.com/monsterLin/RoundedImageView

https://github.com/pungrue26/SelectableRoundedImageView
https://github.com/hdodenhof/CircleImageView
https://github.com/MostafaGazar/CustomShapeImageView
https://github.com/siyamed/android-shape-imageview

下面我們通過使用RoundedImageView來是實現這種效果:

 

首先我們在項目中匯入開源架構:

 

匯入成功後-

第一種方式:直接書寫xml檔案:

<com.makeramen.roundedimageview.RoundedImageView        xmlns:app="http://schemas.android.com/apk/res-auto"        android:id="@+id/imageView1"        android:layout_width="40dp"        android:layout_height="40dp"        android:src="@drawable/photo"        app:riv_corner_radius="30dip"        app:riv_border_width="2dip"        app:riv_border_color="#333333"        app:riv_mutate_background="true"        app:riv_oval="true" />

 

第二種方式:通過代碼實現:

public static class RoundedDrawable extends Drawable {        protected final float cornerRadius;        protected final int margin;        protected final RectF mRect = new RectF(),                mBitmapRect;        protected final BitmapShader bitmapShader;        protected final Paint paint;        public RoundedDrawable(Bitmap bitmap, int cornerRadius, int margin) {            this.cornerRadius = cornerRadius;            this.margin = margin;            bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);            mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);                        paint = new Paint();            paint.setAntiAlias(true);            paint.setShader(bitmapShader);        }        @Override        protected void onBoundsChange(Rect bounds) {            super.onBoundsChange(bounds);            mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);                        // Resize the original bitmap to fit the new bound            Matrix shaderMatrix = new Matrix();            shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);            bitmapShader.setLocalMatrix(shaderMatrix);                    }        @Override        public void draw(Canvas canvas) {            canvas.drawRoundRect(mRect, cornerRadius, cornerRadius, paint);        }        @Override        public int getOpacity() {            return PixelFormat.TRANSLUCENT;        }        @Override        public void setAlpha(int alpha) {            paint.setAlpha(alpha);        }        @Override        public void setColorFilter(ColorFilter cf) {            paint.setColorFilter(cf);        }    }imageAware.setImageDrawable(new RoundedDrawable(bitmap, cornerRadius, margin));

 

附錄:

eclipse的開源架構:http://files.cnblogs.com/files/boy1025/roundedimageview.zip

Android程式設計-圓形圖片的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.