自定义垂直文字view
16lz
2021-12-04
package app.inter.com.heartprogress;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Rect;import android.util.AttributeSet;import android.util.Log;import android.view.View;/** * Created by li.fuzhen on 2017/2/4. */public class VerticalTextView extends View { private Paint mPaint; private String mText; private static final int ORIENTATION_HORIZONTAL = 0; private static final int ORIENTATION_VERTICAL = 1; private int orientation = ORIENTATION_HORIZONTAL; private Rect rect = new Rect(); private int mHeight; private int mCurrentHeight; private String singleText; private int mWithOnce = 1; private int index; public VerticalTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); if (attrs != null) { TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.VerticalTextView); mText = typeArray.getString(R.styleable.VerticalTextView_tv_text); orientation = typeArray.getInt(R.styleable.VerticalTextView_orientation, ORIENTATION_HORIZONTAL); typeArray.recycle(); } mPaint.getTextBounds(mText, 0, mText.length(), rect); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStrokeWidth(1f); mPaint.setStyle(Paint.Style.FILL); mPaint.setColor(Color.BLACK); mPaint.setTextSize(40f); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mHeight = getHeight(); Log.e("zxc", "-----" + rect.width() / mHeight); } @Override protected void onDraw(Canvas canvas) { Log.e("zxc", "2"); if (orientation == ORIENTATION_VERTICAL) { for (int i = 0; i < mText.length(); i++) { singleText = Character.toString(mText.charAt(i)); mCurrentHeight = rect.height() * (index + 1); index++; if (mCurrentHeight >= mHeight) { mWithOnce++; index = 0 + 1; mCurrentHeight = rect.height(); } canvas.drawText(singleText, getWidth() - 80 * mWithOnce, mCurrentHeight, mPaint);//30 是设置的单个文字大小 } } }}
attrs代码如下
name="VerticalTextView"> name="tv_text" format="string" /> name="orientation"> name="vertical" value="1" /> name="horizontal" value="0" />
更多相关文章
- android WiFi 开关代码
- android View onMeasure
- 基于Android的邮件收发
- 《Android(安卓)基础(十)》FloatingActionButton
- Android(安卓)MenuItem 设置文字颜色-TextColor的设置
- Android中播放视频
- android通知栏提示
- Android(安卓)解压zip文件
- android进度条对话框小例子