package com.qnear.wechat;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
这是自定义的类
来源:http://www.cnblogs.com/goagent/p/5159126.html
public class ImageViewPlus extends ImageView {
private Paint mPaintBitmap = new Paint(Paint.ANTI_ALIAS_FLAG);
private Bitmap mRawBitmap;
private BitmapShader mShader;
private Matrix mMatrix = new Matrix();
public ImageViewPlus(Context context, AttributeSet attrs) {
super(context,attrs);
}
@Override
protected void onDraw(Canvas canvas){
Bitmap rawBitmap = getBitmap(getDrawable());
if(rawBitmap!=null){
int viewWidth = getWidth();
int viewHeight = getHeight();
int viewMinSize = Math.min(viewWidth, viewHeight);
float dstWidth = viewMinSize;
float dstHeight = viewMinSize;
if(mShader==null||!rawBitmap.equals(mRawBitmap)){
mRawBitmap = rawBitmap;
mShader = new BitmapShader(mRawBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}
if(mShader!=null){
mMatrix.setScale(dstWidth/rawBitmap.getWidth(),dstHeight/rawBitmap.getHeight());
mShader.setLocalMatrix(mMatrix);
}
mPaintBitmap.setShader(mShader);
float radius =viewMinSize/2.0f;
canvas.drawCircle(radius,radius,radius,mPaintBitmap);
}else{
super.onDraw(canvas);
}
}
private Bitmap getBitmap(Drawable drawable) {
if(drawable instanceof BitmapDrawable){
return ((BitmapDrawable) drawable).getBitmap();
}else if(drawable instanceof ColorDrawable){
Rect rect = drawable.getBounds();
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int color = ((ColorDrawable) drawable).getColor();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawARGB(Color.alpha(color),Color.red(color),Color.blue(color),Color.green(color));
return bitmap;
}else{
return null;
}
}
}
这是引用语句:
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/mwinbag"
android:scaleType="centerInside"
android:layout_marginTop="146dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
然后我就遇到了这个问题:
一个显示正常,一个黑屏