欢迎使用CSDN-markdown编辑器

Android自定义的View显示Text,但未实现换行

  • 显示文字

代码块

import java.util.List;
import com.dts.app.levelreader.R;
import com.dts.app.levelreader.entity.Word;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

/***
 * zdy a text view for display article
 * 
 * @author LD
 * @date 2015-05
 */
public class ATextView extends TextView {
    /** TAG **/
    private String TAG = "ATextView";
    /** Paint for text **/
    private Paint mTextPaint;
    /** text content **/
    private String mText;
    /** the height for text from baseline **/
    private int mAscent;

    @SuppressWarnings("unused")
    private List<Word> words;

    /**
     * Constructor. This version is only needed if you will be instantiating the
     * object manually (not from a layout XML file).
     * 
     * @param context
     */
    public ATextView(Context context) {
        super(context);
        initLabelView();
    }

    /**
     * Construct object, initializing with any attributes we understand from a
     * layout file. These attributes are defined in
     * SDK/assets/res/any/classes.xml.
     * 
     * @see android.view.View#View(android.content.Context,
     *      android.util.AttributeSet)
     */
    public ATextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLabelView();
        /**
         * get textView TypedArray
         * 
         ***/
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ATextView);
        /***
         * get text string; CharSequence is a interface
         */
        CharSequence s = a.getString(R.styleable.ATextView_AText);
        if (s != null) {
            setText(s.toString());
        }
        /**
         * get color for text
         */
        setTextColor(a.getColor(R.styleable.ATextView_ATextColor, Color.WHITE));
        /**
         * get size for text
         */
        int textSize = a.getDimensionPixelOffset(
                R.styleable.ATextView_ATextSize, 0);
        if (textSize > 0) {
            setTextSize(textSize);
        }
        /**
         * must do this
         */
        a.recycle();
    }

    /**
     * init a label view for if not exist a attr
     */
    private final void initLabelView() {
        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
        mTextPaint.setColor(Color.WHITE);
        setPadding(5, 3, 3, 5);
    }

    /***
     * for other activity use
     * 
     * @param words
     */
    public void setWords(List<Word> words) {
        this.words = words;
    }

    /**
     * Sets the text to display in this label
     * 
     * @param text
     *            The text to display. This will be drawn as one line.
     */
    public void setText(String text) {
        Log.i(TAG, "setText");
        mText = text;

        requestLayout();
        invalidate();
    }

    /**
     * Sets the text size for this label
     * 
     * @param size
     *            Font size
     */
    public void setTextSize(int size) {
        mTextPaint.setTextSize(size);
        requestLayout();
        invalidate();
    }

    /**
     * Sets the text color for this label.
     * 
     * @param color
     *            ARGB value for the text
     */
    public void setTextColor(int color) {
        mTextPaint.setColor(color);
        invalidate();
    }

    /**
     * @see android.view.View#measure(int, int)
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.i(TAG, "onMeasure");

        setMeasuredDimension(measureWidth(widthMeasureSpec),
                measureHeight(heightMeasureSpec));
    }

    /**
     * Determines the width of this view
     * 
     * @param measureSpec
     *            A measureSpec packed into an int
     * @return The width of the view, honoring constraints from measureSpec
     */
    private int measureWidth(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        if (specMode == MeasureSpec.EXACTLY) {
            /**
             * parent must want to do
             */
            result = specSize;
        } else {
            result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
                    + getPaddingRight();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result;
    }

    /**
     * Determines the height of this view
     * 
     * @param measureSpec
     *            A measureSpec packed into an int
     * @return The height of the view, honoring constraints from measureSpec
     */
    private int measureHeight(int measureSpec) {
        int result = 0;
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);

        mAscent = (int) mTextPaint.ascent();
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        } else {
            result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
                    + getPaddingBottom();
            if (specMode == MeasureSpec.AT_MOST) {
                result = Math.min(result, specSize);
            }
        }
        return result * 10;
    }

    /**
     * Render the text
     * 
     * @see android.view.View#onDraw(android.graphics.Canvas)
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG, "onDraw");
        // Paint mPaint = new Paint();
        // mPaint.setColor(Color.YELLOW);
        // canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(),
        // mPaint);
        canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent,
                mTextPaint);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值