Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gorgo.pirates.view;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Typeface;
- import android.view.View;
- import android.widget.TextView;
- public class Text extends TextView {
- private Paint paint;
- private Context context;
- private boolean finished, initTime = true;
- private long beginTime;
- private float x,y;
- public Text(Context context) {
- super(context);
- this.context = context;
- paint = new Paint();
- }
- public void draw(Canvas canvas, String string, long timeShow, int color) {
- canvas.save();
- if (initTime == true){ // Prima chiamata draw(), salvo beginTime
- System.out.println("Salvo tempo " + timeShow);
- finished = false;
- saveTime();
- }
- //System.out.println(System.currentTimeMillis()-beginTime + " < " + timeShow);
- if (System.currentTimeMillis() - beginTime < timeShow) {
- Typeface chops = Typeface.createFromAsset(this.context.getAssets(),
- "Commodore.ttf");
- // Setup a textview like you normally would with your activity context
- TextView tv = new TextView(context);
- tv.setText(string);
- tv.setTextColor(color);
- tv.setTypeface(chops);
- tv.setShadowLayer(10, 5, 5, Color.BLACK);
- tv.setGravity(FOCUS_LEFT | FOCUS_UP);
- // you have to enable setDrawingCacheEnabled, or the getDrawingCache will return null
- tv.setDrawingCacheEnabled(true);
- // we need to setup how big the view should be..which is exactly as big as the canvas
- tv.measure(MeasureSpec.makeMeasureSpec(canvas.getWidth()-5, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(canvas.getHeight(), MeasureSpec.EXACTLY));
- // assign the layout values to the textview
- tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
- // draw the bitmap from the drawingcache to the canvas
- if(string != null)
- canvas.drawBitmap(tv.getDrawingCache(), x, y, paint);
- // disable drawing cache
- tv.setDrawingCacheEnabled(false);
- } else {
- this.setVisibility(View.GONE);
- initTime = true;
- finished = true;
- }
- canvas.restore();
- }
- private void saveTime() {
- beginTime = System.currentTimeMillis();
- initTime = false;
- }
- public boolean getFinished(){
- return finished;
- }
- public void setXY(float x, float y){
- this.x=x;
- this.y=y;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement