Android Open Source - HexNanoController_Android Text






From Project

Back to project page HexNanoController_Android.

License

The source code is released under:

Code license GNU GPL v2 http://www.gnu.org/licenses/gpl.html Content license CC BY-NC-SA 4.0 http://creativecommons.org/licenses/by-nc-sa/4.0/

If you think the Android project HexNanoController_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.hexairbot.hexmini.ui;
/* w ww  . ja  va2 s.com*/
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.MotionEvent;
import android.view.View;

import com.hexairbot.hexmini.ui.gl.GLSprite;

public class Text extends Sprite{

  public static final int VISIBLE = 1;
  public static final int INVISIBLE = 0;
  
  private GLSprite sprite;
  private String text;
  private Paint paint;
  private int visibility;
  
  private boolean updateTexture;
  private boolean blink;
  private long prevNano;
  
  private Resources res;
  
  public Text(Context context, String text, Align alignment) {
    super(alignment);  
    res = context.getResources();
    this.paint = new Paint();

    Resources res = context.getResources();
    paint.setColor(res.getColor(android.R.color.primary_text_dark));
    paint.setTextSize(24);
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    
    
    this.text = text;
    
    initSprite();
    
    updateTexture = false;
    visibility = VISIBLE;
  }
  
  
  private void initSprite()
  {
    Bitmap bitmap = createBitmapToRender();
    sprite = new GLSprite(res, bitmap);
    bitmap.recycle();
  }


  private Bitmap createBitmapToRender() {
    float width = 1;
    float height = 1;
    
    if (text.length() > 0) {  
      width = paint.measureText(text);
      height = paint.getTextSize() + 3; //??????????????bug
    }
    
    Bitmap bitmap = Bitmap.createBitmap(Math.round(width), Math.round(height), Bitmap.Config.ARGB_4444);
    bitmap.eraseColor(0x00000000);
    bitmap.setDensity(res.getDisplayMetrics().densityDpi);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, 0, height - paint.getFontMetrics().bottom, paint);
    return bitmap;
  }

  @Override
  public void init(GL10 gl, int program) {
    sprite.init(gl, program);
  }

  @Override
  public void draw(GL10 gl) 
  {  
    if (updateTexture) {
      Bitmap bitmap = createBitmapToRender();
      sprite.updateTexture(res, bitmap);
      layout(surfaceWidth, surfaceHeight);
      
      updateTexture = false;
    }
    
    if (blink) {
      long nano = System.currentTimeMillis();
      if (nano - prevNano > 500) {
        prevNano = nano;
        
        if (sprite.alpha >= 1.0f) {
          sprite.setAlpha(0.0f);
        } else {
          sprite.setAlpha(1.0f);
        }
      }
    }
    
    if (visibility == VISIBLE) {
      sprite.onDraw(gl, bounds.left, surfaceHeight - bounds.top - sprite.height);
    }
  }

  @Override
  public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    
  }

  @Override
  public boolean onTouchEvent(View v, MotionEvent event) 
  {
    return false;
  }

  
  public void onAlphaChanged(float newAlpha)
  {
      sprite.setAlpha(newAlpha);
  }
  
  
  @Override
  public boolean isInitialized() 
  {
    return sprite.isReadyToDraw();
  }

  
  @Override
  public void setViewAndProjectionMatrices(float[] vMatrix, float[] projMatrix) 
  {
    sprite.setViewAndProjectionMatrices(vMatrix, projMatrix);  
  }

  
  @Override
  public int getWidth() 
  {
    return sprite.width;
  }

  
  @Override
  public int getHeight() 
  {
    return sprite.height;
  }
  
  
  public void setTextSize(int size)
  {
    if (paint.getTextSize() != size) {
      paint.setTextSize(size);
      invalidate();
    }
  }
  
  
  public void setTextColor(int color)
  {
    if (color != paint.getColor()) {
      paint.setColor(color);
      invalidate();
    }
  }
  

  public void setText(String string) 
  {
    if (!this.text.equals(string)) {
      this.text = string;
      invalidate();
    }
  }
  
  
  public void setBold(boolean bold)
  {
    if (bold) {
      paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
    } else {
      paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.NORMAL));
    }
  }


  public void setVisibility(int visibility) {
    this.visibility = visibility;  
  }
  
  
  private void invalidate()
  {
    updateTexture = true;
  }


  public void blink(boolean b) 
  {  
    this.blink = b;
  }


  public void setTypeface(Typeface tf) 
  {
    paint.setTypeface(tf);
    invalidate();
  }

  
    @Override
    public void freeResources()
    {
        sprite.freeResources();
    }
    
    @Override
    public void setNeedsUpdate() {

    }
}




Java Source Code List

.FileHelper.java
.Input.java
.Output.java
.Serializable.java
com.hexairbot.hexmini.HelpActivity.java
com.hexairbot.hexmini.HexMiniApplication.java
com.hexairbot.hexmini.HudActivity.java
com.hexairbot.hexmini.HudViewControllerDelegate.java
com.hexairbot.hexmini.HudViewController.java
com.hexairbot.hexmini.SettingsDialogDelegate.java
com.hexairbot.hexmini.SettingsDialog.java
com.hexairbot.hexmini.SettingsViewControllerDelegate.java
com.hexairbot.hexmini.SettingsViewController.java
com.hexairbot.hexmini.ViewController.java
com.hexairbot.hexmini.adapter.SettingsViewAdapter.java
com.hexairbot.hexmini.ble.BleConnectinManagerDelegate.java
com.hexairbot.hexmini.ble.BleConnectinManager.java
com.hexairbot.hexmini.ble.BluetoothLeService.java
com.hexairbot.hexmini.gestures.EnhancedGestureDetector.java
com.hexairbot.hexmini.modal.ApplicationSettings.java
com.hexairbot.hexmini.modal.Channel.java
com.hexairbot.hexmini.modal.OSDCommon.java
com.hexairbot.hexmini.modal.Transmitter.java
com.hexairbot.hexmini.sensors.DeviceOrientationChangeDelegate.java
com.hexairbot.hexmini.sensors.DeviceOrientationManager.java
com.hexairbot.hexmini.sensors.DeviceSensorManagerWrapper.java
com.hexairbot.hexmini.sensors.SensorManagerWrapper.java
com.hexairbot.hexmini.services.ConnectStateManager.java
com.hexairbot.hexmini.services.IpcControlService.java
com.hexairbot.hexmini.services.IpcProxy.java
com.hexairbot.hexmini.services.NavData.java
com.hexairbot.hexmini.services.OnIpcConnectChangedListener.java
com.hexairbot.hexmini.services.VIConfig.java
com.hexairbot.hexmini.ui.Button.java
com.hexairbot.hexmini.ui.Image.java
com.hexairbot.hexmini.ui.Indicator.java
com.hexairbot.hexmini.ui.Sprite.java
com.hexairbot.hexmini.ui.Text.java
com.hexairbot.hexmini.ui.ToggleButton.java
com.hexairbot.hexmini.ui.UIRenderer.java
com.hexairbot.hexmini.ui.control.CustomSeekBar.java
com.hexairbot.hexmini.ui.control.ViewPagerIndicator.java
com.hexairbot.hexmini.ui.gl.GLSprite.java
com.hexairbot.hexmini.ui.joystick.AcceleratorJoystick.java
com.hexairbot.hexmini.ui.joystick.AnalogueJoystick.java
com.hexairbot.hexmini.ui.joystick.JoystickBase.java
com.hexairbot.hexmini.ui.joystick.JoystickFactory.java
com.hexairbot.hexmini.ui.joystick.JoystickListener.java
com.hexairbot.hexmini.util.DebugHandler.java
com.hexairbot.hexmini.util.FontUtils.java
com.hexairbot.hexmini.util.SystemUiHiderBase.java
com.hexairbot.hexmini.util.SystemUiHiderHoneycomb.java
com.hexairbot.hexmini.util.SystemUiHider.java
com.hexairbot.hexmini.util.SystemUtil.java
com.hexairbot.hexmini.util.TextureUtils.java
fix.android.opengl.GLES20.java