<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 使用自定義ImageView實現圓形Imageview android自帶的Imageview是只能調節它的寬高,不能實現一些圓形的Imageview,而很多社交軟件的頭像或者一些小圖形都是圓形的,看起來非常美觀,接下來,我就為大家實現一個自定義的imageview,能夠實現圓形圖案。 ~~~ import android.annotation.SuppressLint; import android.content.Context; 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.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.widget.ImageView; /** * 自定義的圓形ImageView,可以直接當組件在布局中使用。 */ @SuppressLint("DrawAllocation") public class CircleImageView extends ImageView{ private Paint paint ; public CircleImageView(Context context) { this(context,null); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs,0); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); paint = new Paint(); } /** * 繪制圓形圖片 * @author se7en */ @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { Drawable drawable = getDrawable(); if (drawable != null) { Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); Bitmap b = getCircleBitmap(bitmap, 14); int z = Math.min(b.getWidth(),b.getHeight()); final Rect rectSrc = new Rect(0, 0, z , z); final Rect rectDest = new Rect(0,0,getWidth(),getHeight()); paint.reset(); canvas.drawBitmap(b, rectSrc, rectDest, paint); } else { super.onDraw(canvas); } } /** * 獲取圓形圖片方法 * @param bitmap * @param pixels * @return Bitmap * @author se7en */ private Bitmap getCircleBitmap(Bitmap bitmap, int pixels) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; int min = Math.min(bitmap.getWidth(),bitmap.getHeight()); final Rect rect = new Rect(0, 0, min , min); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(min / 2, min / 2, min / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } } ~~~ 要說明的有如下幾點: 1. ? 注意導入包的選擇,請看清楚我導的那具體的包,導錯了,我不保證能不能實現; 1. ? int z = Math.min(b.getWidth(),b.getHeight()); 這句代碼非常重要,如果你原始圖片的寬和長不相等的話,這句代碼就能保證畫出來的是一個圓。而不是那種一個圓被砍了一刀,我不知道那種圖形叫什么名字,暫且這樣描述; 1. ? ~~~ int min = Math.min(bitmap.getWidth(),bitmap.getHeight()); ~~~ ~~~ final Rect rect = new Rect(0, 0, min , min); ~~~ ~~~ canvas.drawCircle(min / 2, min / 2, min / 2, paint); 這3句代碼也是和第2點差不多,能夠保證畫出來的圓是一個填充滿的圓,而不是橢圓或者少了一點(像是被砍了一刀)的圓; ~~~ 1. ~~~ paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));這句代碼是為paint設置繪圖模式,具體含義如下: ~~~ ~~~ setXfermode設置兩張圖片相交時的模式。 在正常的情況下, ~~~ 就是在已有的圖像上繪圖將會在其上面添加一層新的形狀。如果新的Paint是完全不透明的,那么它將完全遮擋住下面的Paint;如果它是部分透明的,那么它將會被染上下面的顏色。 而setXfermode就可以來解決這個問題 . canvas原有的圖片可以理解為背景,就是dst; 新畫上去的圖片可以理解為前景,就是src。請看如下圖表: ![](https://box.kancloud.cn/2016-04-25_571e2151bff3a.jpg) 自定好了CircleImageView 后就可以在xml中使用了,用起來非常的方便,大家也可以把這個代碼封裝起來,以后直接用就可以了; xml的代碼如下: ~~~ <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ray_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/menu_bg" android:orientation="vertical" android:gravity="center" > <com.youle.bige.view.CircleImageView android:id="@+id/person_title_image" android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/zhoujielun" /> </LinearLayout> ~~~ 效果如下: 1. ![](https://box.kancloud.cn/2016-04-25_571e2151e57b8.jpg) 現在看起來就非常漂亮和美觀了;好了下班吃飯了!88
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看