ImageView圓角,iosimageview圓角

來源:互聯網
上載者:User

ImageView圓角,iosimageview圓角

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.PorterDuffXfermode;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) {  //建立bitmap對象        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),                  bitmap.getHeight(), Config.ARGB_8888);          //建立畫筆、畫布        Canvas canvas = new Canvas(output);          final Paint paint = new Paint();          final int color = 0xff424242;          //建立矩形-位置        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());          final RectF rectF = new RectF(rect);          //圓角        final float roundPx = pixels;          paint.setAntiAlias(true);         //透明        canvas.drawARGB(0, 0, 0, 0);          paint.setColor(color);                  canvas.drawRoundRect(rectF, roundPx, roundPx, paint);          paint.setXfermode(new PorterDuffXfermode(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.PorterDuffXfermode;import android.graphics.RectF;import android.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;/** * 初始化一些自訂的參數 */public CustomImageView(Context context, 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 = a.getIndexCount();// 遍曆屬性for (int i = 0; i < n; i++) {int attr = a.getIndex(i);switch (attr) {case R.styleable.CustomImageView_src:mSrc = BitmapFactory.decodeResource(getResources(),a.getResourceId(attr, 0));break;case R.styleable.CustomImageView_type:// 預設為Circle--0type = a.getInt(attr, 0);break;case R.styleable.CustomImageView_borderRadius:// 轉變為標準尺寸的一個函數-- 預設為10DIPmRadius = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f,getResources().getDisplayMetrics()));break;}}a.recycle();}/** * 計算控制項的高度和寬度 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {/** * 參數:它們指明控制項可獲得的空間以及關於這個空間描述的中繼資料 設定寬度 */int specMode = MeasureSpec.getMode(widthMeasureSpec);int specSize = MeasureSpec.getSize(widthMeasureSpec);/** * MeasureSpec.EXACTLY是精確尺寸,當我們將控制項的layout_width或layout_height指定為具體數值時如andorid:layout_width="50dip" * 或者為FILL_PARENT是,都是控制項大小已經確定的情況,都是精確尺寸。 */if (specMode == MeasureSpec.EXACTLY)// match_parent{mWidth = specSize;} else {// 由圖片決定的寬int desireByImg = getPaddingLeft() + getPaddingRight()+ mSrc.getWidth();if (specMode == MeasureSpec.AT_MOST)// wrap_content{/** * 此時控制項尺寸只要不超過父控制項允許的最大尺寸即可 */mWidth = Math.min(desireByImg, specSize);} elsemWidth = desireByImg;}/*** * 設定高度 */specMode = MeasureSpec.getMode(heightMeasureSpec);specSize = MeasureSpec.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);}/** * 繪製 */@Overrideprotected void onDraw(Canvas canvas) {switch (type) {case TYPE_CIRCLE:int min = Math.min(mWidth, mHeight);// Bitmap 建立 一個新的,縮放後的 BitmapmSrc = Bitmap.createScaledBitmap(mSrc, min, min, false);//在某個位置畫圓canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);break;case TYPE_ROUND:canvas.drawBitmap(createRoundConerImage(mSrc), 0, 0, null);break;}}/** * 根據原圖和變長繪製圓形圖片 */private Bitmap createCircleImage(Bitmap source, int min) {final Paint paint = new Paint();paint.setAntiAlias(true);//該函數建立一個帶有特定寬度、高度和顏色格式的位元影像Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);//產生一個同樣大小的畫布Canvas canvas = new Canvas(target);//首先繪製圓形-原心xy座標以及半徑canvas.drawCircle(min / 2, min / 2, min / 2, paint);//取兩層繪製交集。顯示上層paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//繪製圖片canvas.drawBitmap(source, 0, 0, paint);return target;}/** * 根據原圖添加圓角 */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 PorterDuffXfermode(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.PorterDuffXfermode;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 PorterDuffXfermode(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);        //產生一個紅色的圓角矩形        canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);        //        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);        super.draw(canvas);        canvas.restore();    } }




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.