2015-02-12 68 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