Raj
Raj

Reputation: 1872

rendaring issue with surfaceview in viewflipper?

i am drawing a piechart by extending surfaceview to PieChart Class. now i am creating 2 objects for Piechart and adding to a VieWFlipper to swipe between those charts. now my problem is 2nd Piechart is not visible to the user if user swipes to 2nd view. but all the 2nd pies functionality is working. i am thinking like its refresh problem of the surfaceview.

any help on this will be appreciable. the following is my PieChart class.

class MyPieChart extends SurfaceView implements SurfaceHolder.Callback {

    @Override
    public void onDraw(Canvas canvas) {
        if (hasData) {
            resetColor();
            try {
                canvas.drawColor(getResources().getColor(R.color.graphbg_color));

                graphDraw(canvas);
            } catch (ValicException ex) {

            }

        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

        Log.i("PieChart", "surfaceChanged");

    }

    public int callCount = 0;

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if ((callCount++) % 2 == 0) {
            callCount = 1;
            try {
                 Log.i("PieChart", "surfaceCreated");
                mChartThread = new ChartThread(getHolder(), this);
                mChartThread.setRunning(true);

                if (!mChartThread.isAlive()) {
                    mChartThread.start();
                }

                mFrame = holder.getSurfaceFrame();

                mOvalF = new RectF(0, 0, mFrame.right, mFrame.right);

            } catch (Exception e) {
                // No error message required
            }
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
         Log.i("PieChart", "surfaceDestroyed");
        boolean retry = true;
        callCount = 0;
        mChartThread.setRunning(false);
        while (retry) {
            try {
                mChartThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // No error message required
            }
        }
    }
}

class ChartThread extends Thread {
    private SurfaceHolder mSurfaceHolder;
    private PieChart mPieChart;
    private boolean mRefresh = false;

    public ChartThread(SurfaceHolder surfaceHolder, PieChart pieChart) {
        // Log.i("ChartThread", "ChartThread");
        mSurfaceHolder = surfaceHolder;
        mPieChart = pieChart;
    }

    public void setRunning(boolean Refresh) {
        // Log.i("ChartThread", "setRunning : " + Refresh);
        mRefresh = Refresh;
    }

    @Override
    public void run() {
        Canvas c;
        // Log.i("ChartThread", "run : " + mRefresh);
        while (mRefresh) {
            c = null;
            try {
                c = mSurfaceHolder.lockCanvas(mPieChart.mFrame);
            //  c.drawColor(0xFFebf3f5);
                synchronized (mSurfaceHolder) {

                    mPieChart.onDraw(c);

                }
            } catch (Exception ex) {

            } finally {
                // do this in a finally so that if an exception is thrown
                // during the above, we don't leave the Surface in an
                // inconsistent state
                if (c != null) {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

here is my flipper.xml

<ViewFlipper  
android:id="@+id/flipper"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout  android:id="@+id/pie1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" />

<LinearLayout android:id="@+id/pie2" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" />

and i here is my activity

public class ViewFlipperActivity extends Activity {
    Button b1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        b1 = (Button)findViewById(R.id.submit1);

        b1.setOnClickListener(new View.OnClickListener() {
            ViewFlipper vflipper=(ViewFlipper)findViewById(R.id.flipper);   
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                vflipper.showNext();

            }
        });

    }

and i am dding the piecharts to Pie1 and Pie2 LinearLayouts in the Flipper.

both the pie's are created and pasted on to the Pie Layouts. now if i move to the 2nd view in the flipper Pie1 is showing instead of pie2 and all other data and functionality which i am getting is related to Pie2. my doubt is Pie2 is rendering and hidden under Pie1. can any one help me on this with some solution.

i got a break through for this issue. which caused another issue with the following changes.

in flipper.xml replaced LinearLayout i place of view flipper.

    <LinearLayout  
    android:id="@+id/flipper"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout  android:id="@+id/pie1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" />

   </LinearLayout  >

and in ViewFlipperActivity

    public class ViewFlipperActivity extends Activity {
        Button b1;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            b1 = (Button)findViewById(R.id.submit1);

            b1.setOnClickListener(new View.OnClickListener() {
                LinearLayout vflipper=(LinearLayout)findViewById(R.id.flipper); 
LinearLayout pie1=(LinearLayout)findViewById(R.id.pie1);    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    vflipper.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));

pie1.removeAllViews();
pie1.addView(PieChart/SurfaceView);

                }
            });

        }

with the animation and is working fine and piechart is getting changed on the view. but block rect is getting visible from the surfaceview for a sec when swiping between piecharts. can some one help me on this issue.

Upvotes: 1

Views: 1247

Answers (2)

Raj
Raj

Reputation: 1872

here main problem is SurfaceView cannot be animated. that's why neither ViewFlipper nor Layout Animation cannot be applied for SurfaceView.

Upvotes: 0

Romain Guy
Romain Guy

Reputation: 98501

SurfaceView is not going to work properly inside a ViewFlipper as it cannot be animated correctly. This is why Android 4.0 introduces a new widget called TextureView that solves this problem.

Upvotes: 2

Related Questions