Advertisement
Guest User

Untitled

a guest
Apr 9th, 2013
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package com.gorgo.pirates.view;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Canvas;
  5. import android.graphics.Color;
  6. import android.graphics.Paint;
  7. import android.graphics.Typeface;
  8. import android.view.View;
  9. import android.widget.TextView;
  10.  
  11. public class Text extends TextView {
  12.     private Paint paint;
  13.     private Context context;
  14.     private boolean finished, initTime = true;
  15.     private long beginTime;
  16.  
  17.  
  18.     public Text(Context context) {
  19.         super(context);
  20.         this.context = context;
  21.         paint = new Paint();
  22.     }
  23.  
  24.     public void draw(Canvas canvas, String string, long timeShow, int color) {
  25.         canvas.save();
  26.         if (initTime == true){ // Prima chiamata draw(), salvo beginTime
  27.             System.out.println("Salvo tempo " + timeShow);
  28.             finished = false;
  29.             saveTime();
  30.         }
  31.         //System.out.println(System.currentTimeMillis()-beginTime + " < " + timeShow);
  32.        
  33.         if (System.currentTimeMillis() - beginTime < timeShow) {
  34.            
  35.             Typeface chops = Typeface.createFromAsset(this.context.getAssets(),
  36.                     "Commodore.ttf");
  37.            
  38.             // Setup a textview like you normally would with your activity context
  39.             TextView tv = new TextView(context);
  40.             tv.setText(string);
  41.             tv.setTextColor(color);
  42.             tv.setTypeface(chops);
  43.             tv.setShadowLayer(10, 5, 5, Color.BLACK);
  44.            
  45.             tv.setGravity(FOCUS_LEFT | FOCUS_UP);
  46.             // you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null
  47.             tv.setDrawingCacheEnabled(true);
  48.             // we need to setup how big the view should be..which is exactly as big as the canvas
  49.             tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth()-5, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
  50.             // assign the layout values to the textview
  51.             tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
  52.             // draw the bitmap from the drawingcache to the canvas
  53.             if(string != null)
  54.                 canvas.drawBitmap(tv.getDrawingCache(), (canvas.getWidth() / 80), (canvas.getWidth() / 5), paint);
  55.             // disable drawing cache
  56.             tv.setDrawingCacheEnabled(false);
  57.         } else {
  58.             this.setVisibility(View.GONE);
  59.             initTime = true;
  60.             finished = true;
  61.         }
  62.         canvas.restore();
  63.     }
  64.  
  65.     private void saveTime() {
  66.         beginTime = System.currentTimeMillis();
  67.         initTime = false;
  68.     }
  69.    
  70.     public boolean getFinished(){
  71.         return finished;
  72.     }
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement