6

Actually i have to create visiting card. I have different samples of visiting cards. user can select card and their colors. I am having trouble in writing text on bitmap. Text is not adjusting according to different screen sizes. I have tested on Samasung galaxy s5 and nexus 5. Both devices have 1080x1920 resolution. But have different ppi. Sampe image:

enter image description here

I am using this code:

     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;
}

}

2
  • Google keywords android supporting multiple screens But let me suggest you that "drawn text" is not a great idea. It would be better to provide your font and use sps May 25, 2016 at 16:51
  • actually i can not place lables. i have fixed height and width of image. 525x300 . i have to send this image to server too. so i have taken images of this height and width and m placing text on runtime. Cards are not so simple like this. May 26, 2016 at 18:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.