Use Non-Standard Font on Android

Asked 1 years ago, Updated 1 years ago, 89 views

I'm thinking of making a simple game using OpenGL for Android devices, and I want to use my own font.

I am using the following application.

  • AppCompatActivity
  • GLSurfaceView
  • Render

Also, I use the following for drawing.These three are under GLSurfaceView.

  • Bitmap
  • Canvas
  • Paint

I want to use paint.setTypeface(Typeface.createFromAsset(getAsset(), "fonts/ipag.ttf")); to use fonts, but I cannot resolve getAsset.
I don't know how to resolve this.

I would appreciate it if someone could tell me how to solve it.
Thank you for your cooperation.

Source Code

  • MainActivity.java
package com.example.ma2.opengles;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.view.View;
import android.widget.TextView;

public class MainActivity extensions AppCompatActivity{

    private GLSurfaceView mGLView;

    @ Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity
        mGLView = new MyGLSurfaceView(this);
        setContentView (mGLView);

    }

    @ Override
    protected void onPause() {
        super.onPause();
        mGLView.onPause();
    }

    @ Override
    protected void onResume() {
        super.onResume();
        mGLView.onResume();
    }

    @ Override
    public void onWindowFocusChanged(boolean hasFocus){
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.KITKAT){
                getWindow().getDecorView().setSystemUiVisibility()
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); // this line is api19+
            } else{
                getWindow().getDecorView().setSystemUiVisibility()
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                | View.SYSTEM_UI_FLAG_FULLSCREEN);
            }
        }
    }

}
  • MyGLSurfaceView.java
package com.example.ma2.opengles;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.opengl.GLSurfaceView;

public class MyGLSurfaceView extensions GLSurfaceView{

    GLRender myRender;

    public MyGLSurfaceView (Context context) {
        super(context);
        myRender = new GLRender();
        setRenderer (myRenderer);
    }

}
  • GLRender.java
package com.example.ma2.opengles;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Typeface;
import android.opengl.GLSurfaceView;
import android.opengl.GLSurfaceView.Render;
import android.opengl.GLUtils;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class GLRender implements Render {

    @ Override
    public void onSurfaceCreated(GL10gl,EGLConfig config){

    }

    @ Override
    public void onSurfaceChanged(GL10gl, int width, int height) {
        Bitmap bmp;

        gl.glViewport(0,0, width, height);
        {
            // BitMap characters using Canvas
            bmp = createStrImage("123");
        }

        gl.glEnable (GL10.GL_TEXTURE_2D);
        int[] buffers = new int[1];
        gl.glGenTextures(1, buffers, 0);
        int textureName=buffers[0];
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureName);
        GLUtils.texImage2D (GL10.GL_TEXTURE_2D, 0, bmp, 0);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);

        bmp.recycle();

    }

    @ Override
    public void onDrawFrame (GL10gl) {

        gl.glClearColor (1.0f, 0.50f, 0.50f, 1.0f);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        floatuv [ ] = {
                0.0f, 0.0f, // upper left
                0.0f, 1.0f, // bottom left
                1.0f, 0.0f, // upper right
                1.0f, 1.0f, // bottom right
        };

        ByteBuffer bbuv=ByteBuffer.allocateDirect(uv.length*4);
        bbuv.order(ByteOrder.nativeOrder());

        FloatBuffer fbuv=bbuv.asFloatBuffer();
        fbuv.put(uv);
        fbuv.position(0);

        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glTexCoordPointer(2,GL10.GL_FLOAT,0,fbuv);
        float positions [] = {
                // !x yz
                - 1.0f, 1.0f, 0.0f, // upper left (corresponding to line 1 of uv)
                -1.0f, -1.0f, 0.0f, // bottom left (corresponding to line 2 of uv)
                1.0f, 1.0f, 0.0f, // upper right (corresponding to line uv3)
                1.0f, -1.0f, 0.0f, // bottom right (corresponding to line 4 of uv)
        };

        ByteBuffer bb = ByteBuffer.allocateDirect (positions.length*4);
        bb.order(ByteOrder.nativeOrder());
        FloatBuffer fb = bb.asFloatBuffer();
        fb.put(positions);
        fb.position(0);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
        gl.glVertexPointer(3,GL10.GL_FLOAT,0,fb);
        gl.glDrawArrays (GL10.GL_TRIANGLE_STRIP, 0, 4);

    }

    public Bitmap createStrImage(String str){
        Paint paint = new Paint (Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(Typeface.DEFAULT);
        paint.setTypeface(Typeface.createFromAsset(getAsets(), "fonts/ipag.ttf"));
        paint.setTextSize(30);

        Paint.FontMetrics fm=paint.getFontMetrics();
        paint.setARGB (255, 255, 255, 0);

        float width = paint.measureText(str);
        float height = -fm.top + fm.bottom;

        Bitmap bitmap = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_888);
        Canvas canvas = new Canvas (bitmap);
        canvas.drawText(str, 0, -fm.top, paint);

        return bitmap;
    }
}

java android

2022-09-30 21:34

1 Answers

Since getAssets() is actually a call such as Activity.getAssets(), you may need to pass a reference such as Activity with the createStrImage argument.


2022-09-30 21:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.