精华内容
下载资源
问答
  • Android圆形图片

    2018-09-25 19:41:32
    Android圆形图片处理工具类jar包,导入项目可直接使用
  • android圆形图片

    2016-06-29 14:42:42
    android开源系列:CircleImageView自定义圆形控件的使用
  • Android 圆形图片

    2018-11-08 21:15:58
    圆形头像:CircleImageView的使用 注:在build.gradle中添加: implementation ‘de.hdodenhof:circleimageview:1.3.0’ XML布局文件: <?xml version="1.0&...

    **绝对布局:通过直接给定控件起始坐标 ( x , y ) 和 ( w , l ) ,来生成控件。
    圆形头像:CircleImageView的使用 **

    注:在build.gradle中添加:

    implementation 'de.hdodenhof:circleimageview:1.3.0'

    XML布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout
        android:id="@+id/root"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Home"
        android:layout_gravity="center">
        <!--定义一个文本框用于存放头像,使用绝对布局-->
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/imageview"
            android:layout_x="150dp"
            android:layout_y="75dp"
            android:layout_width="100dp"
            android:layout_height="100dp"/>
        <!--定义一个文本框,使用绝对定位-->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="20dp"
            android:layout_y="225dp"
            android:text="用户名:"/>
        <!--定义一个文本编辑框,使用绝对定位-->
        <EditText
            android:layout_x="80dp"
            android:layout_y="215dp"
            android:hint="邮箱/手机/用户名"
            android:layout_width="wrap_content"
            android:width="275dp"
            android:layout_height="wrap_content"
            android:singleLine="true" />
        <!--定义一个文本框使用绝对定位-->
        <TextView
            android:layout_x="20dp"
            android:layout_y="285dp"
            android:text=" 密 码 :"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <!--定义一个文本编辑框,使用绝对定位-->
        <EditText
            android:layout_x="80dp"
            android:layout_y="275dp"
            android:hint="密码/验证码"
            android:layout_width="wrap_content"
            android:width="275dp"
            android:layout_height="wrap_content"
            android:password="true"
            android:singleLine="true" />
        <!--定义一个按钮,使用绝对定位-->
        <Button
            android:layout_x="100dp"
            android:layout_y="350dp"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text=" 登 录 "/>
    
    </AbsoluteLayout>

    Java代码 动态设置头像:

    //点击 切换图片

    public class Home extends AppCompatActivity {
        private LinearLayout mainLayout=null;
        private ImageView iv=null;
        //定义一个访问图片的数组
        int[] images = new int[]{//放置你的图片
                R.drawable.gass,
                R.drawable.gonzhixiaochou
        };
        //用于图片切换
        int currenImg = 0;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);//显示manLayout
            //创建CirImageView组件
            final CircleImageView circleimageView01 = (CircleImageView) findViewById(R.id.imageview);
            //设置CirImageView背景
            circleimageView01.setImageResource(images[0]);
            circleimageView01.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //切换背景
                    circleimageView01.setImageResource(images[++currenImg % images.length]);
                }
            });
        }
    }

    效果:

    展开全文
  • android 圆形图片

    2015-04-13 15:09:57
    import com.siso.dy.buyenough.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BitmapShader; import android.graphics.Ca
    import com.siso.dy.buyenough.R;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.graphics.Shader;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.ColorDrawable;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    public class CircleImageView extends ImageView {
    
        private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
    
        private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
        private static final int COLORDRAWABLE_DIMENSION = 1;
    
        private static final int DEFAULT_BORDER_WIDTH = 0;
        private static final int DEFAULT_BORDER_COLOR = Color.WHITE;
    
        private final RectF mDrawableRect = new RectF();
        private final RectF mBorderRect = new RectF();
    
        private final Matrix mShaderMatrix = new Matrix();
        private final Paint mBitmapPaint = new Paint();
        private final Paint mBorderPaint = new Paint();
    
        private int mBorderColor = DEFAULT_BORDER_COLOR;
        private int mBorderWidth = DEFAULT_BORDER_WIDTH;
    
        private Bitmap mBitmap;
        private BitmapShader mBitmapShader;
        private int mBitmapWidth;
        private int mBitmapHeight;
    
        private float mDrawableRadius;
        private float mBorderRadius;
    
        private boolean mReady;
        private boolean mSetupPending;
    
        public CircleImageView(Context context) {
            super(context);
        }
    
        public CircleImageView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            super.setScaleType(SCALE_TYPE);
    
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle, 0);
    
            mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
            mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);
    
            a.recycle();
    
            mReady = true;
    
            if (mSetupPending) {
                setup();
                mSetupPending = false;
            }
        }
    
        @Override
        public ScaleType getScaleType() {
            return SCALE_TYPE;
        }
    
        @Override
        public void setScaleType(ScaleType scaleType) {
            if (scaleType != SCALE_TYPE) {
                throw new IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));
            }
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            if (getDrawable() == null) {
                return;
            }
    
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);
    //        canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);
        }
    
        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
            setup();
        }
    
        public int getBorderColor() {
            return mBorderColor;
        }
    
        public void setBorderColor(int borderColor) {
            if (borderColor == mBorderColor) {
                return;
            }
    
            mBorderColor = borderColor;
            mBorderPaint.setColor(mBorderColor);
            invalidate();
        }
    
        public int getBorderWidth() {
            return mBorderWidth;
        }
    
        public void setBorderWidth(int borderWidth) {
            if (borderWidth == mBorderWidth) {
                return;
            }
    
            mBorderWidth = borderWidth;
            setup();
        }
    
        @Override
        public void setImageBitmap(Bitmap bm) {
            super.setImageBitmap(bm);
            mBitmap = bm;
            setup();
        }
    
        @Override
        public void setImageDrawable(Drawable drawable) {
            super.setImageDrawable(drawable);
            mBitmap = getBitmapFromDrawable(drawable);
            setup();
        }
    
        @Override
        public void setImageResource(int resId) {
            super.setImageResource(resId);
            mBitmap = getBitmapFromDrawable(getDrawable());
            setup();
        }
    
        private Bitmap getBitmapFromDrawable(Drawable drawable) {
            if (drawable == null) {
                return null;
            }
    
            if (drawable instanceof BitmapDrawable) {
                return ((BitmapDrawable) drawable).getBitmap();
            }
    
            try {
                Bitmap bitmap;
    
                if (drawable instanceof ColorDrawable) {
                    bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
                } else {
                    bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
                }
    
                Canvas canvas = new Canvas(bitmap);
                drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
                drawable.draw(canvas);
                return bitmap;
            } catch (OutOfMemoryError e) {
                return null;
            }
        }
    
        private void setup() {
            if (!mReady) {
                mSetupPending = true;
                return;
            }
    
            if (mBitmap == null) {
                return;
            }
    
            mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    
            mBitmapPaint.setAntiAlias(true);
            mBitmapPaint.setShader(mBitmapShader);
    
            mBorderPaint.setStyle(Paint.Style.STROKE);
            mBorderPaint.setAntiAlias(true);
            mBorderPaint.setColor(mBorderColor);
            mBorderPaint.setStrokeWidth(mBorderWidth);
    
            mBitmapHeight = mBitmap.getHeight();
            mBitmapWidth = mBitmap.getWidth();
    
            mBorderRect.set(0, 0, getWidth(), getHeight());
            mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);
    
            mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);
            mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);
    
            updateShaderMatrix();
            invalidate();
        }
    
        private void updateShaderMatrix() {
            float scale;
            float dx = 0;
            float dy = 0;
    
            mShaderMatrix.set(null);
    
            if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {
                scale = mDrawableRect.height() / (float) mBitmapHeight;
                dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
            } else {
                scale = mDrawableRect.width() / (float) mBitmapWidth;
                dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
            }
    
            mShaderMatrix.setScale(scale, scale);
            mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);
    
            mBitmapShader.setLocalMatrix(mShaderMatrix);
        }
    
    }


    attrs.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="CircleImageView">
            <attr name="border_width" format="dimension" />
            <attr name="border_color" format="color" />
        </declare-styleable>
    </resources>




    展开全文
  • android圆形图片控件

    2019-01-11 16:07:10
    android圆形图片控件,在项目开发中,我们经常需要用到圆形图片效果,典型案例是用户头像的显示。
  • Android 圆形图片 CircleImageView
  • Android 圆形图片

    2014-09-29 01:58:21
    Android 圆形图片
  • android圆形图片控件自定义
  • android圆形图片裁剪demo以及实现,在处理一些用户头像上传和设置的时候极有可能会用到 android圆形图片裁剪demo以及实现,在处理一些用户头像上传和设置的时候极有可能会用到
  • 此为博客实例代码:...此资源同“Android 圆形图片 CircleImageView”为同一份资源,因为之前上传的时候,忘记添加描述信息,但又不能编辑,不能删除(好恶心的功能),所以就重新上传了一份。
  • android圆形图片工具类

    2017-11-20 14:43:46
    android圆形图片工具类

    这是之前在网上搜到的,过了太久,忘记了原作者是谁,还请谅解。因为csdn现在上传的资源都要很高的下载分,所以我就把代码完整的贴到博客上,希望对大家能有帮助。

    public class NoBorderCircleImageView extends ImageView {
    	/**
    	 * 图片尺寸及质量
    	 */
    	private static final int COLORDRAWABLE_DIMENSION = 1;
    	private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
    
    	/**
    	 * 圆形图片填充类型
    	 */
    	private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;
    	/**
    	 * 默认边框的大小和颜色
    	 */
    //	private static final int DEFAULT_BORDER_WIDTH = 2;
    //	private static final int DEFAULT_BORDER_COLOR = Color.YELLOW;
    	/**
    	 * 实际边框的大小和颜色
    	 */
    	private int mBorderWidth;
    	private int mBorderColor;
    	/**
    	 * 图片渲染方式
    	 */
    	private BitmapShader mBitmapShader;
    	private Paint mBitmapPaint = new Paint();
    	private Paint mBorderPaint = new Paint();
    	/**
    	 * 要绘制的图片以及自身宽高
    	 */
    	private Bitmap mBitmap;
    	private int mBitmapHeight;
    	private int mBitmapWidth;
    
    	private RectF mBorderRect = new RectF();
    	private float mBorderRadius;
    	private RectF mDrawableRect = new RectF();
    	private float mDrawableRadius;
    	private Matrix mShaderMatrix = new Matrix();
    	private boolean mReady;
    	private boolean mSetupPending;
    
    	public NoBorderCircleImageView(Context context) {
    		super(context);
    	}
    
    	public NoBorderCircleImageView(Context context, AttributeSet attrs) {
    		this(context, attrs, 0);
    	}
    
    	public NoBorderCircleImageView(Context context, AttributeSet attrs, int defStyle) {
    		super(context, attrs, defStyle);
    		super.setScaleType(SCALE_TYPE);
    //		TypedArray arr = context.obtainStyledAttributes(attrs,
    //				R.styleable.CircleImageView, defStyle, 0);
    //		mBorderWidth = arr.getDimensionPixelSize(
    //				R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);
    //		mBorderColor = arr.getColor(R.styleable.CircleImageView_border_color,
    //				DEFAULT_BORDER_COLOR);
    //		arr.recycle();
    		mReady = true;
    		if (mSetupPending) {
    			setup();
    			mSetupPending = false;
    		}
    	}
    
    	@Override
    	public ScaleType getScaleType() {
    		return SCALE_TYPE;
    	}
    
    	@Override
    	public void setScaleType(ScaleType scaleType) {
    		if (scaleType != SCALE_TYPE) {
    			throw new IllegalArgumentException(String.format(
    					"ScaleType %s not supported.", scaleType));
    		}
    	}
    
    	@Override
    	protected void onDraw(Canvas canvas) {
    		if (getDrawable() == null) {
    			return;
    		}
    		canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius,
    				mBitmapPaint);
    		canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius,
    				mBorderPaint);
    	}
    
    	@Override
    	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    		super.onSizeChanged(w, h, oldw, oldh);
    		setup();
    	}
    
    	@Override
    	public void setImageBitmap(Bitmap bm) {
    		super.setImageBitmap(bm);
    		mBitmap = bm;
    		setup();
    	}
    
    	@Override
    	public void setImageDrawable(Drawable drawable) {
    		super.setImageDrawable(drawable);
    		mBitmap = getBitmapFromDrawable(drawable);
    		setup();
    	}
    
    	@Override
    	public void setImageResource(int resId) {
    		super.setImageResource(resId);
    		mBitmap = getBitmapFromDrawable(getDrawable());
    		setup();
    	}
    
    	private Bitmap getBitmapFromDrawable(Drawable drawable) {
    		if (drawable == null) {
    			return null;
    		}
    
    		if (drawable instanceof BitmapDrawable) {
    			return ((BitmapDrawable) drawable).getBitmap();
    		}
    
    		try {
    			Bitmap bitmap;
    
    			if (drawable instanceof ColorDrawable) {
    				bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION,
    						COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);
    			} else {
    				bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
    						drawable.getIntrinsicHeight(), BITMAP_CONFIG);
    			}
    
    			Canvas canvas = new Canvas(bitmap);
    			drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    			drawable.draw(canvas);
    			return bitmap;
    		} catch (OutOfMemoryError e) {
    			return null;
    		}
    	}
    
    	public void setBorderColor(int borderColor) {
    		if (borderColor == mBorderColor) {
    			return;
    		}
    
    		mBorderColor = borderColor;
    		mBorderPaint.setColor(mBorderColor);
    		invalidate();
    	}
    
    	public int getBorderWidth() {
    		return mBorderWidth;
    	}
    
    	public void setBorderWidth(int borderWidth) {
    		if (borderWidth == mBorderWidth) {
    			return;
    		}
    
    		mBorderWidth = borderWidth;
    		setup();
    	}
    
    	private void setup() {
    
    		if (!mReady) {
    			mSetupPending = true;
    			return;
    		}
    
    		if (mBitmap == null) {
    			return;
    		}
    		mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,
    				Shader.TileMode.CLAMP);
    
    		mBitmapPaint.setAntiAlias(true); // 是否有锯齿
    		mBitmapPaint.setShader(mBitmapShader);
    
    		mBitmapHeight = mBitmap.getHeight();
    		mBitmapWidth = mBitmap.getWidth();
    
    		mBorderPaint.setStyle(Paint.Style.STROKE);
    		mBorderPaint.setAntiAlias(true);
    		mBorderPaint.setColor(mBorderColor);
    		mBorderPaint.setStrokeWidth(mBorderWidth);
    
    		mBorderRect.set(0, 0, getWidth(), getHeight());
    		mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2,
    				(mBorderRect.width() - mBorderWidth) / 2);
    
    		mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width()
    				- mBorderWidth, mBorderRect.height() - mBorderWidth);
    		mDrawableRadius = Math.min(mDrawableRect.height() / 2,
    				mDrawableRect.width() / 2);
    
    		updateShaderMatrix();
    		invalidate();
    	}
    
    	private void updateShaderMatrix() {
    		float scale;
    		float dx = 0;
    		float dy = 0;
    
    		mShaderMatrix.set(null);
    
    		if (mBitmapWidth * mDrawableRect.height() > mDrawableRect.width()
    				* mBitmapHeight) {
    			scale = mDrawableRect.height() / (float) mBitmapHeight;
    			dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;
    		} else {
    			scale = mDrawableRect.width() / (float) mBitmapWidth;
    			dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;
    		}
    
    		mShaderMatrix.setScale(scale, scale);
    		mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth,
    				(int) (dy + 0.5f) + mBorderWidth);
    		mBitmapShader.setLocalMatrix(mShaderMatrix);
    	}
    }
    这是无边框的,要是需要边框,就把上面注掉的几行打开,在values下面新建个attrs的xml文件,里面加上:

        <declare-styleable name="CircleImageView">
            <attr name="border_width" format="dimension" />
            <attr name="border_color" format="color" />
        </declare-styleable>




    展开全文
  • Android 圆形图片代码

    2017-04-05 12:03:23
    Android处理圆形图片的代码。
  • Android圆形图片或者圆角图片的快速实现 话不多说直接上code xml文件布局

    Android圆形图片或者圆角图片的快速实现

    话不多说直接上code

    xml文件布局

            <LinearLayout
            android:id="@+id/ll_headpict"
            android:layout_width="match_parent"
            android:layout_height="97dp"
            android:layout_margin="13dp"
            android:background="@drawable/shape_white_radius10_solid"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="25dp">
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="头像"
                android:textColor="@color/color4A4A4A"
                android:textSize="14sp"
                android:textStyle="bold" />
    
            <ImageView
                android:id="@+id/iv_headpict"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_marginRight="37dp"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_headview_demo" />
        </LinearLayout>
    

    初始化控件之后用工具类加载
    //第一个参数上下文,第二个控件名称,第三个图片url地址,第四个参数圆角大小
    ViewUtils.loadImageRadius(this, mIvpict, stringUrl, 15);//头像

    ViewUtils.java工具类

        /**
     * Created by wjw on 2016/11/28
     * 倒圆角工具类
     */
    
    public class ViewUtils {
    
        /**
         * 图片加载
         * @param context
         * @param iv
         * @param url
         */
        public static void loadImage(Context context, ImageView iv, String url) {
            if(null ==context || null==iv){
                return;
            }
            if(Utils.isTxtEmpty(url)){
                try {
                    Glide.with(context).load(R.mipmap.placeholder_icon) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).into(iv);
                }catch (Exception e){
    
                }
            }else {
                try {
                    Glide.with(context).load(url) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.placeholder_icon).into(iv);
                } catch (Exception e) {
                }
            }
        }
    
    
        public static void loadImage(Context context, ImageView iv, int id) {
            if(null ==context || null==iv){
                return;
            }
            try {
                Glide.with(context).load(id) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.placeholder_icon).into(iv);
            }catch (Exception e){
            }
        }
    
    
        /**
         * 本地图片
         * @param context
         * @param iv
         * @param id
         * @param radius
         */
        public static void loadImage(Context context, ImageView iv, int id,int radius) {
            if(null ==context || null==iv){
                return;
            }
            try {
                Glide.with(context).load(id) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
                        transform(new GlideRoundTransform(context, radius)).into(iv);
            }catch (Exception e){
    
            }
    
        }
    
        public static void setImageResource(ImageView iv, int id) {
            if(null!=iv){
                iv.setImageResource(id);
            }
        }
    
    
        /**
         * 加载网络图片(带圆角)
         * @param context
         * @param iv
         * @param url
         * @param radius
         */
        public static void loadImageRadius(Context context, ImageView iv, String url, int radius) {
            if(null ==context || null==iv){
                return;
            }
            if(Utils.isTxtEmpty(url)){
                try {
                    Glide.with(context).load(R.mipmap.placeholder_icon) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
                            transform(new GlideRoundTransform(context, radius)).into(iv);
                }catch (Exception e){
                }
            }else{
                try {
                    Glide.with(context).load(url) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
                            transform(new GlideRoundTransform(context, radius)).placeholder(R.mipmap.placeholder_icon).into(iv);
                }catch (Exception e){
    
                }
            }
    
        }
    
        /**
         * 加载网络图片(圆形)
         * @param context
         * @param iv
         * @param url
         */
        public static void loadImageCircle(Context context, ImageView iv, String url) {
            if(null ==context || null==iv){
                return;
            }
            if (Utils.isTxtEmpty(url)) {
                try {
                    Glide.with(context).load(R.mipmap.placeholder_icon) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).
                            transform(new GlideCircleTransform(context)).into(iv);
                }catch (Exception e){
                }
            } else {
                try {
                    Glide.with(context).load(url) .dontAnimate().diskCacheStrategy(DiskCacheStrategy.ALL).transform(new GlideCircleTransform(context)).
                            placeholder(R.mipmap.placeholder_icon).into(iv);
                }catch (Exception e){
                }
            }
        }
    	}
    
       //圆形图片
        public static class GlideCircleTransform extends BitmapTransformation {
            public GlideCircleTransform(Context context) {
                super(context);
            }
    
            @Override
            protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
                return circleCrop(pool, toTransform);
            }
    
            private Bitmap circleCrop(BitmapPool pool, Bitmap source) {
                if (source == null)
                    return null;
                int size = Math.min(source.getWidth(), source.getHeight());
                int x = (source.getWidth() - size) / 2;
                int y = (source.getHeight() - size) / 2;
                Bitmap squared = Bitmap.createBitmap(source, x, y, size, size);
                Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888);
                if (result == null) {
                    result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                }
                Canvas canvas = new Canvas(result);
                Paint paint = new Paint();
                paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
                paint.setAntiAlias(true);
                float r = size / 2f;
                canvas.drawCircle(r, r, r, paint);
                return result;
            }
    
            @Override
            public String getId() {
                return getClass().getName();
            }
        }
    
        //圆角
        public static class GlideRoundTransform extends BitmapTransformation {
            private static float radius = 0f;
    
            public GlideRoundTransform(Context context) {
                this(context, 4);
            }
    
            public GlideRoundTransform(Context context, int dp) {
                super(context);
                this.radius = Resources.getSystem().getDisplayMetrics().density * dp;
            }
    
            @Override
            protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
                return roundCrop(pool, toTransform);
            }
    
            private static Bitmap roundCrop(BitmapPool pool, Bitmap source) {
                if (source == null)
                    return null;
    
                Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
                if (result == null) {
                    result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
                }
    
                Canvas canvas = new Canvas(result);
                Paint paint = new Paint();
                paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
                paint.setAntiAlias(true);
                RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight());
                canvas.drawRoundRect(rectF, radius, radius, paint);
                return result;
            }
    
            @Override
            public String getId() {
                return getClass().getName() + Math.round(radius);
            }
        }
    

    效果如图圆角图片

    展开全文
  • Android圆形图片控件效果图如下: 代码如下: RoundImageView.java package com.dxd.roundimageview; import android.content.Context; import android.content.res.TypedArray; import android....
  • Android圆形图片不求人,自定义View实现(BitmapShader使用) 原文章地址:http://blog.csdn.net/halaoda/article/details/78343787 在很多APP当中,圆形的图片是必不可少的元素,美观大方。本文将带领读者...
  • 在项目中使用圆形的图片的需求还是蛮大的,所以封装一个圆形图片的控件,可以加载本地图片以及网络图片,此控件是继承自Imageview控件。
  • android开发中将图片转化为圆形图片的demo
  • Android圆形图片--自定义控件

    万次阅读 热门讨论 2014-12-17 22:24:48
    Android圆形图片--自定义控件
  • 上一篇文章中,讲了Android圆形图片实现2种方式中的Xfermode方式。 Android 圆形图片 CircleImageView(Xfermode方式) 今天讲解Android圆形图片实现的另一种方式,BitmapShader(着色器,也叫渲染器)和Matrix...
  • Android圆形图片控件效果图例如以下: 代码例如以下: RoundImageView.java package com.dxd.roundimageview; import android.content.Context; import android.content.res.TypedArray; import ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 2,320
精华内容 928
关键字:

android圆形图片