How make a banner or ribbon with textview or any layout [closed]

孤街醉人 提交于 2019-12-04 21:06:26
insomniac

To create a banner like that You need to do these things

  1. Create a Shape drawable of an Inverted right angled triangle by tweaking this code
  2. Set the background of the textView to this shape
  3. Change the textView Gravity to right/end align

Here's a simple imageview with the text banner in the corner which I wrote for my own use. The same structure could be applied to any type of view.

public class BannerImageView extends ImageView {

    private Paint mRibbonPaint;
    private Paint mTextPaint;
    private Paint mBoxPaint;
    private float mScale;
    private String mBannerText;

    public BannerImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPainters(attrs);
    }

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

    private void initPainters(AttributeSet attrs) {
        TypedArray attributes = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.BannerImageView, defStyleAttr, 0);
        mBannerText = attributes.getString(R.styleable.BannerImageView_label);
        mBoxPaint = new Paint();
        int white = ContextCompat.getColor(getContext(), R.color.white);
        mBoxPaint.setColor(white);
        mBoxPaint.setAlpha(156);
        mRibbonPaint = new Paint();
        mRibbonPaint.setColor(ContextCompat.getColor(getContext(), R.color.banner_color));
        mRibbonPaint.setAntiAlias(true);
        mRibbonPaint.setStyle(Paint.Style.STROKE);
        mRibbonPaint.setStrokeCap(Paint.Cap.BUTT);
        mScale = getResources().getDisplayMetrics().density;
        mRibbonPaint.setStrokeWidth(dp(16));
        mTextPaint = new Paint();
        mTextPaint.setColor(white);
        mTextPaint.setTextSize( dp(12) );
    }

    /**
     * Converts dp to px
     * @param dp
     * @return
     */
    private float dp(float dp) {
        return dp * mScale + 0.5f;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if ( !TextUtils.isEmpty(mBannerText) ) {
            canvas.drawRect(0, 0, getRight(), getBottom(), mBoxPaint);
            canvas.drawLine(-dp(16), dp(64), dp(64), -dp(16), mRibbonPaint);
            canvas.save();
            canvas.rotate(-45, 0, 0);
            canvas.drawText(mBannerText, -dp(24), dp(38), mTextPaint);
            canvas.restore();
        }
    }

Then declare the extra attributes you want to use in values/attrs.xml

<declare-styleable name="BannerImageView">
    <attr name="label" format="string"/>
</declare-styleable>

Of course you use in your XML layouts as usual but remember the app namespace for the custom attributes

<com.my.package.BannerImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:label="Sold Out" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!