tac*_*lux 16
制作自己的动画.
动画/ marquee.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="100%"
android:toXDelta="-100%"
android:duration="10000"
android:repeatCount="infinite"
android:repeatMode="restart"
android:interpolator="@android:anim/linear_interpolator"/>
</set>
Run Code Online (Sandbox Code Playgroud)
在你的活动中,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
TextView myTextView = (TextView) findViewById(R.id.myTextView);
Animation marquee = AnimationUtils.loadAnimation(this, R.anim.marquee);
myTextView.startAnimation(marquee);
}
Run Code Online (Sandbox Code Playgroud)
采纳 @JodiMiddleton 关于填充文本的建议,我构建了一些辅助方法,以基于 TextPaint 对象将文本填充到目标宽度(确保测量时字体等的大小正确):
/**
* Pad a target string of text with spaces on the right to fill a target
* width
*
* @param text The target text
* @param paint The TextPaint used to measure the target text and
* whitespaces
* @param width The target width to fill
* @return the original text with extra padding to fill the width
*/
public static CharSequence padText(CharSequence text, TextPaint paint, int width) {
// First measure the width of the text itself
Rect textbounds = new Rect();
paint.getTextBounds(text.toString(), 0, text.length(), textbounds);
/**
* check to see if it does indeed need padding to reach the target width
*/
if (textbounds.width() > width) {
return text;
}
/*
* Measure the text of the space character (there's a bug with the
* 'getTextBounds() method of Paint that trims the white space, thus
* making it impossible to measure the width of a space without
* surrounding it in arbitrary characters)
*/
String workaroundString = "a a";
Rect spacebounds = new Rect();
paint.getTextBounds(workaroundString, 0, workaroundString.length(), spacebounds);
Rect abounds = new Rect();
paint.getTextBounds(new char[] {
'a'
}, 0, 1, abounds);
float spaceWidth = spacebounds.width() - (abounds.width() * 2);
/*
* measure the amount of spaces needed based on the target width to fill
* (using Math.ceil to ensure the maximum whole number of spaces)
*/
int amountOfSpacesNeeded = (int)Math.ceil((width - textbounds.width()) / spaceWidth);
// pad with spaces til the width is less than the text width
return amountOfSpacesNeeded > 0 ? padRight(text.toString(), text.toString().length()
+ amountOfSpacesNeeded) : text;
}
/**
* Pads a string with white space on the right of the original string
*
* @param s The target string
* @param n The new target length of the string
* @return The target string padded with whitespace on the right to its new
* length
*/
public static String padRight(String s, int n) {
return String.format("%1$-" + n + "s", s);
}
Run Code Online (Sandbox Code Playgroud)
因此,当您使用基于 TextView 的方法时,您将调用:
textView.setText(padText(myTargetString, textView.getPaint(), textView.getWidth()));
Run Code Online (Sandbox Code Playgroud)
它并不优雅,而且我几乎可以肯定可以进行改进(更不用说更好的方法了),但尽管如此,我在代码中使用了它,而且它似乎起到了作用:)
更新的答案
measureText允许更准确的测量(使用上面的内容我发现空格字符比测量的稍短,足以不选框)。
调整上面的答案将简化为:
public static CharSequence padText(CharSequence text, TextPaint paint, int width) {
float widthOfText = paint.measureText(text.toString());
if (widthOfText > width) {
return text;
}
// Calculate how many spaces are required
float widthOfSpace = paint.measureText(" ");
int amountOfSpacesNeeded = (int) Math.ceil((width - widthOfText) / widthOfSpace);
return amountOfSpacesNeeded > 0 ? padRight(text.toString(), text.toString().length()
+ amountOfSpacesNeeded) : text;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4875 次 |
最近记录: |