gpt4 book ai didi

java - 单击时为按钮着色

转载 作者:行者123 更新时间:2023-12-01 23:13:35 25 4
gpt4 key购买 nike

我正在为 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

最佳答案

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

关于java - 单击时为按钮着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21550695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com