android纹理文本

android纹理文本,android,text,textures,Android,Text,Textures,如何使用纹理而不是文本颜色或渐变来制作文本(例如png文件)?差不多。我理解逻辑,我应该使文本颜色透明,并将其置于文本位图下。我想用Textview无法做到这一点。我不知道如何使用画布或OpenGL来实现它。有什么想法吗?这是JVitela的一个片段-答案 // Create an empty, mutable bitmap Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_4444); // get a can

如何使用纹理而不是文本颜色或渐变来制作文本(例如png文件)?差不多。我理解逻辑,我应该使文本颜色透明,并将其置于文本位图下。我想用
Textview
无法做到这一点。我不知道如何使用画布或OpenGL来实现它。有什么想法吗?

这是JVitela的一个片段-答案

 // Create an empty, mutable bitmap
Bitmap bitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_4444);

// get a canvas to paint over the bitmap
Canvas canvas = new Canvas(bitmap);
bitmap.eraseColor(0);

// get a background image from resources
// note the image format must match the bitmap format
Drawable background = context.getResources().getDrawable(R.drawable.background);
background.setBounds(0, 0, 256, 256);
background.draw(canvas); // draw the background to our bitmap

// Draw the text
Paint textPaint = new Paint();
textPaint.setTextSize(32);
textPaint.setAntiAlias(true);
textPaint.setARGB(0xff, 0×00, 0×00, 0×00);

// draw the text centered
canvas.drawText(“Hello World”, 16,112, textPaint);

//Generate one texture pointer…
gl.glGenTextures(1, textures, 0);

//…and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);

//Create Nearest Filtered Texture
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_LINEAR);

//Different possible texture parameters, e.g. GL10.GL_CLAMP_TO_EDGE
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);

//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

//Clean up
bitmap.recycle();

这里有一种使用PorterDuffXfermode的方法

public class MainActivity extends Activity {
    private EditText mEditText;
    private ImageView mImageView;
    private Bitmap mTexture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mEditText = (EditText) findViewById(R.id.activity_main_edittext);
        mImageView = (ImageView) findViewById(R.id.activity_main_image);

        mTexture = BitmapFactory.decodeResource(getResources(),
                R.drawable.texture);
    }

    public void onTextCreate(View v) {
        final String text = mEditText.getEditableText().toString();

        Bitmap result = Bitmap.createBitmap(mTexture.getWidth(),
                mTexture.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(200);
        paint.setARGB(255, 0, 0, 0);

        canvas.drawText(text, 200, 200, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(mTexture, 0, 0, paint);
        paint.setXfermode(null);

        mImageView.setImageBitmap(result);
    }
}
布局非常简单:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/activity_main_edittext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:hint="Write a sample text" />

    <ImageView
        android:id="@+id/activity_main_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:onClick="onTextCreate"
        android:text="Do it!" />

</RelativeLayout>

此代码使用
canvas.drawText()
编写文本。如果要使用常规的
文本视图
,可以:

  • 创建
    TextView
  • 设置文本
  • 使用
    TextView.draw(canvas)将
    TextView
    绘制到画布中
  • 使用
    canvas.drawBitmap()

    • 很抱歉这么晚才发布,但我想我会有所帮助。 因此,要制作纹理文本,例如,在布局/文件夹中有一个名为my_layout.xml的文件,其定义如下:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
          <TextView
          android:id="@+id/some_text_id"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:text="@string/some_text"/>
      
      </RelativeLayout>
      

      好问题!你能上传纹理吗?Macarse,我不明白你想从我这里得到什么纹理:)从我作为示例链接的图像中?@jumperOk:只是想要你用来测试我代码的纹理。我将用示例代码来回答。@jumper0k你们进展如何?我试过两种方法——你们的和Macarse的。它们都很好。但我接受Macarse的方法,因为它更简单,正如我所想,而且它不混合gl和canvas。也许对一个更有效率的人来说,你的方式就是这样。所以我昨天把你的答案标为有用。
      public class MainActivity extends Activity
      {
          @Override
          protected void onCreate(Bundle savedInstanceState)
          {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.my_layout);
              TextView welcome = (TextView) findViewById(R.id.some_text_id);
              Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.texture);
              Shader shader = new BitmapShader(bitmap,Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
              welcome.getPaint().setShader(shader);
      
          }
      }