Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep text inside diamond shaped background

I'm drawing a blank here!

I have a textview with a diamond shaped background. The text needs to be contained within the diamond. Like this.

textview_with_diamond_shaped_bg

How do I do this? Is there a way to define the bounds of the text inside a textview?

like image 873
Kong Far Avatar asked May 31 '26 03:05

Kong Far


1 Answers

Edit

Put the below code in a java class like DiamondTextView:

public class DiamondTextView extends TextView{

    public DiamondTextView(Context ctx, AttributeSet attrs) {
        super(ctx, attrs);
    }

    Paint mBorderPaint = new Paint();

        @Override
        protected void onDraw(Canvas canvas) {

            mPath.moveTo(mWidth/2 , 0);
            mPath.lineTo(mWidth , mHeight/2);
            mPath.lineTo(mWidth /2 , mHeight);
            mPath.lineTo(0 , mHeight/2);
            mPath.lineTo( mWidth/2 ,0);

            //setup the paint for fill
            mBorderPaint.setAlpha(255);
            mBorderPaint.setColor(mBorderColor);
            mBorderPaint.setStyle(Paint.Style.FILL);
            borderPaint.setStrokeWidth(mBorderWidth);

            //draw it
            canvas.drawPath(mPath ,mBorderPaint);

            //setup the paint for stroke
            mBorderPaint.setAlpha(51);
            mBorderPaint.setStyle(Paint.Style.STROKE);

            //draw it again
            canvas.drawPath(mPath ,mBorderPaint);

            super.onDraw(canvas);
        }
}

and call it as where you want to use this textView :

 <com.erginus.ABC.Commons.DiamondTextView...../>

For more infomation look here:http://developer.android.com/reference/android/graphics/drawable/shapes/PathShape.html

You can use the following library too: https://github.com/siyamed/android-shape-imageview

like image 74
Android Geek Avatar answered Jun 01 '26 18:06

Android Geek