android豎直顯示文字

最近的項目用到了一個效果,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

發佈了26 篇原創文章 · 獲贊 0 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章