3
嗨,我需要爲textview應用顏色過渡,然後移動到下一個屏幕。我試過的是我嘗試使用靜態顏色textview使用spannable字符串,但我無法應用在textview上移動的動態顏色過渡。這是我嘗試過的。如何從左到右應用textview的顏色過渡android?
public class AppActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.text);
Spannable text = new SpannableStringBuilder("Loading..");
setSpan(tv, text);
tv.setText(text);
}
private void setSpan(TextView tv, Spannable text) {
int blueColor = 0xff0000ff;
int redColor = 0xffff0000;
ForegroundColorSpan blue = new ForegroundColorSpan(blueColor);
ForegroundColorSpan red = new ForegroundColorSpan(redColor);
HalfColorApply o = new HalfColorApply("o", tv, blueColor, redColor);
text.setSpan(blue, 0, 1, 0);
text.setSpan(o, 1, 2, 0);
text.setSpan(red, 2, text.length(), 0);
}
class HalfColorApply extends DynamicDrawableSpan {
private final static String TAG = "DrawableSpanTest";
Picture mPicture;
public HalfColorApply(String text, TextView tv, int c0, int c1) {
super(ALIGN_BASELINE);
mPicture = new Picture();
TextPaint p = tv.getPaint();
Rect bounds = new Rect();
p.getTextBounds(text, 0, text.length(), bounds);
float width = p.measureText(text);
float height = bounds.height();
float y = height;
Canvas c = mPicture.beginRecording((int) width, (int) height);
c.save(Canvas.CLIP_SAVE_FLAG);
// c.drawColor(0x[masked]);
p = new TextPaint(p);
p.setColor(c0);
c.clipRect(0, 0, width/2, height, Region.Op.REPLACE);
c.drawText(text, 0, y, p);
p.setColor(c1);
c.clipRect(width/2, 0, width, height, Region.Op.REPLACE);
c.drawText(text, 0, y, p);
c.restore();
mPicture.endRecording();
}
@Override
public Drawable getDrawable() {
PictureDrawable d = new PictureDrawable(mPicture);
d.setBounds(0, 0, mPicture.getWidth(), mPicture.getHeight());
return d;
}
}
}
這裏是截圖,我在一個固定的特定文本應用。我需要使顏色過渡在textview上移動。
在此先感謝。
使用的LinearGradient和動畫使用ValueAnimator/ObjectAnimator時,由左到右 – pskink 2015-04-03 08:32:51
移動,以動畫播放它取決於你使用哪一個 – pskink 2015-04-03 09:35:22