如何制作带有纹理而不是文本颜色或渐变的文本(例如 png 文件)?就像是this。我理解逻辑,我应该使文本颜色透明并放在文本位图下方。我想我无法通过Textview
。我不知道如何使用画布或 OpenGL 来做到这一点。有什么想法吗?
这是一种使用方法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
- 设置文本
- 绘制
TextView
到画布上使用textView.draw(canvas);
- 而不是做
canvas.drawText()
use canvas.drawBitmap()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)