简体   繁体   English

单击时为按钮着色

[英]color a button when is clicked

I am creating a small calculator for Android. 我正在为Android创建一个小型计算器。 The keyboard of the calculator is created by Java code in OnDraw: 计算器的键盘是通过OnDraw中的Java代码创建的:

public void onDraw(Canvas canvas){
    buttonH = height/rows;
    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.CENTER);
    AssetManager assetManager = getResources().getAssets();
        Typeface tf = Typeface.create(Typeface.createFromAsset(assetManager,"fonts/font.ttf"),
    Typeface.NORMAL);

    paint.setTypeface(tf);
    paint.setAntiAlias(true);

    textSize = (width+height)/43;

    paint.setTextSize(textSize);
    paint.setColor(Color.rgb(0, 0, 0));

    Bitmap[][] buttons = new Bitmap[rows][];
    int spacing = (width+height)/400;

    LinearGradient gradient = new LinearGradient(buttonW/2,0,buttonW/2,buttonH,
                buttonShade,null,Shader.TileMode.REPEAT);

    for(int i = 0; i < rows; i++){
        int thisColumns = cScreen[i].length;
        buttons[i] = new Bitmap[thisColumns];
        buttonW = width/thisColumns;
        for(int j = 0; j < thisColumns; j++){

            buttons[i][j] = Bitmap.createBitmap(buttonW,buttonH,Bitmap.Config.ARGB_8888);
            Canvas temp = new Canvas(buttons[i][j]);
            paint.setShader(gradient1);
            RectF rect = new RectF(spacing,spacing,buttonW-spacing,buttonH-spacing);
            temp.drawRoundRect(rect, width/100,height/100,paint);
            paint.setShader(null);
            temp.drawText(cScreen[i][j],buttonW/2,(buttonH+textSize)/2,paint);
            canvas.drawBitmap(buttons[i][j], buttonW*j+shift,buttonH*i, paint);
        }
    }
}

but I would like to color the area of the button when the button is clicked 但是我想在单击按钮时为按钮的区域着色

在此处输入图片说明

If you want just to color the button while it is pressed you can implement this using xml. 如果只想在按下按钮时给按钮上色,则可以使用xml来实现。

   <?xml version="1.0" encoding="utf-8"?> 
   <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
      <item android:state_focused="true" android:state_pressed="true" 
            android:drawable="@drawable/bg_clolor_highlight" /> 
      <item android:state_focused="false" android:state_pressed="true" 
            android:drawable="@drawable/bg_clolor_highlight" /> 
      <item android:drawable="@drawable/bg_clolor_normal" /> 
  </selector>

Otherwise you can just call .setBackgroundColor(your_color) on the button. 否则,您只需在按钮上调用.setBackgroundColor(your_color)。

Create a different Paint object in onDraw for the clicked button: 在onDraw中为单击的按钮创建一个不同的Paint对象:

 Paint p=new Paint(); 
 paint.setColor(Color.RED);

Get the coordinates of the click position on the canvas: 获取画布上单击位置的坐标:

in constructor attach an onTouch listener() 在构造函数中附加一个onTouch listener()

float x,y;

this.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View arg0, MotionEvent event) {
          x=event.getX();y=event.getY();
     }
});

in onDraw: 在onDraw中:

    Paint p=new Paint(); 
    paint.setColor(Color.RED);
for(int i = 0; i < rows; i++){
    int thisColumns = cScreen[i].length;
    buttons[i] = new Bitmap[thisColumns];
    buttonW = width/thisColumns;

    for(int j = 0; j < thisColumns; j++){
        Paint tempPaint;
         if(x> buttonW*j+shift&&x< buttonW*j+shift+buttonW&&y>buttonH*i&&y<buttonH*i+buttonH) 
         tempPaint=p;
         else
         tempPaint=paint;
        buttons[i][j] = Bitmap.createBitmap(buttonW,buttonH,Bitmap.Config.ARGB_8888);
        Canvas temp = new Canvas(buttons[i][j]);
        tempPaint.setShader(gradient1);
        RectF rect = new RectF(spacing,spacing,buttonW-spacing,buttonH-spacing);
        temp.drawRoundRect(rect, width/100,height/100,tempPaint);
        tempPaint.setShader(null);
        temp.drawText(cScreen[i][j],buttonW/2,(buttonH+textSize)/2,tempPaint);

        canvas.drawBitmap(buttons[i][j], buttonW*j+shift,buttonH*i, tempPaint);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM