美文网首页Android技术知识程序员Android知识
android把Glide不同大小的图片切圆角和圆形

android把Glide不同大小的图片切圆角和圆形

作者: 无情剑_pgl | 来源:发表于2017-11-15 15:14 被阅读0次

    android 开发程序员专题找一管理员,要求:长期浏览简书并且是android开发人员,有意者简信我☺
    需求Glide下载图片并切圆角或圆形,但图片有大有小,图片不能改变,切圆还好说,但是切圆角就会发现图片小的会比图片大的要圆
    搜一下 " Glide动态加载圆形图片跟圆角图片 " 就会出现很多文章,但这些都不能解决上面的问题 怎样能 Glide动态加载不同大小的图片切圆形图片跟圆角图片呢?
    解决很简单:既然是图片大小不一致而导致图片切出来不一样,那就把图片变的一样大小不就可以吗
    申明一下我的代码也是在Glide动态加载圆形图片跟圆角图片搜出来的代码基础上修改的. 下面就是代码了.

    build.gradle

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.2"
    
        defaultConfig {
            applicationId "cn.xm.weidongjian.glidedemo"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
            debug {
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:26.1.0'
        compile 'com.github.bumptech.glide:glide:3.6.1'
    }
    
    

    activity_main.xml

    <RelativeLayout 
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:paddingLeft="@dimen/activity_horizontal_margin"
                    android:paddingRight="@dimen/activity_horizontal_margin"
                    android:paddingTop="@dimen/activity_vertical_margin"
                    android:paddingBottom="@dimen/activity_vertical_margin"
                    tools:context=".MainActivity">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="正常图片"
            android:id="@+id/button"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"/>
    
        <ImageView
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:id="@+id/imageView"
            android:scaleType="fitCenter"
            android:layout_below="@+id/button"
            android:layout_alignRight="@+id/button"
            android:layout_alignEnd="@+id/button"
            android:layout_marginTop="150dp"/>
    
        <ImageView
            android:layout_width="72dp"
            android:layout_height="72dp"
            android:id="@+id/imageView2"
            android:scaleType="fitCenter"
            android:layout_below="@+id/imageView"
            android:layout_alignRight="@+id/imageView"
            android:layout_alignEnd="@+id/imageView"
            android:layout_marginTop="5dp"
            />
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="圆角图片"
            android:id="@+id/button2"
            android:layout_below="@+id/button"
            android:layout_alignLeft="@+id/button"
            android:layout_alignRight="@+id/imageView"
            android:layout_alignEnd="@+id/imageView"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="大圆角图片"
            android:id="@+id/button3"
            android:layout_below="@+id/button2"
            android:layout_alignLeft="@+id/button2"
            android:layout_alignStart="@+id/button2"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="圆形图片"
            android:id="@+id/button4"
            android:layout_below="@+id/button3"
            android:layout_alignLeft="@+id/button"
            android:layout_alignRight="@+id/button3"
            android:layout_alignEnd="@+id/button3"/>
    </RelativeLayout>
    
    

    MainActivity

    package cn.xm.weidongjian.glidedemo;
    
    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageView;
    import com.bumptech.glide.Glide;
    import com.bumptech.glide.RequestManager;
    
    public class MainActivity extends AppCompatActivity implements OnClickListener {
    
        private ImageView imageView;
        private RequestManager glideRequest;
        private Context context = this;
        private ImageView imageView2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            init();
        }
    
        private void init() {
            findViewById(R.id.button).setOnClickListener(this);
            findViewById(R.id.button2).setOnClickListener(this);
            findViewById(R.id.button3).setOnClickListener(this);
            findViewById(R.id.button4).setOnClickListener(this);
            imageView = (ImageView) findViewById(R.id.imageView);
            imageView2 = (ImageView) findViewById(R.id.imageView2);
            glideRequest = Glide.with(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1704146358.png").into(imageView);
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1716089900.png").into(imageView2);
                    break;
                case R.id.button2:
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1704146358.png").transform(new GlideRoundTransform(context)).into(imageView);
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1716089900.png").transform(new GlideRoundTransform(context)).into(imageView2);
                    break;
                case R.id.button3:
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1704146358.png").transform(new GlideRoundTransform(context, 7)).into(imageView);
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1716089900.png").transform(new GlideRoundTransform(context, 7)).into(imageView2);
                    break;
                case R.id.button4:
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1704146358.png").transform(new GlideCircleTransform(context)).into(imageView);
                    glideRequest.load("http://androidop.le890.com/hma/upload/2016/10/11/1716089900.png").transform(new GlideCircleTransform(context)).into(imageView2);
                    break;
            }
        }
    }
    

    GlideCircleTransform

    package cn.xm.weidongjian.glidedemo;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
    import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
    
    public 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 static 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;
            // TODO this could be acquired from the pool too
            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();
        }
    }
    

    GlideRoundTransform

    package cn.xm.weidongjian.glidedemo;
    
    import android.content.Context;
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapShader;
    import android.graphics.Canvas;
    import android.graphics.Matrix;
    import android.graphics.Paint;
    import android.graphics.RectF;
    import android.util.Log;
    import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
    import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
    
    public 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 bitmap = changeBitmapSize(source);
            Bitmap result = pool.get(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_4444);
            if (result == null) {
    
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                result = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_4444);
            }
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint();
            paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP));
            paint.setAntiAlias(true);
            RectF rectF = new RectF(0f, 0f, bitmap.getWidth(), bitmap.getHeight());
            canvas.drawRoundRect(rectF, radius, radius, paint);
            return result;
        }
        public static Bitmap changeBitmapSize(Bitmap bitmap) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            //设置想要的大小
            int newWidth=72;
            int newHeight=72;
            //计算压缩的比率
            float scaleWidth=((float)newWidth)/width;
            float scaleHeight=((float)newHeight)/height;
            //获取想要缩放的matrix
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth,scaleHeight);
            //获取新的bitmap
            bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
            bitmap.getWidth();
            bitmap.getHeight();
            Log.e("newWidth","newWidth"+bitmap.getWidth());
            Log.e("newHeight","newHeight"+bitmap.getHeight());
            return bitmap;
        }
    
        @Override public String getId() {
            return getClass().getName() + Math.round(radius);
        }
    }
    

    很简单吧,就是用changeBitmapSize方法把图片压缩到72*72的这样图片都一样大了,在切就不会出现切出来的图片效果不一样了

    最后代码(dome)地址: https://github.com/liang9/Imagedome

    相关文章

      网友评论

        本文标题:android把Glide不同大小的图片切圆角和圆形

        本文链接:https://www.haomeiwen.com/subject/oxrcvxtx.html