自定义控件 带描边的TextView


使用
自定义控件 带描边的TextView
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stroke_textview);
        //在布局中使用
        ((StrokeTextView) findViewById(R.id.tv1)).append("\n支持append,支持图文混排");
        ((StrokeTextView) findViewById(R.id.tv2)).setStrokeColorAndWidth(0xff0000ff, 3f);

        //在代码中使用
        LinearLayout ll_root = (LinearLayout) findViewById(R.id.ll_root);
        //默认
        StrokeTextView strokeTextView3 = new StrokeTextView(this);
        strokeTextView3.setText("支持在代码中创建");
        strokeTextView3.setBackgroundColor(0x3f0000ff);
        strokeTextView3.setGravity(Gravity.CENTER);
        ll_root.addView(strokeTextView3);//默认是(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        //设置setPadding、setMargins
        StrokeTextView strokeTextView4 = new StrokeTextView(this);
        strokeTextView4.setText("设置Padding、Margins");
        strokeTextView4.setTextColor(0xff00ff00);
        strokeTextView4.setTextSize(20);
        strokeTextView4.setStrokeColorAndWidth(0xffff0000, 2.5f);
        strokeTextView4.setBackgroundColor(0x30ff00ff);
        strokeTextView4.setPadding(30, 0, 60, 0);
        LinearLayout.LayoutParams mLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        mLayoutParams.setMargins(30, 0, 0, 0);
        strokeTextView4.setLayoutParams(mLayoutParams);
        ll_root.addView(strokeTextView4);
    }
}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2000"
    android:orientation="vertical"
    android:padding="5dp" >
    <com.bqt.myview.StrokeTextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_launcher"
        android:text="带描边的TextView"
        android:textColor="#fff"
        android:textSize="18sp" />
    <com.bqt.myview.StrokeTextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text="支持设置padding,margin,textSize,宽高、textColor,背景,gravity,及sp,dp,px单位"
        android:textColor="#0f0"
        android:textSize="30px" />
</LinearLayout>

View
/*
 * 给文字加描边,实现方法是两个TextView叠加,只有描边的TextView放在底部,实体TextView叠加在上面
 * 当此控件被创建的同时创建另一个TextView,并将此TextView设为STROKE样式
 * 在测量onMeasure、绘制onDraw、布局onLayout自身时,使用同样的参数同样的方式处理此STROKE样式的TextView
 */
public class StrokeTextView extends TextView {
    /**描边的TextView*/
    private TextView strokeTextView = null;
    /**描边的TextView的颜色*/
    private int strokeColor = 0xffff0000;
    /**描边的TextView的宽度*/
    private float strokeWidth = 1.5f;

    /**
     * 设置描边的颜色和宽度
     */
    public void setStrokeColorAndWidth(int strokeColor, float strokeWidth) {
        this.strokeColor = strokeColor;
        this.strokeWidth = strokeWidth;
        init();//必须重新调用初始化方法
    }

    public StrokeTextView(Context context) {
        this(context, null);
    }
    public StrokeTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        strokeTextView = new TextView(context, attrs, defStyle);
        init();
    }
    public void init() {
        TextPaint textPaint = strokeTextView.getPaint();
        textPaint.setStrokeWidth(strokeWidth); //设置描边宽度
        strokeTextView.setTextColor(strokeColor); //设置描边颜色
        textPaint.setStyle(Style.STROKE); //对文字只描边
        invalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        strokeTextView.setText(getText());
        strokeTextView.setGravity(getGravity());
        strokeTextView.measure(widthMeasureSpec, heightMeasureSpec);
    }
    @Override
    //卧槽,必须把super放在后面
    protected void onDraw(Canvas canvas) {
        strokeTextView.draw(canvas);
        super.onDraw(canvas);
    }
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        strokeTextView.layout(left, top, right, bottom);
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
        strokeTextView.setLayoutParams(params);
    }
    @Override
    public void setBackgroundResource(int resid) {
        super.setBackgroundResource(resid);
        strokeTextView.setBackgroundResource(resid);
    }
    @Override
    public void setTextSize(float size) {
        super.setTextSize(size);
        strokeTextView.setTextSize(size);
    }
    @Override
    public void setTextSize(int unit, float size) {
        super.setTextSize(unit, size);
        strokeTextView.setTextSize(unit, size);
    }
    @Override
    public void setPadding(int left, int top, int right, int bottom) {
        super.setPadding(left, top, right, bottom);
        strokeTextView.setPadding(left, top, right, bottom);
    }
}

上一篇:C# IEnumberable & IQueryable 区别


下一篇:2013.11.15 初学ant构建