android竖直显示文字

3 篇文章 0 订阅
2 篇文章 0 订阅

最近的项目用到了一个效果,textview需要旋转90度,于是写了一个小控件,以备不时之需。

效果图如下:

垂直的textview

===================================================================

其实实现是很简单的,利用了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);
}
复制代码

===================================================

还有一些功能没有实现,比如对齐方式。而且再次修改字体大小,内容还有点问题。


原文链接:http://www.cnblogs.com/changqing/archive/2012/02/22/2362613.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值