android 開(kāi)發(fā)程序員專(zhuān)題找一管理員,要求:長(zhǎng)期瀏覽簡(jiǎn)書(shū)并且是android開(kāi)發(fā)人員,有意者簡(jiǎn)信我?
需求Glide下載圖片并切圓角或圓形,但圖片有大有小,圖片不能改變,切圓還好說(shuō),但是切圓角就會(huì)發(fā)現(xiàn)圖片小的會(huì)比圖片大的要圓
搜一下 " Glide動(dòng)態(tài)加載圓形圖片跟圓角圖片 " 就會(huì)出現(xiàn)很多文章,但這些都不能解決上面的問(wèn)題 怎樣能 Glide動(dòng)態(tài)加載不同大小的圖片切圓形圖片跟圓角圖片呢?
解決很簡(jiǎn)單:既然是圖片大小不一致而導(dǎo)致圖片切出來(lái)不一樣,那就把圖片變的一樣大小不就可以嗎
申明一下我的代碼也是在Glide動(dòng)態(tài)加載圓形圖片跟圓角圖片搜出來(lái)的代碼基礎(chǔ)上修改的. 下面就是代碼了.
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();
//設(shè)置想要的大小
int newWidth=72;
int newHeight=72;
//計(jì)算壓縮的比率
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);
}
}
很簡(jiǎn)單吧,就是用changeBitmapSize方法把圖片壓縮到72*72的這樣圖片都一樣大了,在切就不會(huì)出現(xiàn)切出來(lái)的圖片效果不一樣了
最后代碼(dome)地址: https://github.com/liang9/Imagedome