N
N
Nikita2018-05-21 23:22:41
Java
Nikita, 2018-05-21 23:22:41

How to make a custom textview?

This is my view

<com.example.blabla.HateTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/hate"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="Text" />

Further code:
public class HateTextView extends android.support.v7.widget.AppCompatTextView {
    
        Paint mTxtPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private String str = " ";
    
        HateTextView(Context context, AttributeSet attrs){
            super(context, attrs);
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.HateTextView);
            a.recycle();
        }
    
    
        @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint.FontMetrics fm = new Paint.FontMetrics();
            mTxtPaint.setColor(Color.parseColor("#d00000"));
            mTxtPaint.setTextSize(20 * getResources().getDisplayMetrics().density);
            mTxtPaint.getFontMetrics(fm);
            mTxtPaint.isAntiAlias();
    
            int margin = 5;
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                canvas.drawRoundRect(90 - margin, 100 + fm.top - margin,
                        115 + mTxtPaint.measureText(str) + margin, 100 + fm.bottom + margin,35,35, mTxtPaint);
            }
    
            mTxtPaint.setColor(Color.WHITE);
            canvas.drawText(str, 100, 100, mTxtPaint);
        }
    
        public void setTextHate(String hate) {
            str = hate;
        }
    }

I create something like this:
HateTextView h = itemView.findViewById(R.id.hate);
 h.setTextHate("какой-то текст..");
h.setRotation(номер поворота); только из-за поворота столько проблем. А вернее из-за antialias. Просто из xml getPaint.AntiAlia.. не работает.

here I set match_parent, and if the content is the text that I draw is not visible at all. How can I insert my text that I draw instead of this text.
5b0329bbb28cd223512000.jpeg
It is advisable to write those who can help, and not smart people who write God, you are a lazy ass. I write in desperation

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question