android带纹理的文本 [英] android textured text
本文介绍了android带纹理的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何制作带有纹理而不是文本颜色或渐变的文本(例如png文件)?类似于此.我理解逻辑,我应该使文本颜色透明并放在文本位图下.我想我无法通过 Textview
实现这一目标.而且我不知道如何使用画布或OpenGL.有什么想法吗?
How can I make a text with texture instead of text color or gradient(for example png file)? Something like this. I understand the logic, that I should make text color transparency and put under the text bitmap. I think I can't achive this with a Textview
. And I don't know how to do it with a canvas or OpenGL. Any ideas please?
推荐答案
这是使用 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
,则可以:
This code with write text using canvas.drawText()
. If you want to use a regular TextView
, you can:
- 创建
TextView
- 设置文字
- 使用
textView.draw(canvas);
将 - 使用
canvas.drawBitmap()
代替执行
TextView
绘制到画布中 canvas.drawText()
这篇关于android带纹理的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文