Android以编程方式中风 [英] Android Programmatically Stroke

查看:95
本文介绍了Android以编程方式中风的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android的文本上画一个黑色的笔画.

I want to draw a black stroke on my text in Android.

我看过这个例子: 如何使用Android中的MapView上有边框?

解决方案将覆盖onDraw()以创建笔划.

Where the solution overrides onDraw() to create the stroke.

问题是,我仍然相对较早地开始使用Android,而且我不知道如何过渡到使用该解决方案.

The problem is, I'm still relatively starting out in Android, and I have no idea how to transition to using that solution.

在我的onCreate中,我设置了文字字体(这是自定义的):

In my onCreate I set the text typeface (it's custom):

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

    requestWindowFeatures();

    // Set content view and component listeners
    setContentView(R.layout.meme_maker);
    setListeners();

    context = this;

    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
    TextView mmt = (TextView) findViewById(R.id.meme_maker_title);
    TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
    TextView tbc = (TextView) findViewById(R.id.bottom_text_canvas);

    ttc.setTypeface(tf);
    tbc.setTypeface(tf);
    mmt.setTypeface(tf);
}

我有一个onClickListener,它根据用户在TextEntry中编写他/她想要的文本并随后单击按钮来更改TextView的文本内容.

And I have an onClickListener where I change the text content of the TextView, based on the user writing the text he/she wants in a TextEntry and clicking a button afterwards.

ImageView ii = (ImageView) findViewById(R.id.insert_image);
    ii.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText tt = (EditText) findViewById(R.id.top_text_text);
            EditText bt = (EditText) findViewById(R.id.bottom_text_text);

            TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
            TextView btc = (TextView) findViewById(R.id.bottom_text_canvas);

            ttc.setText(tt.getText().toString().toUpperCase());
            btc.setText(bt.getText().toString().toUpperCase());
        }
    });

到目前为止,这非常简单.我的问题是:如何插入文字的笔触?在哪里?我需要创建一个Canvas和Paint对象吗?

It's pretty straightforward so far. My question is: how to insert the stroke of the text? Where? Do I need to create a Canvas and Paint objects?

推荐答案

为TextView中呈现的文本获取阴影的最简单方法是按照

The simplest way to get a shadow for text rendered in a TextView is to set up a style as described in this answer. That requires very little work and sounds like it will work fine in your situation.

使用链接到的技术涉及到扩展现有的View类,覆盖onDraw()并在传递给onDraw()的画布上使用Canvas.drawText()自己呈现文本.在某些情况下,这可能正是您所需要的,但是对于您当前的情况来说,这听起来太过分了.如果您想进一步研究,请有关此主题的Android开发人员指南是一本好书.

Using the technique you link to involves extending an existing View class, overriding onDraw(), and using Canvas.drawText() on the canvas passed to onDraw() to render the text yourself. That can be exactly what you need in some situations, but sounds like overkill for your current situation. If you want to look into it further, the Android dev guide on the subject is a good read.

这篇关于Android以编程方式中风的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆