0

我试图用画布画一条线。加载 Activity 时它的值为 0,然后我有一个具有单击侦听器的 Button 来更改值并画一条线。它在模拟器中运行良好,但是当我在我的真实设备(android 版本 4.1)中运行时,画布没有改变,但我知道我点击了按钮,因为我在点击监听器中放了一个吐司。这真的很奇怪。

有没有人遇到过同样的问题?任何想法将不胜感激。

以下是我的活动:

public class MainActivity extends Activity{

    private Paint paintFree = new Paint();
    private Paint paintLocal = new Paint();
    private Paint paintRoaming = new Paint();

    private int freeUsage = 0;
    private int localUsage = 0;
    private int roamingUsage = 0;

    private int freeBarPoints;
    private int localBarPoints;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        overridePendingTransition(0, 0);
        line();

        ((Button) findViewById(R.id.btn1)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                freeUsage = 12;
                localUsage = 1;
                roamingUsage = 1;
                line();
                Log.i("Hit Btn1", "True");
                Toast.makeText(v.getContext(), "Hit Btn1", Toast.LENGTH_SHORT).show();
            }
        });

    }

    class Draw extends View{

        public Draw(Context context) {
            super(context);
            // TODO Auto-generated constructor stub

            paintFree.setStrokeWidth(20f);
            paintLocal.setStrokeWidth(20f);
            paintRoaming.setStrokeWidth(20f);

             if (freeUsage == 0){
                    paintFree.setColor(Color.GRAY);
                } else {
                    paintFree.setColor(Color.rgb(70, 227, 78));
                }
                if (localUsage == 0){
                    paintLocal.setColor(Color.GRAY);
                } else {
                    paintLocal.setColor(Color.rgb(238, 232, 102));
                }
                if (roamingUsage == 0){
                    paintRoaming.setColor(Color.GRAY);
                } else {
                    paintRoaming.setColor(Color.rgb(101, 177, 231));
                }

        }

        protected void onDraw(Canvas canvas) {

            int maxBarLength = canvas.getWidth() * 4 / 5;
            double totalBarPoints = freeUsage + localUsage + roamingUsage;  
            freeBarPoints = (int) Math.round(freeUsage * maxBarLength / totalBarPoints);
            localBarPoints = (int) Math.round(localUsage * maxBarLength / totalBarPoints);
            // need not compute the roaming bar points  

            int localStartX = 0 + Math.round(freeBarPoints);
            int roamingStartX = (int) localStartX + Math.round(localBarPoints);

            canvas.drawLine(0, 10, localStartX, 10, paintFree);
            canvas.drawLine(localStartX, 10, roamingStartX, 10, paintLocal);
            canvas.drawLine(roamingStartX, 10, maxBarLength, 10, paintRoaming);
        }


    }

    public void line(){
        Draw draw;
        draw = new Draw(this);
        ((LinearLayout) findViewById(R.id.linear)).addView(draw);   
    }

}
4

1 回答 1

0

您需要为onMeasure您的类添加一个实现Draw。查看http://developer.android.com/training/custom-views/custom-drawing.html了解更多详情。

于 2013-08-28T02:07:52.943 回答