自定义TextView

在View中有一些比较重要的回调方法,但是只需要重写特定的方法

onFinishInflate()//从xml加载组件后回调
onSizeChanged(); //组件大小改变时回调
onMeasure();     //回调该方法来进行测量
onLayout();      //回调该方法来确定显示的位置
onTouchEvent();  //监听到触摸事件时回调

自定义控件的实现方法

方法一:对现有控件进行拓展

public class MyTextView extends TextView {

    private Paint mpaint1;
    private Paint mpaint2;
    private int mViewWidth = 0;  //用于获取整个View的宽度
    private LinearGradient mLinearGradient;
    private Matrix matrix;
    private int mtranslate = 0;  //用于记录渲染的偏移量

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mpaint1 = new Paint();
        mpaint1.setColor(getResources().getColor(
                android.R.color.holo_blue_light));
        mpaint1.setStyle(Paint.Style.FILL);
        mpaint2 = new Paint();
        mpaint2.setColor(Color.YELLOW);
        mpaint2.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //绘制外层矩形
        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mpaint1);

        //绘制内层矩形
        canvas.drawRect(10, 10, getMeasuredWidth() - 10,
                getMeasuredHeight() - 10, mpaint2);

        canvas.save();
        //绘制文字前平移10像素
        canvas.translate(10, 0);
        //绘制文本
        super.onDraw(canvas);
        canvas.restore();

        if (matrix != null){
            mtranslate += mViewWidth / 5;
            //循环
            if(mtranslate > 2 * mViewWidth){
                mtranslate = -mViewWidth;
            }
            matrix.setTranslate(mtranslate,0);
            mLinearGradient.setLocalMatrix(matrix);
            postInvalidateDelayed(100);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //通过这个条件判断,可以保证只在初始化的时候调用一次
        if (mViewWidth == 0) {
            mViewWidth = getMeasuredWidth();
            if (mViewWidth > 0) {

                //创建渐变渲染器
                mLinearGradient = new LinearGradient(0, 0, mViewWidth, 0,
                        new int[]{Color.GREEN, 0xffffffff, Color.RED}, null,
                        Shader.TileMode.CLAMP);
                //对当前View的paint设置渲染
                getPaint().setShader(mLinearGradient);
                matrix = new Matrix();
            }
        }
    }
}

方法二:通过组合实现新的控件

方法三:重写View来实现全新的控件

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android自定义TextView中显示全部内容,可以使用以下两种方法: 1. 使用setEllipsize()方法 通过设置setEllipsize()方法,可以在TextView的末尾添加省略号,从而指示文本被截断。你可以使用以下代码来实现: ``` yourTextView.setEllipsize(TextUtils.TruncateAt.END); yourTextView.setSingleLine(true); ``` 上述代码将设置TextView只显示一行并在末尾添加省略号。 2. 自定义TextView 你可以从TextView类继承一个新类,并覆盖onMeasure()方法以测量控件的高度和宽度。 你可以使用以下代码实现: ``` public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取TextView的内容 CharSequence text = getText(); if (text != null) { //测量TextView的高度 int width = getMeasuredWidth(); int height = getMeasuredHeight(); int lineCount = getLineCount(); int lineHeight = getLineHeight(); int totalHeight = lineCount * lineHeight; if (totalHeight > height) { setMeasuredDimension(width, totalHeight); } } } } ``` 上述代码将测量TextView的高度,如果文本的高度超出了TextView的高度,则调整TextView的高度以适应文本。然后你可以使用此自定义TextView来显示你的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值