使用


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);
    }
}

自定义控件 带描边的TextView的更多相关文章

  1. Android自定义控件 -- 带边框的TextView

    使用xml实现边框 原来使用带边框的TextView时一般都是用XML定义来完成,在drawable目录中定义如下所示的xml文件: <?xml version="1.0" ...

  2. 李洪强iOS开发之带placeHolder的Textview

    李洪强iOS开发之带placeHolder的Textview  01 - 创建工过程,定义全局属性,遵守textview的代理协议  02 - 添加一个textview和一个label 03 - 实现 ...

  3. Android 手机卫士--自定义控件(获取焦点的TextView)

    本文地址:http://www.cnblogs.com/wuyudong/p/5906735.html,转载请注明源地址. 本文将实现标题栏下面的textview中的文字跑马灯的效果,就是将一行文字水 ...

  4. 新浪微博客户端(36)-自定义带placeholder的TextView

    iOS 上自带的UITextView竟然不能设置placeholder,但是UITextView却可以,我也真是醉了.没办法了,自己写一个 DJTextView.h #import <UIKit ...

  5. android自定义控件实例(Linearlayout组合TextView和ImageView)

    2013-12-18 11:25:22 转载自: http://www.open-open.com/lib/view/open1328836804515.html 很多时候android常用的控件不能 ...

  6. Android 如何实现带滚动条的TextView,在更新文字时自动滚动到最后一行?

    1.在布局文件中放置一个TextView,给它添加scrollbars和fadeScrollbars两个属性. 如下设置:滚动条为垂直滚动条,并且一直可见(当TextView中的文字行数超过页面能显示 ...

  7. android 自定义组件-带图片的textView

    1. 定义属性 <?xml version="1.0" encoding="utf-8"?> <resources> <decla ...

  8. 学习下关于ViewStub实例的用法及带Drawable的TextView的妙用

    在项目中,我们可能有多种数据来源比如: 里面有ListView也有当获得数据为空的时候显示的空信息.根据点击的项目还是差事不同,显示的空消息也不同.a.没有收藏的项目,b目前没有收藏的差事. 其实实现 ...

  9. 跑马灯带你深入浅出TextView的源码世界

    一.背景 想必大家平时也没那么多时间是单独看源码,又或者只是单纯的看源码遇到问题还是不知道怎么从源码的角度解决. 但是大家平时开发过程中肯定会遇到这样或那样的小问题,通过百度.Google搜索都无果, ...

随机推荐

  1. The partial sum problem

    算法:搜索 描述 One day,Tom's girlfriend give him an array A which contains N integers and asked him:Can yo ...

  2. 新安装ubuntu后几项配置

    新安的ubuntu13.04 为了编程方便 进行如下设置 安装右键terminal    sudo apt-get install nautilus-open-terminal 安装远程连接ssh   ...

  3. PHP的抽象类和接口

    抽象类与接口相似,都是一种比较特殊的类.抽象类是一种特殊的类,而接口也是一种特殊的抽象类.它们通常配合面向对象的多态性一起使用.虽然声明和使用都比较容易,但它们的作用在理解上会困难一点. ①抽象类 在 ...

  4. 常用排序算法之——选择排序(C语言+VC6.0平台)

    选择排序是另一种经典排序算法,核心思想是:在一趟找最小(大)数的过程中,先假设待排数据中的第一个数据即为最小(大)数据,然后循环将其他数据与该数据比较,每次比较时若小于该数据则让新数据成为最小(大)数 ...

  5. iOS中常用的四种数据持久化方法简介

    iOS中常用的四种数据持久化方法简介 iOS中的数据持久化方式,基本上有以下四种:属性列表.对象归档.SQLite3和Core Data 1.属性列表涉及到的主要类:NSUserDefaults,一般 ...

  6. [POJ] 2226 Muddy Fields(二分图最小点覆盖)

    题目地址:http://poj.org/problem?id=2226 二分图的题目关键在于建图.因为“*”的地方只有两种木板覆盖方式:水平或竖直,所以运用这种方式进行二分.首先按行排列,算出每个&q ...

  7. 小公司生存,一般活过第一年,就能撑3年(读书笔记:成败关键,关键是你是否拥有现金流客户)good

    小公司生存,一般活过第一年,就能撑3年,一般能撑过3年,到第7年遇到坎,如果第7年突破,能活过10年,是想死也不容易,等到第二个七年,可以搞搞人事地震了,因为要二次创业. -------------- ...

  8. 转: linux下错误的捕获:errno和strerror的使用

    经常在调用linux 系统api 的时候会出现一些错误,比方说使用open() write() creat()之类的函数有些时候会返回-1,也就是调用失败,这个时候往往需要知道失败的原因.这个时候使用 ...

  9. 2015第24周四Spring事务4

    Spring配置文件中关于事务配置总是由三个组成部分,分别是DataSource.TransactionManager和代理机制这三部分,无论哪种配置方式,一般变化的只是代理机制这部分.     Da ...

  10. android调用系统自带的的浏览器搜索关键字

    //调用系统的浏览器搜索详情 public void jumpBrowser(String value) { /* 取得网页搜寻的intent */ Intent search = new Inten ...