概述
最近的项目用到了一个效果,textview需要旋转90度,于是写了一个小控件,以备不时之需。
效果图如下:
===================================================================
其实实现是很简单的,利用了canvas.drawTextOnPath(), 构建好path上的点就可以了。
代码不多就都贴出来了:
package com.ray.verticaltextview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
public class VerticalTextView extends View {
/**
* the text which will be drawn vertical
*/
private String verticalText = "";
private Path vt_TextPath = new Path();
private Paint vt_TextPaint = new Paint();
private static final String TAG = "VerticalTextView";
private int vt_ViewWidth = 0;
private int vt_ViewHeight = 0;
/*
* private final static int GRAVITY_LEFT = 0X00001; private final static int
* GRAVITY_RIGHT = 0X00010; private final static int GRAVITY_TOP = 0X00100;
* private final static int GRAVITY_BOTTOM = 0X01000; private final static
* int GRAVITY_CENTER = 0X10000;
*/
private final static int GRAVITY_LEFT = 0;
private final static int GRAVITY_RIGHT = 1;
private final static int GRAVITY_TOP = 2;
private final static int GRAVITY_BOTTOM = 3;
private final static int GRAVITY_CENTER = 4;
private final static boolean DEBUG = true;
/**
* the default minimum offset in horizontal
*/
private final static int DEFAULT_HORIZONTAL_OFFSET = 20;
/**
* the default minimum offset in vertical
*/
private final static int DEFAULT_VERTICAL_OFFSET = 20;
/**
* the gravity of vertical text
*/
private int vt_ViewGravity = 4;
/**
* write the text from bottom to top, or from top to bottom
*/
private boolean isAsc = true;
/**
* the rectangle of vertical text
*/
private Rect vt_TextRect = new Rect();
public VerticalTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* set the vertical text
*
* @author ray Feb 21, 2012
* @param verticalText
* @param textSize
* if textSize <= 0, the default size will be used
* @param isAsc
* the direction of text is ascend or not
* @param typeface
* use default if typeface is null
*/
public void setVerticalText(String verticalText, float textSize, boolean isAsc, Typeface typeface) {
this.verticalText = verticalText;
if (textSize > 0)
this.vt_TextPaint.setTextSize(textSize);
this.isAsc = isAsc;
if (typeface != null)
vt_TextPaint.setTypeface(typeface);
reLayoutView();
}
/**
* set vertical text color
*
* @author ray Feb 21, 2012
* @param color
*/
public void setTextColor(int color) {
vt_TextPaint.setColor(color);
invalidate();
}
/**
* set vertical text size TODO: the size can't be modified by code, it will
* make the text out of the place
*
* @author ray Feb 21, 2012
* @param textSize
*/
private void setTextSize(float textSize) {
vt_TextPaint.setTextSize(textSize);
reLayoutView();
}
/**
* set the size of view
*
* @author ray Feb 21, 2012
* @param vt_ViewWidth
* @param vt_ViewHeight
*/
public void setVerticalTextViewSize(int vt_ViewWidth, int vt_ViewHeight) {
this.vt_ViewHeight = vt_ViewHeight;
this.vt_ViewWidth = vt_ViewWidth;
}
/**
* reset the layout of vertical view
*
* @author ray Feb 21, 2012
*/
public void reLayoutView() {
vt_TextPaint.getTextBounds(verticalText, 0, verticalText.length(), vt_TextRect);
if (DEBUG) {
Log.d(TAG, "=====================Rect=====================");
Log.d(TAG, "width: " + vt_TextRect.height() + "==" + "height: " + vt_TextRect.width());
Log.d(TAG, "==============================================");
}
int offsetHorizontal = vt_ViewWidth - vt_TextRect.height();
int offsetVertical = vt_ViewHeight - vt_TextRect.width();
offsetHorizontal = Math.max(offsetHorizontal, DEFAULT_HORIZONTAL_OFFSET);
offsetVertical = Math.max(offsetVertical, DEFAULT_VERTICAL_OFFSET);
vt_ViewWidth = offsetHorizontal + vt_TextRect.height();
vt_ViewHeight = offsetVertical + vt_TextRect.width();
LayoutParams params = new LayoutParams(vt_ViewWidth, vt_ViewHeight);
setLayoutParams(params);
int startX = vt_ViewWidth / 2;
if (isAsc) {
vt_TextPath.moveTo(startX, vt_ViewHeight - offsetVertical / 2);
vt_TextPath.cubicTo(startX, vt_ViewHeight - offsetVertical / 2, startX, vt_ViewHeight - offsetVertical / 2, startX, 0);
} else {
vt_TextPath.moveTo(startX, offsetVertical / 2);
vt_TextPath.cubicTo(startX, offsetVertical / 2, startX, offsetVertical / 2, startX, vt_ViewHeight);
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
Log.d(TAG, "VerticalTextView onDraw(), verticalText: " + verticalText);
if (!verticalText.equals("")) {
canvas.drawTextOnPath(verticalText, vt_TextPath, 0, vt_TextRect.height() / 2, vt_TextPaint);
}
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VerticalTextView mVerticalTextView = (VerticalTextView) findViewById(R.id.verticalTv);
mVerticalTextView.setTextColor(Color.RED);
mVerticalTextView.setVerticalText("Hello00000000000000", 30, true, null);
}
===================================================
还有一些功能没有实现,比如对齐方式。而且再次修改字体大小,内容还有点问题。
转载于:https://www.cnblogs.com/changqing/archive/2012/02/22/2362613.html
最后
以上就是激昂衬衫为你收集整理的竖直显示Text的全部内容,希望文章能够帮你解决竖直显示Text所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复