I have custom MyKBV class which extends KeyBoardView.I created this custom view to use custom font for the keys. I am able to see the changed font on the keys but the problem is each key overlap's with the default keys from the XML which I presume is TypefaceE.DEFAULT_BOLD.SO what I am seeing is two strings on each key one bold and one with the font I want.How do I go about clearing the default keys so that only the custom ones are visible.I have spent a lot of time on this .It would be helpful if someone can point out where I am going wrong or what I could do.Thanks!!
public class MyKBV extends KeyboardView {
Context context;
@Override
public void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Paint paint = new Paint();
Typeface font = Typeface.createFromAsset(context.getAssets(),
"fonts/Hippie.otf");
paint.setTypeface(font);
paint.setTextSize(40);
List<Key> listKeys = getKeyboard().getKeys();
for (Key key : listKeys) {
if (key.label != null) {
if (key.label.toString().length() > 1) {
paint.setTextSize(30);
canvas.drawText(key.label.toString(), key.x
+ (key.width / 2) - 15, key.y + (key.height / 2)
+ 10, paint);
} else {
canvas.drawText(key.label.toString(), key.x
+ (key.width / 2) - 10, key.y + (key.height / 2)
+ 10, paint);
}
}
}
}
public MyKeyBoardView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context;
}
}
If you override the onDraw() you should draw background first then draw your text.
public class MyKeyboardView extends android.inputmethodservice.KeyboardView {
Context context;
public MyKeyboardView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.context = context ;
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
Typeface font = Typeface.createFromAsset(context.getAssets(),
"fonts/Hippie.otf");
paint.setTypeface(font);
paint.setTextSize(40);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
if(key.pressed){
NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.glow);
npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
npd.draw(canvas);
if(key.label != null)
canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
}else if(key.modifier){ // boolean that defines key is function key
NinePatchDrawable npd = (NinePatchDrawable)context.getResources().getDrawable(R.drawable.btn_keyboard_special);
npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height);
npd.draw(canvas);
if(key.label != null)
canvas.drawText(key.label.toString(), key.x + (key.width/2), key.y + 25, paint);
}
break;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.