我通过将surfaceview扩展到PieChart Class来绘制一个饼图。现在我正在为Piechart创建2个对象,并添加到VieWFlipper以在这些图表之间滑动。现在我的问题是如果用户滑动到第二个视图,用户看不到第二个饼图。但所有第二个馅饼的功能都在起作用。我在想它的表面视图的刷新问题。
对此的任何帮助都会很明显。以下是我的PieChart课程。
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);
}
}
}
}
}
这是我的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" />
我在这里是我的活动
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();
}
});
}
我正在将折页图转移到Flipper中的Pie1和Pie2 LinearLayouts。
创建饼图并将其粘贴到饼图布局上。现在如果我移动到翻板中的第二个视图Pie1显示而不是pie2,我得到的所有其他数据和功能都与Pie2有关。我的疑问是Pie2正在渲染并隐藏在Pie1下。任何人都可以通过一些解决方案来帮助我。
我突破了这个问题。这导致了以下更改的另一个问题。
在flipper.xml中的取代了LinearLayout,取代了视角翻板。
<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 >
和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);
}
});
}
使用动画并且工作正常,并且视图上的饼图正在变化。但是当在饼图之间滑动时,阻止矩形从表面视图中可见一秒钟。有人可以帮我解决这个问题。
答案 0 :(得分:2)
SurfaceView无法在ViewFlipper中正常工作,因为它无法正确设置动画。这就是为什么Android 4.0引入了一个名为TextureView的新小部件来解决这个问题。
答案 1 :(得分:0)
这里的主要问题是SurfaceView无法动画。这就是为什么ViewFlipper和Layout Animation都不能应用于SurfaceView。