1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| /** * Created by Lrxc on 2018/6/13.17:17 * Email: Lrxc1117@163.com */ public class MyView extends View { private final int screenWidth;//屏幕宽度 private final TextPaint textPaint;//画笔 private final int textSize = 50;//画笔大小 private final Bitmap logo;//logo图片 private final int logoPadingTop = 200;//logo上边距
private final String str = "我仰望星空,浩瀚的银河系,哪一颗才是属于我的那个你?无论你在哪里,我都等着你,盼着你,直到遇见你。如果你看见了我,请你一定联系我,请不要让我等的太久,我会遗憾陪伴你的日子太过短暂。" + "我仰望星空,浩瀚的银河系,哪一颗才是属于我的那个你?无论你在哪里,我都等着你,盼着你,直到遇见你。如果你看见了我,请你一定联系我,请不要让我等的太久,我会遗憾陪伴你的日子太过短暂";
public MyView(Context context, @Nullable AttributeSet attrs) { super(context, attrs);
textPaint = new TextPaint(); textPaint.setTextSize(textSize); textPaint.setColor(Color.RED);
DisplayMetrics dm = getResources().getDisplayMetrics(); screenWidth = dm.widthPixels;
logo = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); }
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制logo canvas.drawBitmap(logo, screenWidth - logo.getWidth() - 5, logoPadingTop, null); //绘制不规则文字 lineFeed(canvas, str, textSize); }
/** * @param canvas 画布 * @param str 绘制内容 * @param heigth 每一行的高度 */ private void lineFeed(Canvas canvas, String str, int heigth) { int lineWidth = screenWidth; //右边有logo 空出位置 if (heigth >= logoPadingTop && heigth <= logoPadingTop + logo.getHeight() + textSize) { lineWidth = screenWidth - logo.getWidth(); } //计算当前宽度(width)能显示多少个汉字 int subIndex = textPaint.breakText(str, 0, str.length(), true, lineWidth, null); //截取可以显示的汉字 String mytext = str.substring(0, subIndex); canvas.drawText(mytext, 0, heigth, textPaint);
//计算剩下的汉字 String ss = str.substring(subIndex, str.length()); if (ss.length() > 0) { lineFeed(canvas, ss, heigth + textSize + 10); } } }
|