android 自定义TextView,内容指定位置滚动动画效果
来源:互联网 发布:女生穷游 知乎 编辑:程序博客网 时间:2024/06/05 07:05
public class MyTextView extends TextView {
/*动画开始的位置*/
private int mTextStartIndex;
/*每次动画执行的间隔 事件*/
private int mEverySpaceTime;
private String mTextContent;
private String mSubTextContent;
private boolean mIsStartAnimation;
private ShowTextThread mShowTextThread;
private int mViewVisiblity;
private int mWindowVisiblity;
private volatile boolean mViewVisible;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
setTextStyle(context);
mTextContent = (String)getText();
if (mIsStartAnimation) {
mSubTextContent = mTextContent;
}else{
mSubTextContent = mTextContent.substring(0, mTextStartIndex);
}
}
public boolean ismIsStartAnimation() {
return mIsStartAnimation;
}
public void setmIsStartAnimation(boolean mIsStartAnimation) {
this.mIsStartAnimation = mIsStartAnimation;
}
public int getmTextStartIndex() {
return mTextStartIndex;
}
public void setmTextStartIndex(int mTextStartIndex) {
this.mTextStartIndex = mTextStartIndex;
}
public int getmEverySpaceTime() {
return mEverySpaceTime;
}
public void setmEverySpaceTime(int mEverySpaceTime) {
this.mEverySpaceTime = mEverySpaceTime;
}
private void init(AttributeSet attrs) {
TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.NewTextViewElement);
mEverySpaceTime = t.getInteger(R.styleable.NewTextViewElement_spaceTime, 300);
mTextStartIndex = t.getInteger(R.styleable.NewTextViewElement_animationStartIndex, 1);
mIsStartAnimation = t.getBoolean(R.styleable.NewTextViewElement_isStartAnimation, false);
t.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
setText(mSubTextContent);
super.onDraw(canvas);
}
class ShowTextThread extends Thread {
public void run() {
changeText();
}
private void changeText() {
final int text_length = getText().length();
if (mTextStartIndex > (text_length - 1)) {
return;
}
int currentIndex = mTextStartIndex;
while (true) {
if (!mViewVisible) {
break;
}
mSubTextContent = mTextContent.substring(0, currentIndex);
postInvalidate();
if (currentIndex == text_length) {
currentIndex = mTextStartIndex;
} else {
currentIndex++;
}
try {
sleep(mEverySpaceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* the index will not should at first
*
* @param index
*/
public void setAnimationTextStartIndex(int index) {
mTextStartIndex = index;
}
private void setTextStyle(Context context) {
AssetManager mgr = context.getAssets();
Typeface createFromAsset = Typeface.createFromAsset(mgr, "yf.ttf");
setTypeface(createFromAsset);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
mWindowVisiblity = visibility;
viewVisiblityChanged();
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
mViewVisiblity = visibility;
viewVisiblityChanged();
}
private void viewVisiblityChanged() {
boolean visible = (mViewVisiblity | mWindowVisiblity) == View.VISIBLE;
if (visible == mViewVisible) {
return;
}
mViewVisible = visible;
if (mViewVisible&&mIsStartAnimation) {
// if (mShowTextThread == null) {
// mShowTextThread = new ShowTextThread();
// mShowTextThread.start();
// }
// } else {
// if (mShowTextThread != null) {
// mShowTextThread.interrupt();
// }
new Thread(){
public void run() {
changeText();
}
private void changeText() {
final int text_length = getText().length();
if (mTextStartIndex > (text_length - 1)) {
return;
}
int currentIndex = mTextStartIndex;
while (true) {
if (!mViewVisible) {
break;
}
mSubTextContent = mTextContent.substring(0, currentIndex);
postInvalidate();
if (currentIndex == text_length) {
currentIndex = mTextStartIndex;
} else {
currentIndex++;
}
try {
sleep(mEverySpaceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
/*动画开始的位置*/
private int mTextStartIndex;
/*每次动画执行的间隔 事件*/
private int mEverySpaceTime;
private String mTextContent;
private String mSubTextContent;
private boolean mIsStartAnimation;
private ShowTextThread mShowTextThread;
private int mViewVisiblity;
private int mWindowVisiblity;
private volatile boolean mViewVisible;
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
setTextStyle(context);
mTextContent = (String)getText();
if (mIsStartAnimation) {
mSubTextContent = mTextContent;
}else{
mSubTextContent = mTextContent.substring(0, mTextStartIndex);
}
}
public boolean ismIsStartAnimation() {
return mIsStartAnimation;
}
public void setmIsStartAnimation(boolean mIsStartAnimation) {
this.mIsStartAnimation = mIsStartAnimation;
}
public int getmTextStartIndex() {
return mTextStartIndex;
}
public void setmTextStartIndex(int mTextStartIndex) {
this.mTextStartIndex = mTextStartIndex;
}
public int getmEverySpaceTime() {
return mEverySpaceTime;
}
public void setmEverySpaceTime(int mEverySpaceTime) {
this.mEverySpaceTime = mEverySpaceTime;
}
private void init(AttributeSet attrs) {
TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.NewTextViewElement);
mEverySpaceTime = t.getInteger(R.styleable.NewTextViewElement_spaceTime, 300);
mTextStartIndex = t.getInteger(R.styleable.NewTextViewElement_animationStartIndex, 1);
mIsStartAnimation = t.getBoolean(R.styleable.NewTextViewElement_isStartAnimation, false);
t.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
setText(mSubTextContent);
super.onDraw(canvas);
}
class ShowTextThread extends Thread {
public void run() {
changeText();
}
private void changeText() {
final int text_length = getText().length();
if (mTextStartIndex > (text_length - 1)) {
return;
}
int currentIndex = mTextStartIndex;
while (true) {
if (!mViewVisible) {
break;
}
mSubTextContent = mTextContent.substring(0, currentIndex);
postInvalidate();
if (currentIndex == text_length) {
currentIndex = mTextStartIndex;
} else {
currentIndex++;
}
try {
sleep(mEverySpaceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
/**
* the index will not should at first
*
* @param index
*/
public void setAnimationTextStartIndex(int index) {
mTextStartIndex = index;
}
private void setTextStyle(Context context) {
AssetManager mgr = context.getAssets();
Typeface createFromAsset = Typeface.createFromAsset(mgr, "yf.ttf");
setTypeface(createFromAsset);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
mWindowVisiblity = visibility;
viewVisiblityChanged();
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
mViewVisiblity = visibility;
viewVisiblityChanged();
}
private void viewVisiblityChanged() {
boolean visible = (mViewVisiblity | mWindowVisiblity) == View.VISIBLE;
if (visible == mViewVisible) {
return;
}
mViewVisible = visible;
if (mViewVisible&&mIsStartAnimation) {
// if (mShowTextThread == null) {
// mShowTextThread = new ShowTextThread();
// mShowTextThread.start();
// }
// } else {
// if (mShowTextThread != null) {
// mShowTextThread.interrupt();
// }
new Thread(){
public void run() {
changeText();
}
private void changeText() {
final int text_length = getText().length();
if (mTextStartIndex > (text_length - 1)) {
return;
}
int currentIndex = mTextStartIndex;
while (true) {
if (!mViewVisible) {
break;
}
mSubTextContent = mTextContent.substring(0, currentIndex);
postInvalidate();
if (currentIndex == text_length) {
currentIndex = mTextStartIndex;
} else {
currentIndex++;
}
try {
sleep(mEverySpaceTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
}
xmlns:xsetting="http://schemas.android.com/apk/res/com.itshow"
//xmlns:xsetting="http://schemas.android.com/apk/res/包名"
<com.itshow.view.MyTextView
android:layout_width="170px"
android:layout_height="wrap_content"
android:text="@string/xxx" 如:123456789
xsetting:animationStartIndex="3"
xsetting:isStartAnimation="true"
xsetting:spaceTime="500" />
0 0
- android 自定义TextView,内容指定位置滚动动画效果
- Android 自定义Dialog并指定其显示位置和动画效果
- Android TextView滚动效果
- TextView 滚动效果 android
- Android TextView滚动效果
- 让滚动条滚动到指定位置,带动画效果 jQuery
- Android:TextView的垂直滚动效果和上下滚动效果,原生动画实现
- Android TextView实现滚动效果
- jquery 滚动到指定位置触发动画
- jQuery 滚动到指定位置触发动画
- jquery 滚动到指定位置触发动画
- Android金钱滚动效果-滚动的textView
- 自定义数字滚动动画的TextView
- textview 滚动效果(自定义高度)
- 自定义textview控件实现文字滚动效果
- Android自定义控件之——自定义内容可滚动的TextView
- android自定义TextView点击效果
- Android:TextView的垂直滚动效果,和上下滚动效果
- 排列组合_排列_程序实现
- ext4日志系统分析(一)
- NLP和应用
- Maven构建Spark Framework Java项目--Linux命令行
- matlab形态学处理命令
- android 自定义TextView,内容指定位置滚动动画效果
- c 陷阱与缺陷读书笔记
- 1053. 住房空置率 (20)
- 括号匹配及其变形
- mongodb3.0+导出库导入库
- NOIP 2004 提高组 复赛 chorus 合唱队形
- ReactNative系列之十三WebStorm的JSX语法配置及自动补全设置
- 330. Patching Array(Hard)
- 51Nod 1005 大数加法