2015-02-12 69 views
0

我正在尝试使用此代码绘制头像位图。但是位图是像素化的。这是我的代码。目前我使用createScaledBitmap来调整头像图片的大小。此外,文字在高分辨率的某些设备上稍小一些在画布上绘制位图时出现质量不佳

BitmapFactory.Options opt = new BitmapFactory.Options(); 
     opt.inMutable = true; 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), 
      R.drawable.card_nodpi, opt) ; 

Canvas canvas = new Canvas(bitmap); 

    Paint paint = new Paint(); 
    paint.setColor(Color.BLACK); 
    paint.setTextSize(40); 
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD)); 
    canvas.drawText("The quick brown fox", x, y, paint); 

    Paint paint2 = new Paint(); 
    paint2.setColor(Color.BLACK); 
    paint2.setTextSize(30); 


    canvas.drawText("The quick brown fox", x, y + (40), paint2); 
    canvas.drawText("The quick brown fox", x, y + ((40 * 2)), paint2); 

    if (avatar != null) { 
     Bitmap img = Bitmap.createScaledBitmap(avatar, 250, 250, false); 
     canvas.drawBitmap(img, bitmap.getWidth() - img.getWidth() - x, y - 40, new Paint(Paint.FILTER_BITMAP_FLAG)); 
    } 

imageView.setImageBitmap(bitmap); 
+0

没有错的代码之前......你确定你的头像是不太多小? R.drawable.card_nodpi有多大? – Kasra 2015-02-12 04:21:46

+0

大小为1,016 x 638 – 2015-02-12 05:05:42

回答

1

createScaledBitmap可能会产生一些质朴/质量差的图像。 试试这里的解决方案: https://stackoverflow.com/a/7468636/4557530

让我知道,如果这对你做任何事情!

另外, 试试这个,我用这个代码感谢一些博客,我不记得了

Bitmap newBM = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888); 

    float scaleX = newWidth/(float) origBM.getWidth(); 
    float scaleY = newHeight/(float) origBM.getHeight(); 
    float pivX = 0; 
    float pivY = 0; 

    Matrix scaleMatrix = new Matrix(); 
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY); 

    Canvas canvas = new Canvas(newBM); 
    canvas.setMatrix(scaleMatrix); 
    canvas.drawBitmap(origBM, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG)); 
+0

newBm的目的是什么? – 2015-02-12 05:12:58

+0

哎呦对不起,它太晚了:)现在看看它 – 2015-02-12 05:39:00

+0

对不起,我有点失落。因为在这一张上,你正在画布中使用newBM。然后,我的位图会发生什么?或者这是一个仅用于创建头像的新画布? – 2015-02-12 05:54:33