【问题标题】:Highlight text view in android在android中突出显示文本视图
【发布时间】:2013-04-15 12:39:29
【问题描述】:

我想在 android 中突出显示 textview 与此图像相同

我是怎么做到的?谢谢

喜欢这个链接http://youtube.com/watch?v=0qE3egNettY

【问题讨论】:

标签: android textview


【解决方案1】:

这就是你想要的:

package org.pskink.shape;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Region;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PictureDrawable;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.widget.TextView;

public class DrawableSpanTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawable_span_test);
        TextView tv = (TextView) findViewById(R.id.text);
        Spannable text = new SpannableStringBuilder("This is liric of the song");
        setSpans(tv, text);
        tv.setText(text);
    }

    private void setSpans(TextView tv, Spannable text) {
        int blueColor = 0xff0000ff;
        int redColor = 0xffff0000;
        ForegroundColorSpan blue = new ForegroundColorSpan(blueColor);
        ForegroundColorSpan red = new ForegroundColorSpan(redColor);
        HalfColorSpan o = new HalfColorSpan("o", tv, blueColor, redColor);
        text.setSpan(blue, 0, 14, 0);
        text.setSpan(o, 14, 15, 0);
        text.setSpan(red, 15, text.length(), 0);
    }

    class HalfColorSpan extends DynamicDrawableSpan {
        private final static String TAG = "DrawableSpanTest.HalfColorSpan";
        Picture mPicture;
        public HalfColorSpan(String text, TextView tv, int c0, int c1) {
            super(ALIGN_BASELINE);
            mPicture = new Picture();
            TextPaint p = tv.getPaint();
            Rect bounds = new Rect();
            p.getTextBounds(text, 0, text.length(), bounds);
            float width = p.measureText(text);
            float height = bounds.height();
            float y = height;
            Canvas c = mPicture.beginRecording((int) width, (int) height);
            c.save(Canvas.CLIP_SAVE_FLAG);
//          c.drawColor(0x22000000);
            p = new TextPaint(p);
            p.setColor(c0);
            c.clipRect(0, 0, width / 2, height, Region.Op.REPLACE);
            c.drawText(text, 0, y, p);
            p.setColor(c1);
            c.clipRect(width / 2, 0, width, height, Region.Op.REPLACE);
            c.drawText(text, 0, y, p);
            c.restore();
            mPicture.endRecording();
        }
        @Override
        public Drawable getDrawable() {
            PictureDrawable d = new PictureDrawable(mPicture);
            d.setBounds(0, 0, mPicture.getWidth(), mPicture.getHeight());
            return d;
        }
    }
}

和布局/drawable_span_test.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/text"
    android:textSize="50dip"
    android:gravity="center"
    android:background="#eee"
/>

结果是:

【讨论】:

  • 你能给我一个public ForegroundColorSpan (Parcel src)contruct的例子吗,谢谢
  • 抱歉,我没有注意到“o”是半红半蓝,在这种情况下您可以使用 DynamicDrawableSpan,请参阅我编辑的答案
  • 嗨@pskink,你能把textview 的spanable 动画和ariang karaoke 一样吗?
  • @DuHuynh 不知道 ariang karaoke 是什么
  • 所以我解释说:使用 canvas.drawText 两次,每个 tume 具有不同的前景色
【解决方案2】:

试试这个,不完全是你想要的,但应该做的工作:

String text = "<font color=#cc0030>This is layrics o</font> <font color=#ffff00>f the song</font>";
textview.setText(Html.fromHtml(text));

【讨论】:

  • 我想突出显示“o”字符同一图像的一部分。 “o”字符的每个身体颜色。谢谢
  • 我认为解决方案是使用Canvas 进行一些剪辑。
【解决方案3】:

你也可以使用安卓Spannable

例如this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-25
    • 2015-05-29
    • 2012-11-08
    • 1970-01-01
    • 2012-11-30
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多