自定义View(二)

来源:互联网 发布:大数据分析和数据挖掘 编辑:程序博客网 时间:2024/05/21 06:15

demo来源:

http://blog.csdn.net/lmj623565791/article/details/45460089;

效果图

这里写图片描述

学习目的:

  • 学习onMeasure()测量
  • 学习绘制text
  • 学习自定义属性,如枚举等等

自定义属性attrs.xml

  <declare-styleable name="CustomedImageTextView">        <attr name="text" format="reference|string"/>        <attr name="textColor" format="reference|color"/>        <attr name="textSize" format="reference|dimension"/>        <attr name="imageScaleType" format="enum">            <enum name="center" value="0"/>            <enum name="fillXY" value="1"/>        </attr>        <attr name="image" format="reference"/>    </declare-styleable>

xml布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              xmlns:app="http://schemas.android.com/apk/res-auto"              android:layout_width="match_parent"              android:layout_height="match_parent"              android:gravity="center_horizontal"              android:orientation="vertical">    <com.example.showdy.customedview.customed_view_1.widget.CustomedImageTextView        android:layout_width="250dp"        android:layout_height="wrap_content"        android:padding="10dp"        app:text="劳其筋骨,饥其匹夫,枯发齐声,空乏其身"        app:textColor="#002345"        app:image="@drawable/girl"        app:imageScaleType="center"        app:textSize="18sp"/>    <com.example.showdy.customedview.customed_view_1.widget.CustomedImageTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:text="劳其筋骨,饥其匹夫,枯发齐声"        app:textColor="#2245cb"        app:image="@drawable/ic_launcher"        app:imageScaleType="center"        app:textSize="18sp"        android:padding="10dp"        android:layout_marginTop="20dp"/>    <com.example.showdy.customedview.customed_view_1.widget.CustomedImageTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:text="天将降大任"        app:textColor="#45ff98"        app:image="@drawable/ic_launcher"        app:imageScaleType="fillXY"        app:textSize="18sp"        android:padding="10dp"        android:layout_marginTop="20dp"/></LinearLayout>

实现代码:

public class CustomedImageTextView extends View {    private static final int CENTER = 0;    private static final int FIX_XY = 1;    private String mText;    private int mTextSize;    private Bitmap mImage;    private int mImageScaleType;    private int mTextColor;    private Paint mPaint;    private Rect mTextBound;    private int mDesireWidth;    private int mDesireHeight;    private Rect mRect;    public CustomedImageTextView(Context context) {        this(context, null);    }    public CustomedImageTextView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CustomedImageTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomedImageTextView);        mText = array.getString(R.styleable.CustomedImageTextView_text);        mTextSize = array.getDimensionPixelOffset(R.styleable.CustomedImageTextView_textSize,                (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 24, getResources().getDisplayMetrics()));        mTextColor = array.getColor(R.styleable.CustomedImageTextView_textColor, Color.BLACK);        mImageScaleType = array.getInt(R.styleable.CustomedImageTextView_imageScaleType, 0);        mImage= BitmapFactory.decodeResource(getResources(),array.getResourceId(R.styleable.CustomedImageTextView_image, 0));        array.recycle();        mTextBound = new Rect();        mRect = new Rect();        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);        mPaint.setTextSize(mTextSize);        mPaint.getTextBounds(mText, 0, mText.length(), mTextBound);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int desireWith = View.resolveSize(getDesireWidth(widthMeasureSpec), widthMeasureSpec);        int desireHeight = View.resolveSize(getDesireHeight(heightMeasureSpec), heightMeasureSpec);        setMeasuredDimension(desireWith, desireHeight);    }    public int getDesireWidth(int widthMeasureSpec) {        //获取图片的宽度        int imageWidth = getPaddingLeft() + getPaddingRight() + mImage.getWidth();        int textWidth = getPaddingLeft() + getPaddingRight() + mTextBound.width();        mDesireWidth = Math.max(imageWidth, textWidth);        int mode = MeasureSpec.getMode(widthMeasureSpec);        int size = MeasureSpec.getSize(widthMeasureSpec);        if (mode == MeasureSpec.EXACTLY || mode == MeasureSpec.AT_MOST) {            if (size < mDesireWidth) {                mDesireWidth = size;            }        }        return mDesireWidth;    }    public int getDesireHeight(int heightMeasureSpec) {        mDesireHeight = getPaddingBottom() + getPaddingBottom() + mTextBound.height() + mImage.getHeight();        int mode = MeasureSpec.getMode(heightMeasureSpec);        int size = MeasureSpec.getSize(heightMeasureSpec);        if (mode == MeasureSpec.EXACTLY || mode == MeasureSpec.AT_MOST) {            if (size < mDesireHeight) {                mDesireHeight = size;            }        }        return mDesireHeight;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //绘制边框        mPaint.setColor(Color.RED);        mPaint.setStyle(Paint.Style.STROKE);        mPaint.setStrokeWidth(8);        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);        //绘制文字        mPaint.setStyle(Paint.Style.FILL);        mPaint.setColor(mTextColor);        mRect.left = getPaddingLeft();        mRect.top = getPaddingTop();        mRect.right = mDesireWidth - getPaddingRight();        mRect.bottom = mDesireHeight - getPaddingBottom();        //若当前设置的宽度小于文字的宽度,将字体改为Xxx        if (mTextBound.width() > mDesireWidth) {            TextPaint paint= new TextPaint(mPaint);            String msg = TextUtils.ellipsize(mText, paint, mDesireWidth - getPaddingRight() - getPaddingLeft(),                    TextUtils.TruncateAt.END).toString();            canvas.drawText(msg,getPaddingLeft(),mDesireHeight-getPaddingBottom(),mPaint);        } else {            canvas.drawText(mText, mDesireWidth / 2.0f - mTextBound.width() / 2.0f, mDesireHeight - getPaddingBottom(), mPaint);        }        //除去已经使用的text的rect        mRect.bottom -= mTextBound.height();        switch (mImageScaleType) {            case CENTER:                //重新计算mRect的坐标值                mRect.left = (int) (mDesireWidth / 2.0f - mImage.getWidth() / 2.0f);                mRect.top = (int) ((mDesireHeight - mTextBound.height()) / 2.0f - mImage.getHeight() / 2.0f);                mRect.right = (int) (mDesireWidth / 2.0f + mImage.getWidth() / 2.0f);                mRect.bottom = (int) ((mDesireHeight - mTextBound.height()) / 2.0f + mImage.getHeight() / 2.0f);                canvas.drawBitmap(mImage, null, mRect, mPaint);                break;            case FIX_XY:                canvas.drawBitmap(mImage, null, mRect, mPaint);                break;            default:                break;        }    }}
0 0
原创粉丝点击