图片和文字居中


Custom drawn Android button which aligns left drawable and its text to center.

https://gist.github.com/rajivnarayana/5224881

package com.webileapps.myrtprofile;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;
 
/**
 * 
 * @author Rajiv
 * 
 * A Button which aligns its text and left drawable at center. Especially useful when we have buttons which don't wrap.
 * 
 * usage:
 * 
 * <com.webileapps.myrtprofile.DrawableAlignedButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableLeft="@drawable/plone"
            android:gravity="center"
            android:padding="7dp"
            android:text="Text" />
 */
public class DrawableAlignedButton extends Button {
 
  public DrawableAlignedButton(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public DrawableAlignedButton(Context context) {
		super(context);
	}
 
	public DrawableAlignedButton(Context context, AttributeSet attrs, int style) {
		super(context, attrs, style);
	}
 
	private Drawable mLeftDrawable;
	
	@Override
  //Overriden to work only with a left drawable.
	public void setCompoundDrawablesWithIntrinsicBounds(Drawable left,
			Drawable top, Drawable right, Drawable bottom) {
		if(left == null) return;
		left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
		mLeftDrawable = left;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		//transform the canvas so we can draw both image and text at center.
		canvas.save();
		canvas.translate(2+mLeftDrawable.getIntrinsicWidth()/2, 0);
		super.onDraw(canvas);
		canvas.restore();
		canvas.save();
		int widthOfText = (int)getPaint().measureText(getText().toString());
		int left = (getWidth()+widthOfText)/2 - mLeftDrawable.getIntrinsicWidth() - 2;
		canvas.translate(left, (getHeight()-mLeftDrawable.getIntrinsicHeight())/2);
		mLeftDrawable.draw(canvas);
		canvas.restore();
	}
 
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int height = getMeasuredHeight();
		height = Math.max(height, mLeftDrawable.getIntrinsicHeight() + getPaddingTop() + getPaddingBottom());
		setMeasuredDimension(getMeasuredWidth(), height);
	}
}


CenterImageTextButton
http://stackoverflow.com/questions/13723236/how-to-create-button-with-centered-text-and-image-and-size-set-to-match-parent

public class CenterImageTextButton extends Button {
    private Paint mPaint = new Paint();
    private String mText = null;
    private float mTextSize = 0;

    public CenterImageTextButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public CenterImageTextButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CenterImageTextButton(Context context) {
        super(context); 
    }

    @Override
    public void onDraw(Canvas canvas) {

        mText = getText().toString();
        mTextSize = getTextSize();

        mPaint.setStyle(Style.FILL); 
        mPaint.setColor(getCurrentTextColor());        

        // get image top
        Drawable drawable = getCompoundDrawables()[1];
        Drawable curDrawable = null;
        if (drawable instanceof StateListDrawable)
            curDrawable = ((StateListDrawable)drawable).getCurrent();
        else
            curDrawable = ((BitmapDrawable)drawable).getCurrent();
        Bitmap image = ((BitmapDrawable)curDrawable).getBitmap();

        // call default drawing method without image/text
        setText("");
        setCompoundDrawables(null, null, null, null);
        super.onDraw(canvas);
        setText(mText);
        setCompoundDrawables(null, drawable, null, null);

        // get measurements of button and Image
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int imgWidth = image.getWidth();
        int imgHeight = image.getHeight();

        // get measurements of text
        //float densityMultiplier = getContext().getResources().getDisplayMetrics().density;
        //float scaledPx = textSize * densityMultiplier;
        //paint.setTextSize(scaledPx);
        mPaint.setTextSize(mTextSize);
        float textWidth = mPaint.measureText(mText);

        // draw Image and text
        float groupHeight = imgHeight + mTextSize;
        canvas.drawBitmap(image, (width - imgWidth) / 2, (height - groupHeight) / 2, null);
        canvas.drawText(mText, (width - textWidth) / 2, mTextSize + (height - groupHeight) / 2 + imgHeight, mPaint);    
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值