如何在运行时更改行的颜色

时间:2013-04-04 11:51:00

标签: android

我正在制作匹配该列的应用程序。每当用户点击错误的复选按钮时,绘制的线条会将颜色更改为红色。我无法改变颜色。这是我的代码

public void onClick(View v) 
{
    Paint cpaint=new Paint();
    cpaint.setStyle(Paint.Style.STROKE);
    cpaint.setStrokeWidth(10);
    cpaint.setAntiAlias(true);
    cpaint.setDither(true);
    cpaint.setStrokeJoin(Paint.Join.ROUND);
    cpaint.setColor(Color.RED);
    Canvas canvas=new Canvas();
for(Bean b:ld.lb)
{
    System.out.println("Offset_x"+b.offset_x);
    System.out.println("Offset_y"+b.offset_y);
    System.out.println("current_x"+b.current_x);
    System.out.println("current_y"+b.current_y);
    if ((b.offset_y>=3.0 || b.offset_y<=10.0)&&(b.current_y>=50 || b.current_y<=60.0))
    {
        System.out.println("Under the first condition");
        canvas.drawLine(b.offset_x,b.offset_y,b.current_x, b.current_y, cpaint);
    }
    if ((b.offset_y>=45.0 || b.offset_y<=50.0)&&(b.current_y>=5 || b.current_y<=10.0))
    {
        System.out.println("Undet the second condition");
        canvas.drawLine(b.offset_x, b.offset_y, b.current_x, b.current_y, cpaint);
    }   
}

查看类代码

            cpaint.setStyle(Paint.Style.STROKE);
    cpaint.setStrokeWidth(10);
    cpaint.setAntiAlias(true);
    cpaint.setDither(true);
    cpaint.setStrokeJoin(Paint.Join.ROUND);
    cpaint.setColor(Color.GRAY);
    lb=new ArrayList<Bean>();
    tv1=(TextView)findViewById(R.id.t1);
    tv2=(TextView)findViewById(R.id.t2);
    tv3=(TextView)findViewById(R.id.t3);
    tv4=(TextView)findViewById(R.id.t4);


}

@Override
public boolean dispatchTouchEvent(MotionEvent event) {

if(event.getAction()==MotionEvent.ACTION_DOWN)
{
    offset_x=current_x=event.getX();
    offset_y=current_y=event.getY();
}

else if(event.getAction()==MotionEvent.ACTION_UP)
{
    current_x=event.getX();
    current_y=event.getY();
    System.out.println("Y axis is"+offset_y);
    System.out.println("Y axis is"+current_y);
     addLine(offset_x,offset_y,current_x,current_y);
}



    return true;
}

public void addLine(float x,float y,float ex,float ey)
{

    Bean bl=new Bean();
    bl.setPoints(x, y, ex, ey);
    lb.add(bl);
}
@Override
public void draw(Canvas canvas) {
    // TODO Auto-generated method stub
    for(Bean b:lb)
    {
        canvas.drawLine(b.offset_x,b.offset_y,b.current_x,b.current_y, cpaint);
    }
    invalidate();
}

0 个答案:

没有答案