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" />    

   

更多相关文章

  1. android WiFi 开关代码
  2. android View onMeasure
  3. 基于Android的邮件收发
  4. 《Android(安卓)基础(十)》FloatingActionButton
  5. Android(安卓)MenuItem 设置文字颜色-TextColor的设置
  6. Android中播放视频
  7. android通知栏提示
  8. Android(安卓)解压zip文件
  9. android进度条对话框小例子

随机推荐

  1. android 网络请求方式 例子
  2. Android(安卓)代码实现重启
  3. Android一些网站介绍
  4. Android(安卓)Initialization Process
  5. Android(安卓)Timer 更好方法
  6. android使用gridview的书架效果
  7. Android应用程序组件Content Provider在
  8. android腾讯微博吹一吹实现原理
  9. Android(安卓)Manifest之元素中文注释
  10. Android选项卡(TabWidget)应用