Perf improvement (small)

This commit is contained in:
jaguillo 2015-10-03 00:19:17 +02:00
parent 700adbded6
commit 549c753450
3 changed files with 27 additions and 15 deletions

View File

@ -31,6 +31,9 @@ class KeyValue
private int _eventCode; private int _eventCode;
private int _flags; private int _flags;
private int _cacheFlags;
private String _cacheSymbol;
public String getName() public String getName()
{ {
return (_name); return (_name);
@ -39,7 +42,14 @@ class KeyValue
public String getSymbol(int flags) public String getSymbol(int flags)
{ {
if (_symbol == null) if (_symbol == null)
return (String.valueOf(getChar(flags))); {
if (flags != _cacheFlags)
{
_cacheSymbol = String.valueOf(getChar(flags));
_cacheFlags = flags;
}
return (_cacheSymbol);
}
return (_symbol); return (_symbol);
} }
@ -87,6 +97,7 @@ class KeyValue
_char = c; _char = c;
_eventCode = eventCode; _eventCode = eventCode;
_flags = flags; _flags = flags;
_cacheFlags = -1;
KeyValue.keys.put(name, this); KeyValue.keys.put(name, this);
} }

View File

@ -30,7 +30,6 @@ public class Keyboard2 extends InputMethodService
{ {
ViewGroup parent = (ViewGroup)_inputView.getParent(); ViewGroup parent = (ViewGroup)_inputView.getParent();
System.out.println("create input view");
if (parent != null) if (parent != null)
parent.removeView(_inputView); parent.removeView(_inputView);
_inputView.reset(); _inputView.reset();
@ -46,21 +45,18 @@ public class Keyboard2 extends InputMethodService
@Override @Override
public void onAppPrivateCommand(String command, Bundle data) public void onAppPrivateCommand(String command, Bundle data)
{ {
System.out.println("App private command: " + command);
} }
@Override @Override
public void onConfigurationChanged(Configuration newConfig) public void onConfigurationChanged(Configuration newConfig)
{ {
_inputView.reset(); _inputView.reset();
System.out.println("configuration change");
} }
public void handleKeyUp(KeyValue key, int flags) public void handleKeyUp(KeyValue key, int flags)
{ {
if (getCurrentInputConnection() == null) if (getCurrentInputConnection() == null)
return ; return ;
System.out.println("key up ");
if (key.getEventCode() == KeyValue.EVENT_CONFIG) if (key.getEventCode() == KeyValue.EVENT_CONFIG)
{ {
Intent intent = new Intent(this, SettingsActivity.class); Intent intent = new Intent(this, SettingsActivity.class);

View File

@ -14,7 +14,7 @@ import android.util.DisplayMetrics;
import android.util.TypedValue; import android.util.TypedValue;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import java.util.LinkedList; import java.util.ArrayList;
public class Keyboard2View extends View public class Keyboard2View extends View
implements View.OnTouchListener, Handler.Callback implements View.OnTouchListener, Handler.Callback
@ -26,7 +26,7 @@ public class Keyboard2View extends View
private Keyboard2 _ime; private Keyboard2 _ime;
private KeyboardData _keyboard; private KeyboardData _keyboard;
private LinkedList<KeyDown> _downKeys = new LinkedList<KeyDown>(); private ArrayList<KeyDown> _downKeys = new ArrayList<KeyDown>();
private int _flags = 0; private int _flags = 0;
@ -56,6 +56,9 @@ public class Keyboard2View extends View
private Paint _keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint _keyLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint _keyLabelLockedPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint _keyLabelLockedPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint _keySubLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private Paint _keySubLabelPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Paint _keySubLabelRightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private static RectF _tmpRect = new RectF();
public Keyboard2View(Context context, AttributeSet attrs) public Keyboard2View(Context context, AttributeSet attrs)
{ {
@ -77,8 +80,12 @@ public class Keyboard2View extends View
_keyLabelLockedPaint.setColor(getResources().getColor(R.color.key_label_locked)); _keyLabelLockedPaint.setColor(getResources().getColor(R.color.key_label_locked));
_keyLabelLockedPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size)); _keyLabelLockedPaint.setTextSize(getResources().getDimension(R.dimen.label_text_size));
_keyLabelLockedPaint.setTextAlign(Paint.Align.CENTER); _keyLabelLockedPaint.setTextAlign(Paint.Align.CENTER);
_keySubLabelPaint.setTextAlign(Paint.Align.LEFT);
_keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label)); _keySubLabelPaint.setColor(getResources().getColor(R.color.key_sub_label));
_keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size)); _keySubLabelPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size));
_keySubLabelRightPaint.setTextAlign(Paint.Align.RIGHT);
_keySubLabelRightPaint.setColor(getResources().getColor(R.color.key_sub_label));
_keySubLabelRightPaint.setTextSize(getResources().getDimension(R.dimen.sublabel_text_size));
setOnTouchListener(this); setOnTouchListener(this);
} }
@ -351,32 +358,30 @@ public class Keyboard2View extends View
{ {
float keyW = _keyWidth * k.width; float keyW = _keyWidth * k.width;
KeyDown keyDown = getKeyDown(k); KeyDown keyDown = getKeyDown(k);
_tmpRect.set(x + _keyBgPadding, y + _keyBgPadding,
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding);
if (keyDown != null) if (keyDown != null)
canvas.drawRect(x + _keyBgPadding, y + _keyBgPadding, canvas.drawRect(_tmpRect, _keyDownBgPaint);
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding, _keyDownBgPaint);
else else
canvas.drawRoundRect(new RectF(x + _keyBgPadding, y + _keyBgPadding, canvas.drawRoundRect(_tmpRect, _keyRound, _keyRound, _keyBgPaint);
x + keyW - _keyBgPadding, y + _keyHeight - _keyBgPadding), _keyRound, _keyRound, _keyBgPaint);
if (k.key0 != null) if (k.key0 != null)
canvas.drawText(k.key0.getSymbol(_flags), keyW / 2f + x, canvas.drawText(k.key0.getSymbol(_flags), keyW / 2f + x,
(_keyHeight + _keyLabelPaint.getTextSize()) / 2f + y, (_keyHeight + _keyLabelPaint.getTextSize()) / 2f + y,
(keyDown != null && (keyDown.flags & KeyValue.FLAG_LOCKED) != 0) (keyDown != null && (keyDown.flags & KeyValue.FLAG_LOCKED) != 0)
? _keyLabelLockedPaint : _keyLabelPaint); ? _keyLabelLockedPaint : _keyLabelPaint);
float subPadding = _keyBgPadding + _keyPadding; float subPadding = _keyBgPadding + _keyPadding;
_keySubLabelPaint.setTextAlign(Paint.Align.LEFT);
if (k.key1 != null) if (k.key1 != null)
canvas.drawText(k.key1.getSymbol(_flags), x + subPadding, canvas.drawText(k.key1.getSymbol(_flags), x + subPadding,
y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint); y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint);
if (k.key3 != null) if (k.key3 != null)
canvas.drawText(k.key3.getSymbol(_flags), x + subPadding, canvas.drawText(k.key3.getSymbol(_flags), x + subPadding,
y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint); y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint);
_keySubLabelPaint.setTextAlign(Paint.Align.RIGHT);
if (k.key2 != null) if (k.key2 != null)
canvas.drawText(k.key2.getSymbol(_flags), x + keyW - subPadding, canvas.drawText(k.key2.getSymbol(_flags), x + keyW - subPadding,
y + subPadding - _keySubLabelPaint.ascent(), _keySubLabelPaint); y + subPadding - _keySubLabelRightPaint.ascent(), _keySubLabelRightPaint);
if (k.key4 != null) if (k.key4 != null)
canvas.drawText(k.key4.getSymbol(_flags), x + keyW - subPadding, canvas.drawText(k.key4.getSymbol(_flags), x + keyW - subPadding,
y + _keyHeight - subPadding - _keySubLabelPaint.descent(), _keySubLabelPaint); y + _keyHeight - subPadding - _keySubLabelRightPaint.descent(), _keySubLabelRightPaint);
x += keyW; x += keyW;
} }
y += _keyHeight; y += _keyHeight;