Android知识总结:Universal-Imageloader学习笔记3 显示自定义形状头像

显示自定义形状

       昨天我们分析了ImageLoader的主业务流程,现在我们把它应用到项目的实践中。

        项目中我们需要显示圆形图片,带圆角的图片,以及图片上叠加其他标记,我们可以分别制作相应的自定义控件,

ImagerLoader为我们建立了BitmapDisplayer,实现这个接口 并将其设置到下载options中。

       在 ImageLoader. getInstance (). displayImage中,将options填入,这样,就可以在方形 的ImageView中显示我们自

定义的任何形状的图形了。

效果与实现

如图,现在我们为圆形图片上加一个小的修饰图片,这样,我们只需实现BitmapDisplayer接口,自定义一

个圆形Drawable,并在其右上角加一个性别标识即可。以后,无论什么样的ImageView,我们都可以直接下载图片,

显示时自动变成圆形带小标志的图片。


下面是具体实现BitmapDisplayer,实现过程可以参考ImageLoader中自带的例子,我们稍加改动。

/**
 * 带性别标识的头像
 * Created by vonchenchen on 2015/12/2 0002.
 */
public class HeaderWithGender implements BitmapDisplayer {
    protected final Integer strokeColor;
    protected final float strokeWidth;
    protected int mGender;

    public HeaderWithGender(Integer strokeColor, int gender) {
        this(strokeColor, 0, gender);
    }

    public HeaderWithGender(Integer strokeColor, float strokeWidth, int gender) {
        this.strokeColor = strokeColor;
        this.strokeWidth = strokeWidth;
        this.mGender = gender;
    }

    @Override
    public void display(Bitmap bitmap, ImageAware imageAware, LoadedFrom loadedFrom) {
        if (!(imageAware instanceof ImageViewAware)) {
            throw new IllegalArgumentException("ImageAware should wrap ImageView. ImageViewAware is expected.");
        }

        imageAware.setImageDrawable(new CircleDrawable(bitmap, strokeColor, strokeWidth, mGender));
    }

    public static class CircleDrawable extends Drawable {

        protected float radius;

        protected final RectF mRect = new RectF();
        protected final RectF mBitmapRect;
        protected final BitmapShader bitmapShader;
        protected final Paint paint;
        protected final Paint strokePaint;
        protected final Paint mGenderIconPaint;
        protected final float strokeWidth;
        protected float strokeRadius;

        public static final int GENDER_MALE = 1;
        public static final int GENDER_FEMALE = 2;

        protected int mGender;
        protected Bitmap mGenderBitmap = null;
        protected int mMarginLeft;

        public CircleDrawable(Bitmap bitmap, Integer strokeColor, float strokeWidth, int gender) {
            radius = Math.min(bitmap.getWidth(), bitmap.getHeight()) / 2;

            mMarginLeft = (int)UIUtils.getResources().getDimension(R.dimen.px_91);

            bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            //图片原始区域
            mBitmapRect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

            mGender = gender;
            if(mGender != GENDER_MALE || mGender != GENDER_FEMALE){
                mGender = GENDER_MALE;
            }

            //给画笔设置Shader
            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setShader(bitmapShader);
            paint.setFilterBitmap(true);
            paint.setDither(true);

            mGenderIconPaint =  new Paint();
            mGenderIconPaint.setAntiAlias(true);

            if (strokeColor == null) {
                strokePaint = null;
            } else {
                //绘制边缘的画笔
                strokePaint = new Paint();
                strokePaint.setStyle(Paint.Style.STROKE);
                strokePaint.setColor(strokeColor);
                strokePaint.setStrokeWidth(strokeWidth);
                strokePaint.setAntiAlias(true);
            }
            this.strokeWidth = strokeWidth;
            strokeRadius = radius - strokeWidth / 2;
        }

        //边界变化时调用   这里是将原始区域的图片映射到新的圆形区域
        @Override
        protected void onBoundsChange(Rect bounds) {
            super.onBoundsChange(bounds);
            //扩一块空间
            mRect.set(0, 0, bounds.width(), bounds.height());
            //取长宽中短的值作为直径
            radius = Math.min(bounds.width(), bounds.height()) / 2;
            strokeRadius = radius - strokeWidth / 2;

            // 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) {
            //绘制Drawable内部图形
            canvas.drawCircle(radius, radius, radius, paint);
            if (strokePaint != null) {
                //绘制边界
                canvas.drawCircle(radius, radius, strokeRadius, strokePaint);
            }

            if(mGender == GENDER_MALE){
                if(mGenderBitmap == null){
                    mGenderBitmap = BitmapFactory.decodeResource(UIUtils.getResources(), R.mipmap.ic_boy);
                }
            }else if(mGender == GENDER_FEMALE){
                if(mGenderBitmap == null){
                    mGenderBitmap = BitmapFactory.decodeResource(UIUtils.getResources(), R.mipmap.ic_gril);
                }
            }

            canvas.drawBitmap(mGenderBitmap, mMarginLeft, 0,mGenderIconPaint);
        }

        @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);
        }
    }
}

调用方法
       mOptionHeader = new DisplayImageOptions.Builder()
                .showImageOnLoading(Constants.DEFAULT_lOADING_IAMGE)
                .showImageForEmptyUri(Constants.DEFAULT_IAMGE)
                .showImageOnFail(Constants.DEFAULT_IAMGE)
                .cacheInMemory(true)                             //是否缓存到内存
                .cacheOnDisk(true)                               //是否缓存到硬盘
                .considerExifParams(true)                        //是否使用Exif信息 (Exif信息可以理解为JPEG图中加入相机相关信息)
                // 添加显示的图片形状
                .displayer(new HeaderWithGender(0, HeaderWithGender.CircleDrawable.GENDER_FEMALE)).build();


ImageLoader.getInstance().displayImage("http://xxxxxxxxxx.jpg", mImageView, mOptionHeader);

除了http,查阅官方文档,我们还可以使用以下格式的uri
"http://site.com/image.png" // from Web
"file:///mnt/sdcard/image.png" // from SD card
"file:///mnt/sdcard/video.mp4" // from SD card (video thumbnail)
"content://media/external/images/media/13" // from content provider
"content://media/external/video/media/13" // from content provider (video thumbnail)
"assets://image.png" // from assets
"drawable://" + R.drawable.img // from drawables (non-9patch images)






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Android Studio实现轮播图的步骤: 1.在app/build.gradle文件中添加依赖项: ```gradle dependencies { implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support:design:28.0.0' implementation 'com.android.support:support-v4:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:palette-v7:28.0.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.android.support:support-vector-drawable:28.0.0' implementation 'com.android.support:animated-vector-drawable:28.0.0' implementation 'com.android.support:customtabs:28.0.0' implementation 'com.android.support:exifinterface:28.0.0' implementation 'com.android.support:preference-v7:28.0.0' implementation 'com.android.support:preference-v14:28.0.0' implementation 'com.android.support:percent:28.0.0' implementation 'com.android.support:mediarouter-v7:28.0.0' implementation 'com.android.support:gridlayout-v7:28.0.0' implementation 'com.android.support:leanback-v17:28.0.0' implementation 'com.android.support:cursoradapter:28.0.0' implementation 'com.android.support:documentfile:28.0.0' implementation 'com.android.support:localbroadcastmanager:28.0.0' implementation 'com.android.support:print:28.0.0' implementation 'com.android.support:slices-builders:28.0.0' implementation 'com.android.support:slices-core:28.0.0' implementation 'com.android.support:slices-view:28.0.0' implementation 'com.android.support:wear:28.0.0' implementation 'com.android.support:wear-watchface:28.0.0' implementation 'com.android.support:wear-remote-interaction:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:cardview-v7:28.0.0' implementation 'com.android.support:recyclerview-v7:28.0.0' implementation 'com.android.support:palette-v7:28.0.0' implementation 'com.android.support:multidex:1.0.3' implementation 'com.android.support:support-vector-drawable:28.0.0' implementation 'com.android.support:animated-vector-drawable:28.0.0' implementation 'com.android.support:customtabs:28.0.0' implementation 'com.android.support:exifinterface:28.0.0' implementation 'com.android.support:preference-v7:28.0.0' implementation 'com.android.support:preference-v14:28.0.0' implementation 'com.android.support:percent:28.0.0' implementation 'com.android.support:mediarouter-v7:28.0.0' implementation 'com.android.support:gridlayout-v7:28.0.0' implementation 'com.android.support:leanback-v17:28.0.0' implementation 'com.android.support:cursoradapter:28.0.0' implementation 'com.android.support:documentfile:28.0.0' implementation 'com.android.support:localbroadcastmanager:28.0.0' implementation 'com.android.support:print:28.0.0' implementation 'com.android.support:slices-builders:28.0.0' implementation 'com.android.support:slices-core:28.0.0' implementation 'com.android.support:slices-view:28.0.0' implementation 'com.android.support:wear:28.0.0' implementation 'com.android.support:wear-watchface:28.0.0' implementation 'com.android.support:wear-remote-interaction:28.0.0' implementation 'com.youth.banner:banner:1.4.10' } ``` 2.在布局文件中添加Banner组件: ```xml <com.youth.banner.Banner android:id="@+id/banner" android:layout_width="match_parent" android:layout_height="200dp" app:banner_indicatorGravity="center" app:banner_delay_time="3000" app:banner_is_auto_play="true" app:banner_scroll_time="1000" /> ``` 3.在Activity或Fragment中设置Banner的数据源和图片加载器: ```java // 设置数据源 List<String> images = new ArrayList<>(); images.add("http://img.zcool.cn/community/01c8dc5d6f1e6fa801219c77f8f8c9.jpg"); images.add("http://img.zcool.cn/community/01c8dc5d6f1e6fa801219c77f8f8c9.jpg"); images.add("http://img.zcool.cn/community/01c8dc5d6f1e6fa801219c77f8f8c9.jpg"); banner.setImages(images); // 设置图片加载器 banner.setImageLoader(new GlideImageLoader()); ``` 4.创建图片加载器类: ```java public class GlideImageLoader extends ImageLoader { @Override public void displayImage(Context context, Object path, ImageView imageView) { Glide.with(context).load(path).into(imageView); } } ``` 5.启动Banner轮播: ```java banner.start(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值