与canvas.draw的x坐标,自定义textview不起作用

时间:2016-03-14 11:48:47

标签: android android-canvas textview android-custom-view android-custom-attributes

我成功创建了This question(left drawable alignment with text in andorid)的答案 但问题是canvas.drawText x坐标不能正常工作,因为x坐标不会影响文本的位置。

这是在drawable上重叠文本的结果, enter image description here

我只想删除重叠并在画布上给出drawable和text之间的距离。

以下是我的代码,

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
android.widget.TextView;
import cl.tt.R;

public class CenterDrawableButton extends TextView{
private Drawable mDrawableCenter;

public CenterDrawableButton(Context context) {
    super(context);
    init(context, null);
}

public CenterDrawableButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

public CenterDrawableButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CenterDrawableButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs){
    if(attrs!=null){
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CenterDrawableButton, 0, 0);
        try {
         setCenterDrawable(a.getDrawable(R.styleable.CenterDrawableButton_drawableCenter));
        } finally {
            a.recycle();
        }
    }
}

public void setCenterDrawable(@Nullable Drawable center) {
    int[] state;
    state = getDrawableState();
    if (center != null) {
        center.setState(state);
        center.setBounds(0, 0, center.getIntrinsicWidth(), center.getIntrinsicHeight());
        center.setCallback(this);
    }
    mDrawableCenter = center;
    invalidate();
    requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    if (mDrawableCenter != null) {
        setMeasuredDimension(Math.max(getMeasuredWidth(), mDrawableCenter.getIntrinsicWidth()),
                Math.max(getMeasuredHeight(), mDrawableCenter.getIntrinsicHeight()));
    }
}

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (mDrawableCenter != null) {
        int[] state = getDrawableState();
        mDrawableCenter.setState(state);
        mDrawableCenter.setBounds(0, 0, mDrawableCenter.getIntrinsicWidth(),
                mDrawableCenter.getIntrinsicHeight());
    }
    invalidate();
}

@Override
protected void onDraw(@NonNull Canvas canvas) {
    super.onDraw(canvas);
    if (mDrawableCenter != null) {
        float textWidth = getPaint().measureText((String)getText());
        int newPosition=getWidth()-((int)textWidth+getPaddingLeft() + getPaddingRight()+mDrawableCenter.getIntrinsicWidth()+ (int)getResources().getDimension(R.dimen._5sdp));
        if(newPosition>0) {
            canvas.translate(newPosition, (getHeight() - mDrawableCenter.getIntrinsicHeight()) / 2);
            mDrawableCenter.draw(canvas);
            canvas.save();
            Paint textPaint = new Paint();
            int yPos = (int) ((getHeight() - ((textPaint.descent() + textPaint.ascent())) / 2));
            canvas.drawText(getText().toString(), newPosition, yPos, textPaint);
            canvas.restore();
        }else
        {
            int gnewPosition=getPaddingLeft() + getPaddingRight()+(int)getResources().getDimension(R.dimen._5sdp);
            canvas.translate(gnewPosition, (getHeight() - mDrawableCenter.getIntrinsicHeight()) / 2);
            mDrawableCenter.draw(canvas);
            canvas.save();
            int nggewPosition=getPaddingLeft() + getPaddingRight()+mDrawableCenter.getIntrinsicWidth()+ (int)getResources().getDimension(R.dimen._5sdp);
            Paint textPaint = new Paint();
            int yPos = (int) (getHeight() - ((textPaint.descent() + textPaint.ascent())) / 2);
            canvas.translate(nggewPosition, yPos);
            canvas.drawText(getText().toString(), nggewPosition, yPos, textPaint);
            canvas.restore();
        }
    }
}

}

这是attr,

 <attr name="drawableCenter" format="reference"/>
<declare-styleable name="CenterDrawableButton">
    <attr name="drawableCenter"/>
</declare-styleable>

0 个答案:

没有答案