一. 實(shí)現(xiàn)很簡(jiǎn)單,就是對(duì)顏色進(jìn)行線性的漸變。用到的類是LinearGradient.直接貼代碼:
二. 實(shí)例圖
public class Test extends Activity { private TextViewTest mTextViewTest; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTextViewTest = new TextViewTest(this); setContentView(mTextViewTest); } public class TextViewTest extends TextView { private LinearGradient mGradient; private int[] mGradientColors = { Color.RED, Color.rgb(255, 165, 0), Color.YELLOW, Color.GREEN, Color.rgb(0, 255, 255), Color.BLUE, Color.rgb(160, 32, 240) }; private int mTitleWidth; private String mTitle = "三十六天罡,七十二地煞,乃是天地正邪之氣所生,如今奉命下凡轉(zhuǎn)世為科學(xué)家.但法用以正,則可以造福人群;"; public TextViewTest(Context context) { super(context); // TODO Auto-generated constructor stub setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); setWillNotDraw(false); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); measure(0, 0); Paint paint = new Paint(); paint.setStyle(Style.FILL_AND_STROKE); mGradient = new LinearGradient(0, 0, getWidth(), getMeasuredHeight(), mGradientColors, null, TileMode.REPEAT); paint.setShader(mGradient); mTitleWidth = 0; float[] width = new float[mTitle.length()]; paint.getTextWidths(mTitle, width); Log.i("Tag", "Width.length= " + width.length); int start = 0; int offsetY = 10; for (int j = 0; j < mTitle.length(); j++) { mTitleWidth += (int) Math.ceil(width[j]); if (mTitleWidth > getWidth()) { canvas.drawText(mTitle.substring(start, j), 0, offsetY, paint); mTitleWidth = 0; start = start + j; offsetY = offsetY + getMeasuredHeight() + 5; j--; } } if (mTitleWidth > 0) { canvas.drawText(mTitle.substring(start), 0, offsetY, paint); } } } }
二. 實(shí)例圖

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
