zoukankan      html  css  js  c++  java
  • 17_读取字体资源在canvas中绘文本


    import android.app.Activity;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;

    public class MainActivity extends Activity {
      

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(new RenderView(this));
        }
    }


    class RenderView extends View {
        Paint paint;
        Typeface font;
        Rect bounds = new Rect();

        public RenderView(Context context) {
            super(context);
            paint = new Paint();
            font = Typeface.createFromAsset(context.getAssets(), "font.ttf");
        }

        protected void onDraw(Canvas canvas) {
            paint.setColor(Color.YELLOW);
            paint.setTypeface(font);
            paint.setTextSize(28);
            paint.setTextAlign(Paint.Align.CENTER);
            canvas.drawText("This is a test!", canvas.getWidth() / 2, 100,
                    paint);

            String text = "This is another test o_O";
            paint.setColor(Color.WHITE);
            paint.setTextSize(18);
            paint.setTextAlign(Paint.Align.LEFT);
            paint.getTextBounds(text, 0, text.length(), bounds);
            canvas.drawText(text, canvas.getWidth() - bounds.width(), 140,
                    paint);
            invalidate();
        }
    }

  • 相关阅读:
    任正非:所有公司都是管理第一,技术第二(没有一流管理,领先的技术就会退化;有一流管理,即使技术二流也会进步)
    QuickReport的OnNeedData的触发情况
    Quickreport不用数据字段,如何实现多页打印?
    我要继续做开发吗(对18个问题,全部都是肯定!)
    一台主机,至多可以开启多少个线程
    BenchmarkDotNet
    开发资源
    WebSocket
    TCP
    “在什么时候学习编程才合适?”
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060788.html
Copyright © 2011-2022 走看看