qq_35118155 2016-12-22 05:42 采纳率: 0%
浏览 1554

Android 自定义ImageView出现的问题

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" />
然后我就遇到了这个问题:
图片说明图片说明
一个显示正常,一个黑屏

  • 写回答

4条回答 默认 最新

  • enjoyGai 2016-12-22 06:15
    关注

    你为什么要用模拟器呢??

    评论

报告相同问题?

悬赏问题

  • ¥15 python变量和列表之间的相互影响
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)