如何使用不同屏幕尺寸的油漆在位图上写出像素完美文本

时间:2016-05-25 16:33:12

标签: android bitmap

其实我必须创建名片。我有不同的名片样本。用户可以选择卡片及其颜色。我在位图上写文字时遇到了麻烦。文本未根据不同的屏幕尺寸进行调整。我在Samasung galaxy s5和nexus 5上进行了测试。两款设备的分辨率都是1080x1920。但是有不同的ppi。 Sampe图片:

enter image description here

我正在使用此代码:

     private void customCard(int drawableId, int color) {

    bm1 = null;
    newBitmap = null;
    try {
        bm1 = BitmapFactory.decodeResource(getResources(), drawableId);
        Bitmap.Config config = bm1.getConfig();
        if (config == null) {
            config = Bitmap.Config.ARGB_8888;
        }
        newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);

        paintText = new Paint(Paint.ANTI_ALIAS_FLAG);

        newCanvas = new Canvas(newBitmap);
        newCanvas.drawBitmap(bm1, 0, 0, null);
        rectText = new Rect();


        ivCard.setImageBitmap(newBitmap);
    } catch (Exception e) {
        e.printStackTrace();
    }


    if (theme == 0) {
        //Set First Name
        if (firstName != null && !firstName.equals("")) {
            displayCardOne(firstName, color, 16, 60, 50, HELVETICA_REGULAR);
        }
        //Set Last Name
        if (lastName != null && !lastName.equals("")) {
            displayCardOne(lastName, color, 16, 100, 35, HELVETICA_BOLD);
        }
        //Set email label
        displayCardOne("Email", color, 70, 220, 20, HELVETICA_BOLD);
        //Set email
        if (email != null && !email.equals("")) {
            displayCardOne(email, color, 70, 240, 20, HELVETICA_REGULAR);
        }

        if (showPhoneNumber) {
            //Set  phone Number label
            displayCardOne(lPhone, color, 70, 170, 20, HELVETICA_BOLD);
            //Set  phone Number
            if (phoneNumber != null && !phoneNumber.equals("")) {
                displayCardOne(phoneNumber, color, 70, 190, 20, HELVETICA_REGULAR);
            }

        }
    }
}

       private void displayCardOne(String stringToDisplay, int color, float x, float y, float textSize, int textFont) {

    paintText.setTextAlign(Paint.Align.LEFT);
    // color 0 = black, color 1 = white
    if (color == 0) {
        paintText.setColor(Color.BLACK);
    } else {
        paintText.setColor(Color.WHITE);
    }
    paintText.setStyle(Paint.Style.FILL);

    paintText.setTextSize(DeviceDimensionsHelper.convertDpToPixel(textSize, this));

    if (textFont == HELVETICA_REGULAR) {
        paintText.setTypeface(Typeface.create(FontUtils.getInstance(this).getFontHelveticaRegular(), Typeface.NORMAL));
    } else if (textFont == HELVETICA_BOLD) {
        paintText.setTypeface(Typeface.create(FontUtils.getInstance(this).getFontHelveticaBold(), Typeface.BOLD));
    }
    paintText.getTextBounds(stringToDisplay, 0, stringToDisplay.length(), rectText);

    newCanvas.drawText(stringToDisplay, DeviceDimensionsHelper.convertDpToPixel(x, this),
            DeviceDimensionsHelper.convertDpToPixel(y, this), paintText);


}
     public class DeviceDimensionsHelper {
//     DeviceDimensionsHelper.getDisplayWidth(context) => (display width in pixels)
public static int getDisplayWidth(Context context) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return displayMetrics.widthPixels;
}

// DeviceDimensionsHelper.getDisplayHeight(context) => (display height in pixels)
public static int getDisplayHeight(Context context) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return displayMetrics.heightPixels;
}

// DeviceDimensionsHelper.convertDpToPixel(25f, context) => (25dp converted to pixels)
public static float convertDpToPixel(float dp, Context context) {
    Resources r = context.getResources();
    return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics());
}

// DeviceDimensionsHelper.convertPixelsToDp(25f, context) => (25px converted to dp)
public static float convertPixelsToDp(float px, Context context) {
    Resources r = context.getResources();
    DisplayMetrics metrics = r.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;
}

}

0 个答案:

没有答案