Get resource ID by resource name
int resId = myContext.getResources().getIdentifier("resouces_name",
"drawable",
myContext.getPackageName());
myVoice.setImageResource(resId);
API:
getIdentifier()
Use of JumpingBeans
implementation 'net.frakbot:jumpingbeans:1.3.0'
TextView textView=findViewById(R.id.textViewer);
jumpingBeans= JumpingBeans.
with(textView).
//设置全部字数跳动
makeTextJump(0,textView.getText().length()).
//设置第一至第五个字数跳动
//makeTextJump(0,5).
setIsWave(true).
//毫秒为单位
setLoopDuration(800).
build();
Part of the source code is:
public static Builder with(@NonNull TextView textView) {
return new Builder(textView);
}
public Builder makeTextJump(int startPos, int endPos) {
CharSequence text = textView.getText();
ensureTextCanJump(startPos, endPos, text);
this.text = text;
this.wave = true;
this.startPos = startPos;
this.endPos = endPos;
return this;
}
Judge the validity of startPos, endPos and text
private static CharSequence ensureTextCanJump(int startPos, int endPos, CharSequence text) {
if (text == null) {
throw new NullPointerException("The textView text must not be null");
}
if (endPos < startPos) {
throw new IllegalArgumentException("The start position must be smaller than the end position");
}
if (startPos < 0) {
throw new IndexOutOfBoundsException("The start position must be non-negative");
}
if (endPos > text.length()) {
throw new IndexOutOfBoundsException("The end position must be smaller than the text length");
}
return text;
}
public Builder setIsWave(boolean wave) {
this.wave = wave;
return this;
}
public JumpingBeans build() {
SpannableStringBuilder sbb = new SpannableStringBuilder(text);
JumpingBeansSpan[] spans;
if (wave) {
spans = getJumpingBeansSpans(sbb);
} else {
spans = buildSingleSpan(sbb);
}
textView.setText(sbb);
return new JumpingBeans(spans, textView);
}
This method should be called at the end, otherwise it is easy to leak memory
public void stopJumping() {
for (JumpingBeansSpan bean : jumpingBeans) {
if (bean != null) {
bean.teardown();
}
}
cleanupSpansFrom(textView.get());
}
Github:
JumpingBeans