今天就跟大家聊聊有關(guān)怎么在Android中利用ImageView控件實(shí)現(xiàn)一個(gè)圓角功能,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。
我們提供的服務(wù)有:成都網(wǎng)站建設(shè)、成都做網(wǎng)站、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、弋陽ssl等。為1000+企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的弋陽網(wǎng)站制作公司
1.創(chuàng)建CustomImageView 類在你的項(xiàng)目中(源碼如下)
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.Matrix; import android.graphics.Paint; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; import com.towatt.charge.towatt.R; /** * @author Mr.lynn * @version 1.0<br> * 圖片圓角實(shí)現(xiàn) */ public class CustomImageView extends android.support.v7.widget.AppCompatImageView { private Paint paint; private Paint paintBorder; private Bitmap mSrcBitmap; /** * 圓角的弧度 */ private float mRadius; private boolean mIsCircle; public CustomImageView(final Context context) { this(context, null); } public CustomImageView(Context context, AttributeSet attrs) { this(context, attrs, R.attr.customImageViewStyle); } public CustomImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0); mRadius = ta.getDimension(R.styleable.CustomImageView_radius, 0); mIsCircle = ta.getBoolean(R.styleable.CustomImageView_circle, false); int srcResource = attrs.getAttributeResourceValue( "http://schemas.android.com/apk/res/android", "src", 0); if (srcResource != 0) mSrcBitmap = BitmapFactory.decodeResource(getResources(), srcResource); ta.recycle(); paint = new Paint(); paint.setAntiAlias(true); paintBorder = new Paint(); paintBorder.setAntiAlias(true); } @Override public void onDraw(Canvas canvas) { int width = canvas.getWidth() - getPaddingLeft() - getPaddingRight(); int height = canvas.getHeight() - getPaddingTop() - getPaddingBottom(); Bitmap image = drawableToBitmap(getDrawable()); if (mIsCircle) { Bitmap reSizeImage = reSizeImageC(image, width, height); canvas.drawBitmap(createCircleImage(reSizeImage, width, height), getPaddingLeft(), getPaddingTop(), null); } else { Bitmap reSizeImage = reSizeImage(image, width, height); canvas.drawBitmap(createRoundImage(reSizeImage, width, height), getPaddingLeft(), getPaddingTop(), null); } } /** * 畫圓角 * * @param source * @param width * @param height * @return */ private Bitmap createRoundImage(Bitmap source, int width, int height) { Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(target); RectF rect = new RectF(0, 0, width, height); canvas.drawRoundRect(rect, mRadius, mRadius, paint); // 核心代碼取兩個(gè)圖片的交集部分 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source, 0, 0, paint); return target; } /** * 畫圓 * * @param source * @param width * @param height * @return */ private Bitmap createCircleImage(Bitmap source, int width, int height) { Paint paint = new Paint(); paint.setAntiAlias(true); Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(target); canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, paint); // 核心代碼取兩個(gè)圖片的交集部分 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(source, (width - source.getWidth()) / 2, (height - source.getHeight()) / 2, paint); return target; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width, height); } /** * drawable轉(zhuǎn)bitmap * * @param drawable * @return */ private Bitmap drawableToBitmap(Drawable drawable) { if (drawable == null) { if (mSrcBitmap != null) { return mSrcBitmap; } else { return null; } } else if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); drawable.draw(canvas); return bitmap; } /** * 重設(shè)Bitmap的寬高 * * @param bitmap * @param newWidth * @param newHeight * @return */ private Bitmap reSizeImage(Bitmap bitmap, int newWidth, int newHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); // 計(jì)算出縮放比 float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // 矩陣縮放bitmap Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } /** * 重設(shè)Bitmap的寬高 * * @param bitmap * @param newWidth * @param newHeight * @return */ private Bitmap reSizeImageC(Bitmap bitmap, int newWidth, int newHeight) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int x = (newWidth - width) / 2; int y = (newHeight - height) / 2; if (x > 0 && y > 0) { return Bitmap.createBitmap(bitmap, 0, 0, width, height, null, true); } float scale = 1; if (width > height) { // 按照寬度進(jìn)行等比縮放 scale = ((float) newWidth) / width; } else { // 按照高度進(jìn)行等比縮放 // 計(jì)算出縮放比 scale = ((float) newHeight) / height; } Matrix matrix = new Matrix(); matrix.postScale(scale, scale); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true); } }
2.在values目錄下創(chuàng)建attrs.xml(若是已存在該文件)直接復(fù)制如下代碼既可
<resources> <declare-styleable name="Theme"> <attr name="customImageViewStyle" format="reference" /> </declare-styleable> <!-- 自定義圓角ImageView --> <declare-styleable name="CustomImageView"> <attr name="circle" format="boolean" /> <attr name="radius" format="dimension" /> </declare-styleable> </resources>
3.正確的使用方式(在布局文件中)
注意此路徑是你控件所在包
<com.xxx.xxx.view.CustomImageView android:layout_marginTop="5dp" android:layout_width="77dp" android:layout_height="77dp" lynn:radius="@dimen/size_10dp" android:src="@drawable/position_icon" android:id="@+id/iv_build_icon" />
看完上述內(nèi)容,你們對(duì)怎么在Android中利用ImageView控件實(shí)現(xiàn)一個(gè)圓角功能有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。
當(dāng)前名稱:怎么在Android中利用ImageView控件實(shí)現(xiàn)一個(gè)圓角功能
URL鏈接:http://jinyejixie.com/article36/ppeosg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、微信小程序、面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)、網(wǎng)站收錄、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)