我在做某种“游戏分数追踪器”。以下是该应用程序当前的工作方式:
例句:如果有3个球员“詹姆斯,约翰和罗伯特”。然后用户给詹姆斯加5分,给约翰加10分,给罗伯特加15分。这就是TextViews的外观:
詹姆斯五世
约翰10
那么如果用户再次执行完全相同的操作,则会出现以下情况:
詹姆斯五世
约翰10
所以你可以看到,我不为每个玩家保留相同的TextView,但我一直在添加他们(我确实希望这样,我希望用户能够看到他的动作,当他点击-和当+。但是有没有办法为每个用户设置一个颜色?例如:James将是蓝色,John将是红色,Robert将是绿色。我如何预先确定每个玩家的TextView的颜色?
播放机类:
public static class Player implements Parcelable{
String name;
int score;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(score);
dest.writeString(name);
}
public Player(Parcel source){
score = source.readInt();
name = source.readString();
}
public Player(){
}
public static final Parcelable.Creator<Player> CREATOR = new Parcelable.Creator<Novaigra.Player>() {
@Override
public Player createFromParcel(Parcel source) {
return new Player(source);
}
@Override
public Player[] newArray(int size) {
return new Player[size];
}
};
}
当玩家被添加的时候,我可以添加某种调色板到活动中,这样用户就可以预先确定一种颜色什么的吗?
这是一个颜色选择器,android没有内置的颜色选择器,可以根据你的需要随意修改
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class ColorPickerDialog extends Dialog {
public interface OnColorChangedListener {
void colorChanged(String key, int color);
}
private OnColorChangedListener mListener;
private int mInitialColor, mDefaultColor;
private String mKey;
private class ColorPickerView extends View {
private Paint mPaint;
private float mCurrentHue = 0;
private int mCurrentX = 0, mCurrentY = 0;
private int mCurrentColor, mDefaultColor;
private final int[] mHueBarColors = new int[258];
private int[] mMainColors = new int[65536];
private OnColorChangedListener mListener;
private int width,height;
ColorPickerView(Context c, OnColorChangedListener l, int color,
int defaultColor) {
super(c);
mListener = l;
mDefaultColor = defaultColor;
// Get the current hue from the current color and update the main
// color field
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
mCurrentHue = hsv[0];
updateMainColors();
mCurrentColor = color;
// Initialize the colors of the hue slider bar
int index = 0;
for (float i = 0; i < 256; i += 256 / 42) // Red (#f00) to pink
// (#f0f)
{
mHueBarColors[index] = Color.rgb(255, 0, (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Pink (#f0f) to blue
// (#00f)
{
mHueBarColors[index] = Color.rgb(255 - (int) i, 0, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Blue (#00f) to light
// blue (#0ff)
{
mHueBarColors[index] = Color.rgb(0, (int) i, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Light blue (#0ff) to
// green (#0f0)
{
mHueBarColors[index] = Color.rgb(0, 255, 255 - (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Green (#0f0) to yellow
// (#ff0)
{
mHueBarColors[index] = Color.rgb((int) i, 255, 0);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) // Yellow (#ff0) to red
// (#f00)
{
mHueBarColors[index] = Color.rgb(255, 255 - (int) i, 0);
index++;
}
// Initializes the Paint that will draw the View
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setTextSize(12);
}
// Get the current selected color from the hue bar
private int getCurrentMainColor() {
int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
int index = 0;
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(255, 0, (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(255 - (int) i, 0, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(0, (int) i, 255);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(0, 255, 255 - (int) i);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb((int) i, 255, 0);
index++;
}
for (float i = 0; i < 256; i += 256 / 42) {
if (index == translatedHue)
return Color.rgb(255, 255 - (int) i, 0);
index++;
}
return Color.RED;
}
// Update the main field colors depending on the current selected hue
private void updateMainColors() {
int mainColor = getCurrentMainColor();
int index = 0;
int[] topColors = new int[256];
for (int y = 0; y < 256; y++) {
for (int x = 0; x < 256; x++) {
if (y == 0) {
mMainColors[index] = Color.rgb(
255 - (255 - Color.red(mainColor)) * x / 255,
255 - (255 - Color.green(mainColor)) * x / 255,
255 - (255 - Color.blue(mainColor)) * x / 255);
topColors[x] = mMainColors[index];
} else
mMainColors[index] = Color.rgb(
(255 - y) * Color.red(topColors[x]) / 255,
(255 - y) * Color.green(topColors[x]) / 255,
(255 - y) * Color.blue(topColors[x]) / 255);
index++;
}
}
}
@Override
protected void onDraw(Canvas canvas) {
int translatedHue = 255 - (int) (mCurrentHue * 255 / 360);
// Display all the colors of the hue bar with lines
for (int x = 0; x < 256; x++) {
// If this is not the current selected hue, display the actual
// color
if (translatedHue != x) {
mPaint.setColor(mHueBarColors[x]);
mPaint.setStrokeWidth(1);
} else // else display a slightly larger black line
{
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(3);
}
canvas.drawLine(x + 10, 0, x + 10, 40, mPaint);
// canvas.drawLine(0, x+10, 40, x+10, mPaint);
//canvas.drawLine(x + width/27, 0, x + width/27,height/8 , mPaint);
}
// Display the main field colors using LinearGradient
for (int x = 0; x < 256; x++) {
int[] colors = new int[2];
colors[0] = mMainColors[x];
colors[1] = Color.BLACK;
Shader shader = new LinearGradient(0, 50, 0, 306, colors, null,
Shader.TileMode.REPEAT);//0,50,0,306
mPaint.setShader(shader);
canvas.drawLine(x + 10, 50, x + 10, 306, mPaint);
//canvas.drawLine(x + width/27, height/8 + 10, x + width/27, height*(5/6), mPaint);
}
mPaint.setShader(null);
// Display the circle around the currently selected color in the
// main field
if (mCurrentX != 0 && mCurrentY != 0) {
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
canvas.drawCircle(mCurrentX, mCurrentY, 10, mPaint);
}
// Draw a 'button' with the currently selected color
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mCurrentColor);
//canvas.drawRect(10, 316, 138, 356, mPaint);
//canvas.drawRect(width/27, height*(5/6), width/2, height, mPaint);
// Set the text color according to the brightness of the color
if (Color.red(mCurrentColor) + Color.green(mCurrentColor)
+ Color.blue(mCurrentColor) < 384)
mPaint.setColor(Color.WHITE);
else
mPaint.setColor(Color.BLACK);
// canvas.drawText(
// "New Color", 74,
//340, mPaint);
// Draw a 'button' with the default color
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(mDefaultColor);
//canvas.drawRect(138, 316, 266, 356, mPaint);
//canvas.drawRect(width/2, height*(5/6), width, height, mPaint);
// Set the text color according to the brightness of the color
if (Color.red(mDefaultColor) + Color.green(mDefaultColor)
+ Color.blue(mDefaultColor) < 384)
mPaint.setColor(Color.WHITE);
else
mPaint.setColor(Color.BLACK);
//canvas.drawText(
// "Default Color", 202, 340,
//mPaint);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(276,366 );//276,366
width = widthMeasureSpec;
height = heightMeasureSpec;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN)
return true;
float x = event.getX();
float y = event.getY();
// If the touch event is located in the hue bar
if (x > 10 && x < 266 && y > 0 && y < 40) {
// Update the main field colors
mCurrentHue = (255 - x) * 360 / 255;
updateMainColors();
// Update the current selected color
int transX = mCurrentX - 10;
int transY = mCurrentY - 60;
int index = 256 * (transY - 1) + transX;
if (index > 0 && index < mMainColors.length)
mCurrentColor = mMainColors[256 * (transY - 1) + transX];
// Force the redraw of the dialog
invalidate();
}
// If the touch event is located in the main field
if (x > 10 && x < 266 && y > 50 && y < 306) {
mCurrentX = (int) x;
mCurrentY = (int) y;
int transX = mCurrentX - 10;
int transY = mCurrentY - 60;
int index = 256 * (transY - 1) + transX;
if (index > 0 && index < mMainColors.length) {
// Update the current color
mCurrentColor = mMainColors[index];
mListener.colorChanged("", mCurrentColor);
// Force the redraw of the dialog
invalidate();
}
}
// If the touch event is located in the left button, notify the
// listener with the current color
//if (x > 10 && x < 138 && y > 316 && y < 356)
// mListener.colorChanged("", mCurrentColor);
// If the touch event is located in the right button, notify the
// listener with the default color
//if (x > 138 && x < 266 && y > 316 && y < 356)
// mListener.colorChanged("", mDefaultColor);
return true;
}
}
public ColorPickerDialog(Context context, OnColorChangedListener listener,
String key, int initialColor, int defaultColor) {
super(context);
mListener = listener;
mKey = key;
mInitialColor = initialColor;
mDefaultColor = defaultColor;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OnColorChangedListener l = new OnColorChangedListener() {
public void colorChanged(String key, int color) {
mListener.colorChanged(mKey, color);
dismiss();
}
};
setContentView(new ColorPickerView(getContext(), l, mInitialColor,
mDefaultColor));
setTitle("Pick Text Color");
}
}
然后,当您从colorchangelistener获得int颜色时,当他们选择或如果它使用默认值或其他任何东西时,将int保存到播放器中
编辑:我想我取消了默认设置,因为它不能很好地跨设备缩放,有些东西我不记得了,你可能会去检查一下,它可以工作,尽管你需要一个按钮或其他东西来触发颜色选择器
在您的活动中
import "your package name".ColorPickerDialog
然后让你的活动
implement OnColorChangeListener
然后在颜色选择器出现的按钮中
ColorPickerDialog color = new ColorPickerDialog(this,this, "picker",Color.BLACK,Color.WHITE);
color.show();
@Override
public void colorChanged(String key, int color) {
// TODO Auto-generated method stub
paint.setColor(color);
}
我只有paint.setColor(),但您可以将int color保存到Player类中,并将其用于TextViews中的文本颜色。
问题内容: 如果折线图中的数据点高于某个特定值,是否可以为该数据点设置其他颜色? 问题答案: 对于chartjs 2.0,请参见以下答案。 下面是原始答案。 关于ChartJS的好问题。我一直想做类似的事情。即动态地将点颜色更改为其他颜色。您在下面尝试过此吗?我只是尝试过,它对我有用。 试试这个: 或尝试以下方法: 甚至这个: 然后执行以下操作: 我想你可能会喜欢 仍然尝试一下。
fontColor(int $color): self int $color $config = [ 'path' => './tests' ]; $fileObject = new \Vtiful\Kernel\Excel($config); $fileObject = $fileObject->fileName('tutorial.xlsx'); $fileHandle =
我有一个旋转器下拉列表,我想改变所选项目的颜色,但不是列表项目。 我想要的是使选定的项目文本颜色在蓝色背景只有白色和其他的黑色,因为他们是。
问题内容: 我是iOS开发的新手,但我陷入了困境。我正在尝试使用每个面都有不同颜色的SceneKit渲染立方体。 到目前为止,这是我得到的: 但是我希望每张脸都有不同的颜色。我怎么做? 问题答案: 该盒子由六个不同的元素组成(每侧一个)。您可能还已经注意到,几何对象具有 第一种 材料的一个属性,但也具有一系列材料的属性。 具有多个元素和多种材质的对象将选择每个元素的材质增量(并环绕)。 例如4种元
我想增加我的jtable的易读性,这里是MyTableModel.java类下面,如何使每一行与2不同的颜色显示在这张图。应该是什么具体的方法,我可以给每一行不同的颜色,以增加用户的易读性。 Netbeans用变量名自动创建了我的jtable,我设置的变量名是mytable,然后我在下面定义prepareRenderer时遇到问题,我是否错过了这里的步骤?我想用不同的颜色制作每一行,下面是我的示例
我有一个文件,它有固定的行,但每一行都映射到不同的Java类。 例如 对于第一排,我有后续类: 第二排我有: 我想将TSV文件直接解析到相应的对象,但我没有什么想法。