我创建了两个自定义视图,我想切换该视图

时间:2012-03-17 10:23:24

标签: android

为什么这段代码不起作用?它总是强制结束我想切换视图,但它没有发生。它要么强迫杀死或显示黑屏。我想切换四个视图。如何创建动画?

 public class DrawDemoActivity extends Activity {
        DemoView demoview ;
        FinalView finalview;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean first=true;
        boolean sec=false;
        demoview = new DemoView(this);
        finalview = new FinalView(this);
           // for contnivew switching the view 
        for(int i=0;i>0;i++)
        {
            if (first==true||sec==false)
            {
                setContentView(finalview);
                first=false;
                sec=true;
            }else if(first==false||sec==true)
            {
                setContentView(demoview);
                first=true;
                sec=true;
            }else if(first==true||sec==true)
            {
                setContentView(demoview);
                first=false;
                sec=false;
            }else
            {   
                setContentView(demoview);
                first=false;
                sec=false;
            }
        }
    }
//first view 
    private class DemoView extends View{
        public DemoView(Context context){
            super(context);
        }

        @Override protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            // custom drawing code here
            // remember: y increases from top to bottom
            // x increases from left to right
            int x = 150;
            int y = 200;
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);

            // make the entire canvas white
            paint.setColor(Color.CYAN);
            canvas.drawPaint(paint);
            // another way to do this is to use:
            // canvas.drawColor(Color.WHITE);

            // draw blue circle with antialiasing turned on
            paint.setAntiAlias(true);

            paint.setColor(Color.RED);
            canvas.drawCircle(x-24, y+30, 11, paint);
            paint.setColor(Color.WHITE);            
            canvas.drawLine(0, 300 , 320, 300, paint);
        }
    }
    //second view 
        private class FinalView extends View {
            public FinalView(Context context){
        super(context);
        }

            @Override 
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                // custom drawing code here
                // remember: y increases from top to bottom
                // x increases from left to right
                int x = 150;
                int y = 200;
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);

                // make the entire canvas white
                paint.setColor(Color.CYAN);
                canvas.drawPaint(paint);
                // another way to do this is to use:
                // canvas.drawColor(Color.WHITE);

                // draw blue circle with antialiasing turned on
                paint.setAntiAlias(true);


                paint.setColor(Color.BLACK);
                canvas.drawCircle(x, y, 11, paint);
                canvas.drawCircle(x-11, y+19, 11, paint);
                canvas.drawCircle(x, y+38, 11, paint);
                canvas.drawCircle(x+20, y+30, 11, paint);
                canvas.drawCircle(x-11, y-19, 11, paint);


                paint.setColor(Color.RED);
                canvas.drawCircle(x+41, y+30, 11, paint);
                canvas.drawCircle(x+20, y+9, 11, paint);
                canvas.drawCircle(x-23, y-37, 11, paint);

                canvas.drawLine(0, 300 , 320, 300, paint);      
          }
    }
}

2 个答案:

答案 0 :(得分:0)

似乎你的setContentView()不会被调用,因为你的for循环从不迭代

for(int i=0;i>0;i++)

答案 1 :(得分:0)

问题似乎出现在onCreate方法中。由于for循环,setContentView方法无法访问,i永远不会大于零。 int i=0;i>0;i++

此外,如上所述,Anand使用ViewFlipper以防您想要切换视图。你可以找到一个很好的例子here