22
33import android .content .Context ;
44import android .content .res .TypedArray ;
5+ import android .graphics .Bitmap ;
56import android .graphics .Canvas ;
67import android .graphics .Color ;
78import android .graphics .Paint ;
@@ -43,6 +44,9 @@ public class LoadToastView extends View {
4344 private int IMAGE_WIDTH = 40 ;
4445 private int TOAST_HEIGHT = 48 ;
4546 private float WIDTH_SCALE = 0f ;
47+ private int MARQUE_STEP = 1 ;
48+
49+ private long prevUpdate = 0 ;
4650
4751 private Drawable completeicon ;
4852 private Drawable failedicon ;
@@ -51,6 +55,7 @@ public class LoadToastView extends View {
5155 private ValueAnimator cmp ;
5256
5357 private boolean success = true ;
58+ private boolean outOfBounds = false ;
5459
5560 private Path toastPath = new Path ();
5661 private AccelerateDecelerateInterpolator easeinterpol = new AccelerateDecelerateInterpolator ();
@@ -81,6 +86,7 @@ public LoadToastView(Context context) {
8186 BASE_TEXT_SIZE = dpToPx (BASE_TEXT_SIZE );
8287 IMAGE_WIDTH = dpToPx (IMAGE_WIDTH );
8388 TOAST_HEIGHT = dpToPx (TOAST_HEIGHT );
89+ MARQUE_STEP = dpToPx (MARQUE_STEP );
8490
8591 int padding = (TOAST_HEIGHT - IMAGE_WIDTH )/2 ;
8692 iconBounds = new Rect (TOAST_HEIGHT + MAX_TEXT_WIDTH - padding , padding , TOAST_HEIGHT + MAX_TEXT_WIDTH - padding + IMAGE_WIDTH , IMAGE_WIDTH + padding );
@@ -175,6 +181,9 @@ public void setText(String text) {
175181 }
176182
177183 private void calculateBounds () {
184+ outOfBounds = false ;
185+ prevUpdate = 0 ;
186+
178187 textPaint .setTextSize (BASE_TEXT_SIZE );
179188 textPaint .getTextBounds (mText , 0 , mText .length (), mTextBounds );
180189 if (mTextBounds .width () > MAX_TEXT_WIDTH ){
@@ -186,11 +195,14 @@ private void calculateBounds() {
186195 textPaint .getTextBounds (mText , 0 , mText .length (), mTextBounds );
187196 }
188197 if (mTextBounds .width () > MAX_TEXT_WIDTH ){
198+ outOfBounds = true ;
199+ /**
189200 float keep = (float)MAX_TEXT_WIDTH / (float)mTextBounds.width();
190201 int charcount = (int)(mText.length() * keep);
191202 //Log.d("calc", "keep " + charcount + " per " + keep + " len " + mText.length());
192203 mText = mText.substring(0, charcount);
193204 textPaint.getTextBounds(mText, 0, mText.length(), mTextBounds);
205+ **/
194206 }
195207 }
196208 }
@@ -232,8 +244,6 @@ protected void onDraw(Canvas c){
232244 c .drawCircle (spinnerRect .centerX (), spinnerRect .centerY (), iconBounds .height () / 1.9f , backPaint );
233245 //loadicon.draw(c);
234246 c .drawPath (toastPath , backPaint );
235- int yPos = (int ) ((th / 2 ) - ((textPaint .descent () + textPaint .ascent ()) / 2 )) ;
236- c .drawText (mText , 0 , mText .length (), th / 2 + (MAX_TEXT_WIDTH - mTextBounds .width ()) / 2 , yPos , textPaint );
237247
238248 float prog = va .getAnimatedFraction () * 6.0f ;
239249 float progrot = prog % 2.0f ;
@@ -262,6 +272,28 @@ protected void onDraw(Canvas c){
262272 c .rotate (90 *(1f -circleProg ), leftMargin + TOAST_HEIGHT /2 , TOAST_HEIGHT /2 );
263273 icon .draw (c );
264274 c .restore ();
275+
276+ prevUpdate = 0 ;
277+ return ;
278+ }
279+
280+ int yPos = (int ) ((th / 2 ) - ((textPaint .descent () + textPaint .ascent ()) / 2 )) ;
281+
282+ if (outOfBounds ){
283+ float shift = 0 ;
284+ if (prevUpdate == 0 ){
285+ prevUpdate = System .currentTimeMillis ();
286+ }else {
287+ shift = ((float )(System .currentTimeMillis () - prevUpdate ) / 16f ) * MARQUE_STEP ;
288+
289+ if (shift - MAX_TEXT_WIDTH > mTextBounds .width ()){
290+ prevUpdate = 0 ;
291+ }
292+ }
293+ c .clipRect (th / 2 , 0 , th /2 + MAX_TEXT_WIDTH , TOAST_HEIGHT );
294+ c .drawText (mText , th / 2 - shift + MAX_TEXT_WIDTH , yPos , textPaint );
295+ }else {
296+ c .drawText (mText , 0 , mText .length (), th / 2 + (MAX_TEXT_WIDTH - mTextBounds .width ()) / 2 , yPos , textPaint );
265297 }
266298 //c.drawArc(spinnerRect, 360 * progrot, 200 * proglength + 1, true, loaderPaint);
267299 }
0 commit comments