После того, как я применил метод onDraw к своему пользовательскому приложению клавиатуры, кажется, что мое приложение потребляет так много батареи и оперативной памяти.Я только что нашел способ нарисовать круг на своей клавиатуре с помощью метода onDraw, который расширяет клавиатурный вид.Мой класс onDraw ниже, может кто-нибудь взглянуть на это.
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
healA = ddKeys_Master.autohealing;
phealA = ddKeys_Master.autophealing;
Paint paint = new Paint();
paint.setTextAlign(Paint.Align.CENTER);
//get all your keys and draw whatever you want
List<Keyboard.Key> keys = getKeyboard().getKeys();
for (Keyboard.Key key : keys) {
if (key.label != null) {
paint.setColor(Color.parseColor("#66cccc"));
switch (key.codes[0]) {
case 888:
if (healA && phealA) {
if (ddKeys_Master.healcount >= ddKeys_Master.autoHealCount) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount * 3 / 4)) {
paint.setColor(Color.parseColor("#ef6904")); //orange
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount / 2)) {
paint.setColor(Color.parseColor("#efeb04")); //yellow
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount / 4)) {
paint.setColor(Color.parseColor("#09f247")); //light green
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else {
paint.setColor(Color.parseColor("#17a55e")); //green
//orange #ef6904; yellow #efeb04; light green #09f247; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
}
if (ddKeys_Master.phealcount >= ddKeys_Master.autoPhealCount) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(37 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount * 3 / 4)) {
paint.setColor(Color.parseColor("#ef6904")); //orange
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(37 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount / 2)) {
paint.setColor(Color.parseColor("#efeb04")); //yellow
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(37 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount / 4)) {
paint.setColor(Color.parseColor("#09f247")); //light green
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(37 * scale, 17 * scale, 7 * scale, paint);
} else {
paint.setColor(Color.parseColor("#17a55e")); //green
//orange #ef6904; yellow #efeb04; light green #09f247; green #17a55e
canvas.drawCircle(37 * scale, 17 * scale, 7 * scale, paint);
}
} else if (healA) {
if (ddKeys_Master.healcount >= ddKeys_Master.autoHealCount) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount * 3 / 4)) {
paint.setColor(Color.parseColor("#ef6904")); //orange
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount / 2)) {
paint.setColor(Color.parseColor("#efeb04")); //yellow
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount / 4)) {
paint.setColor(Color.parseColor("#09f247")); //light green
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else {
paint.setColor(Color.parseColor("#17a55e")); //green
//orange #ef6904; yellow #efeb04; light green #09f247; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
}
} else if (phealA) {
if (ddKeys_Master.phealcount >= ddKeys_Master.autoPhealCount) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount * 3 / 4)) {
paint.setColor(Color.parseColor("#ef6904")); //orange
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount / 2)) {
paint.setColor(Color.parseColor("#efeb04")); //yellow
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount / 4)) {
paint.setColor(Color.parseColor("#09f247")); //light green
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
} else {
paint.setColor(Color.parseColor("#17a55e")); //green
//orange #ef6904; yellow #efeb04; light green #09f247; green #17a55e
canvas.drawCircle(17 * scale, 17 * scale, 7 * scale, paint);
}
}
break;
case 901:
if (!ddKeys_Master.xprate) {
paint.setColor(Color.parseColor("#356dce")); //blue
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(key.x + (key.width / 2), key.y + (45 * scale), 4 * scale, paint);
}
if (ddKeys_Master.phealcount >= ddKeys_Master.autoPhealCount) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(key.x + (key.width / 2), key.y + (45 * scale), 4 * scale, paint);
}
break;
case 903:
if (healA) {
if (ddKeys_Master.healcount >= (ddKeys_Master.autoHealCount)) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(key.x + (key.width / 2), key.y + (45 * scale), 4 * scale, paint);
} else {
paint.setColor(Color.parseColor("#356dce")); //blue
canvas.drawCircle(key.x + (key.width / 2), key.y + (45 * scale), 4 * scale, paint);
}
}
break;
case 904:
if (phealA) {
if (ddKeys_Master.phealcount >= (ddKeys_Master.autoPhealCount)) {
paint.setColor(Color.parseColor("#ef0404")); //red
//orange #ef6904; yellow #efeb04; green #17a55e
canvas.drawCircle(key.x + (key.width / 2), key.y + (45 * scale), 4 * scale, paint);
} else {
paint.setColor(Color.parseColor("#356dce"));
canvas.drawCircle(key.x + (key.width / 2), key.y + (45 * scale), 4 * scale, paint);
}
}
break;
case 906:
paint.setColor(Color.parseColor("#3d4754"));
canvas.drawCircle(252 * scale, 61 * scale, 17 * scale, paint);
paint.setColor(Color.parseColor("#2e3a42"));
canvas.drawCircle(252 * scale, 61 * scale, 16 * scale, paint);
paint.setColor(Color.parseColor("#b3cbdb"));
paint.setTextSize(12 * scale);
canvas.drawText(sTime, 252 * scale, 65 * scale, paint);
invalidate();
setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Paint paintt = new Paint();
float x = event.getX();
float y = event.getY();
float dx = Math.abs(x - (252 * scale));
float dy = Math.abs(y - (61 * scale));
float R = 17 * scale;//radius of circle.
if (dx > R) {
Log.i(TAG, "Pressed outside");
return false;//outside
} else if (dy > R) {
Log.i(TAG, "Pressed outside");
return false;//
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.i(TAG, "Button Pressed Successfully");
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
getOnKeyboardActionListener().onKey(666, null);
return true;
}
return false;
}
});
break;
}
}
}
}
Как мне сделать код более плавным, чтобы сохранить заряд батареи?