Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 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.     private float x,y;
  17.  
  18.  
  19.     public Text(Context context) {
  20.         super(context);
  21.         this.context = context;
  22.         paint = new Paint();
  23.     }
  24.  
  25.     public void draw(Canvas canvas, String string, long timeShow, int color) {
  26.         canvas.save();
  27.         if (initTime == true){ // Prima chiamata draw(), salvo beginTime
  28.             System.out.println("Salvo tempo " + timeShow);
  29.             finished = false;
  30.             saveTime();
  31.         }
  32.         //System.out.println(System.currentTimeMillis()-beginTime + " < " + timeShow);
  33.        
  34.         if (System.currentTimeMillis() - beginTime < timeShow) {
  35.            
  36.             Typeface chops = Typeface.createFromAsset(this.context.getAssets(),
  37.                     "Commodore.ttf");      
  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.             tv.setGravity(FOCUS_LEFT | FOCUS_UP);
  45.             // you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null
  46.             tv.setDrawingCacheEnabled(true);
  47.             // we need to setup how big the view should be..which is exactly as big as the canvas
  48.             tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth()-5, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
  49.             // assign the layout values to the textview
  50.             tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
  51.             // draw the bitmap from the drawingcache to the canvas
  52.             if(string != null)
  53.                 canvas.drawBitmap(tv.getDrawingCache(), x, y, paint);
  54.             // disable drawing cache
  55.             tv.setDrawingCacheEnabled(false);
  56.         } else {
  57.             this.setVisibility(View.GONE);
  58.             initTime = true;
  59.             finished = true;
  60.         }
  61.         canvas.restore();
  62.     }
  63.  
  64.     private void saveTime() {
  65.         beginTime = System.currentTimeMillis();
  66.         initTime = false;
  67.     }
  68.    
  69.     public boolean getFinished(){
  70.         return finished;
  71.     }
  72.    
  73.     public void setXY(float x, float y){
  74.         this.x=x;
  75.         this.y=y;
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement