如果你使用的image異步加載是開源項目請檢查是否需要在自定義 的imageview中設置ondraw()是需要重新設置繪製的bitmap,比如xutils帶的bitmaputils中的drawable是自定義的AsyncDrawable需要加入註釋掉的內容。
代碼
package com.example.asyncbitmap;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View.MeasureSpec;
import android.widget.ImageView;
public class RoundImageView extends ImageView{
private int canvasSize;
private Bitmap image;
private Paint paint;
public RoundImageView(final Context context) {
this(context, null);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
public void onDraw(Canvas canvas) {
// load the bitmap
image = drawableToBitmap(getDrawable());
// init shader
if (image != null) {
canvasSize = canvas.getWidth();
if (canvas.getHeight() < canvasSize)
canvasSize = canvas.getHeight();
// BitmapShader shader = new BitmapShader(Bitmap.createScaledBitmap(
// image, canvasSize, canvasSize, false),
// Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
BitmapShader shader = new BitmapShader(Bitmap.createBitmap(image),
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
// circleCenter is the x or y of the view's center
// radius is the radius in pixels of the cirle to be drawn
// paint contains the shader that will texture the shape
int circleCenter = (canvasSize ) / 2;
canvas.drawCircle(circleCenter , circleCenter,(canvasSize/2 ), paint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = measureWidth(widthMeasureSpec);
int height = measureHeight(heightMeasureSpec);
setMeasuredDimension(width, height);
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// The parent has determined an exact size for the child.
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// The parent has not imposed any constraint on the child.
result = canvasSize;
}
return result;
}
private int measureHeight(int measureSpecHeight) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpecHeight);
int specSize = MeasureSpec.getSize(measureSpecHeight);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else if (specMode == MeasureSpec.AT_MOST) {
// The child can be as large as it wants up to the specified size.
result = specSize;
} else {
// Measure the text (beware: ascent is a negative number)
result = canvasSize;
}
return (result + 2);
}
public Bitmap drawableToBitmap(Drawable drawable) {
if (drawable == null) {
return new BitmapFactory().decodeResource(getResources(),
R.drawable.head_right);
}
else if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
// else if (this.getDrawable() instanceof AsyncDrawable) {
// // Drawable drawable = this.getDrawable();
// return Bitmap
// .createBitmap(
// getWidth(),
// getHeight(),
// drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
// : Bitmap.Config.RGB_565) == new BitmapFactory()
// .decodeResource(getResources(), R.drawable.head_right) ? null
// : Bitmap.createBitmap(
// getWidth(),
// getHeight(),
// drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
// : Bitmap.Config.RGB_565);
// }
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}
使用代碼
RoundImageView img = (RoundImageView)findViewById(R.id.imageview_id);
ImageLoader.getInstance().displayImage("uri地址", img);