单击时为按钮着色

时间:2014-02-04 11:05:39

标签: java android view ontouchevent ondraw

我正在为Android创建一个小型计算器。计算器的键盘由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);
        }
    }
}

但是我想在点击按钮时为按钮的区域着色

enter image description here

2 个答案:

答案 0 :(得分:2)

如果您只想在按下按钮时为按钮着色,可以使用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>

否则你可以在按钮上调用.setBackgroundColor(your_color)。

答案 1 :(得分:1)

在onDraw中为单击的按钮创建一个不同的Paint对象:

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

获取画布上点击位置的坐标:

在构造函数中附加onTouch侦听器()

float x,y;

this.setOnTouchListener(new OnTouchListener() {
     @Override
     public boolean onTouch(View arg0, MotionEvent event) {
          x=event.getX();y=event.getY();
     }
});
在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);
    }
}