標籤:文本 最佳化 技術 ret 處理 isp shader match 依賴
直接上代碼
SGTextView.java
1 package com.example.tv.view; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.LinearGradient; 7 import android.graphics.Paint; 8 import android.graphics.Shader; 9 import android.graphics.Shader.TileMode;10 import android.util.AttributeSet;11 import android.widget.TextView;12 13 public class SGTextView extends TextView {14 private Paint strokPaint = new Paint();15 private Paint gradientPaint = new Paint();16 17 public SGTextView(Context context) {18 this(context, null);19 }20 21 public SGTextView(Context context, AttributeSet attrs) {22 super(context, attrs);23 }24 25 public void setStyle(String strokeColor, String startColor,26 String endColor, float strokewidthDp, int gradientHeighDp) {27 setStyle(28 Color.parseColor(strokeColor),29 DimensUtils.dip2px(getContext(), strokewidthDp),30 new LinearGradient(0, 0, 0, DimensUtils.dip2px(getContext(),31 gradientHeighDp), new int[] {32 Color.parseColor(startColor),33 Color.parseColor(endColor) }, null, TileMode.CLAMP));34 }35 36 public void setStyle(int strokeColor, float strokewidth, Shader shader) {37 38 strokPaint.setAntiAlias(true);39 // 設定是否使用映像抖動處理,會使繪製出來的圖片顏色更加平滑和飽滿,映像更加清晰40 strokPaint.setDither(true);41 // 如果該項設定為true,則映像在動畫進行中會濾掉對Bitmap映像的最佳化操作,加快顯示42 // 速度,本設定項依賴於dither和xfermode的設定43 strokPaint.setFilterBitmap(true);44 45 strokPaint.setStrokeWidth(strokewidth);46 strokPaint.setColor(strokeColor);47 // 設定繪製時各圖形的結合方式,如凹凸貼圖等48 strokPaint.setStrokeJoin(Paint.Join.ROUND);49 // 當畫筆樣式為STROKE或FILL_OR_STROKE時,設定筆刷的圖形樣式,如圓形樣式50 // Cap.ROUND,或方形樣式Cap.SQUARE51 strokPaint.setStrokeCap(Paint.Cap.ROUND);52 strokPaint.setStyle(Paint.Style.STROKE);53 54 gradientPaint.setAntiAlias(true);55 gradientPaint.setDither(true);56 gradientPaint.setFilterBitmap(true);57 gradientPaint.setShader(shader);58 gradientPaint.setStrokeJoin(Paint.Join.ROUND);59 gradientPaint.setStrokeCap(Paint.Cap.ROUND);60 gradientPaint.setStyle(Paint.Style.FILL_AND_STROKE);61 62 float textsize = getTextSize();63 strokPaint.setTextSize(textsize);64 gradientPaint.setTextSize(textsize);65 66 }67 68 public void setShadowLayer(float radius, float dx, float dy, String color) {69 strokPaint.setShadowLayer(radius, dx, dy, Color.parseColor(color));70 }71 72 @Override73 protected void onDraw(Canvas canvas) {74 75 String text = getText().toString();76 int width = getMeasuredWidth();77 if (width == 0) {78 measure(0, 0);79 width = (int) (getMeasuredWidth() + strokPaint.getStrokeWidth() * 2);80 setWidth(width);81 }82 83 float y = getBaseline();84 float x = (width - strokPaint.measureText(text)) / 2;85 86 canvas.drawText(text, x, y, strokPaint);87 canvas.drawText(text, x, y, gradientPaint);88 }89 }
DimensUtils.java
1 package com.example.tv.view; 2 3 import android.content.Context; 4 5 public final class DimensUtils { 6 private DimensUtils() { 7 } 8 9 public static int px2dip(Context context, float pxValue) {10 final float scale = context.getResources().getDisplayMetrics().density;11 return (int) (pxValue / scale + 0.5f);12 }13 14 public static int dip2px(Context context, float dipValue) {15 final float scale = context.getResources().getDisplayMetrics().density;16 return (int) (dipValue * scale + 0.5f);17 }18 19 public static int px2sp(Context context, float pxValue) {20 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;21 return (int) (pxValue / fontScale + 0.5f);22 }23 24 public static int sp2px(Context context, float spValue) {25 final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;26 return (int) (spValue * fontScale + 0.5f);27 }28 29 }
下面在布局中使用它
1 <com.example.tv.view.SGTextView2 android:id="@+id/tv_tishis"3 android:layout_width="wrap_content"4 android:text="換台中"5 android:layout_height="match_parent"6 android:textSize="15sp"7 android:gravity="center"8 android:visibility="gone"9 android:layout_centerHorizontal="true" />
在代碼中設定顏色以及陰影
1 tvMainPeogressBar = (SGTextView) findViewById(R.id.tv_tishis);2 tvMainPeogressBar.setTextSize(43);//字型大小3 tvMainPeogressBar.setText("換台中……");//常值內容4 tvMainPeogressBar.setStyle("#ded8cd", "#7d7a74", "#a1969d", 3, 9);//漸層顏色5 tvMainPeogressBar.setShadowLayer(2, 0, 2, "#000000");//陰影
Android學習筆記---自訂TextView實現陰影製作效果