我自定义了一个textview显示歌词里面插入了一个动画,当换歌词时直接向上滚动一段距离但是每次都会反弹回来,求各位帮忙看下问题在哪里,该怎么解决,我也知道translate动画不改变属性,用网上看到的layout提前位移了一下也不行。
这个代码是重画歌词的
public void handleMessage(Message msg) {
switch (msg.what) {
case REFRESH_POSITION:
long position = mStreamPlayer.position();
mLyricView.updatePosition(position);
Message message = mHandler.obtainMessage(REFRESH_POSITION);
mHandler.sendMessageDelayed(message, LyricView.UPDATE_FREQUENCY);
break;
default:
break;
}
}
这个是textview重画代码
protected void onDraw(Canvas canvas) {
super.onDraw(canvas)
if (mLyric == null) {
return;
}
ArrayList<LyricSentence> lyricList = mLyric.getTotalSentences();
if (lyricList.isEmpty()) {
return;
}
int curLyricIndex = 0;
for (LyricSentence lyricSentence : lyricList) {
if (mCurTime >= lyricSentence.getFromTime()
&& mCurTime < lyricSentence.getToTime()) {
break;
}
curLyricIndex++;
}
if (curLyricIndex >= lyricList.size()) {
return;
}
if(mLastIndex !=curLyricIndex )
{
// this.setVisibility(GONE);
mLastIndex= curLyricIndex;
// this.layout(getLeft(), getTop()-34, getRight(), getBottom()-34);
Animation anim = null;
anim = new TranslateAnimation(0, 0, 0, -34);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.setDuration(1000);
anim.setFillEnabled(true);
anim.setFillAfter(true);
startAnimation(anim);
invalidate();
// this.setVisibility(GONE);
return;
}
LyricSentence lyricSentence = lyricList.get(curLyricIndex);
canvas.drawText(lyricSentence.getContent(), mLeft, mMiddleY, mOnFocusePaint);
float tempY = mMiddleY;
for (int i = curLyricIndex - 1; i >= 0; i--) {
if (i == curLyricIndex - 1) {
tempY -= (mFocusedTextSpace + mFocusedTextSize);
} else {
tempY -= mUnFocusedTextSpace;
}
if (tempY < 0) {
break;
}
canvas.drawText(lyricList.get(i).getContent(), mLeft, tempY, mLoseFocusPaint);
}
tempY = mMiddleY;
for (int i = curLyricIndex + 1; i < lyricList.size(); i++) {
if (i == curLyricIndex + 1) {
tempY += (mFocusedTextSpace + mUnFocusedTextSize);
} else {
tempY += mUnFocusedTextSpace;
}
if (tempY >= mHeight) {
break;
}
canvas.drawText(lyricList.get(i).getContent(), mLeft, tempY, mLoseFocusPaint);
}
}
------解决方案--------------------
不要用 动画了把 把 TextView 固定在一个地方的话, 平移动画不太好用;
通过设置 TextView 中的内容, 行数不变, 通过逐行 修改里面的内容, 播放完 就删除一行, 在末尾 加上 另一行歌词, 做成滚动的假象, 可以按照你上面的那种滚动方式, 滚动完一行, 弹回, 然后你再设置歌词内容