public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSurfaceView01 = (SurfaceView) findViewById(R.id.mSurfaceView1);
mSurfaceHolder01 = mSurfaceView01.getHolder();
mSurfaceHolder01.addCallback(EX10_04.this);
mButton02 = (Button)findViewById(R.id.buttonObj);
mButton02.setOnClickListener( new Button.OnClickListener(){
public void onClick(View arg0) {
Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
intent.setType("audio/*");
Intent destIntent = Intent.createChooser( intent, "select audio" ); //pick up an audio file
startActivityForResult( destIntent, 0 );
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( resultCode == RESULT_OK )
{
Uri uri = data.getData();
if( uri != null )
{
DrawO();//It will draw a circle
}
else
{
e.printStackTrace();
}
}
The circle not show..... but when i put the DrawO() next to
mSurfaceHolder01.addCallback(EX10_04.this);
It draw a circle !
if i want to draw a circle in the onActivityResult event
what shuld i do ?
or if i can pa开发者_运维问答ss uri to onClick function ?
public void DrawO()
{
Canvas mCanvas01 = mSurfaceHolder01.lockCanvas(null);
mCanvas01.drawColor(getResources().getColor(R.drawable.white));
Paint mPaint01 = new Paint();
mPaint01.setStyle(Paint.Style.FILL);
mPaint01.setColor(getResources().getColor(R.drawable.red));
mPaint01.setStrokeWidth(1.0F);
........
.......
}
1) The circle is not drawn because the surface is not created yet. Try something like this:
boolean needDrawing = false;
public void onResume() {
if (needDrawing) {
Draw0();
needDrawing = false;
}
...
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( resultCode == RESULT_OK )
{
Uri uri = data.getData();
if( uri != null )
{
needDrawing = true;//It asks to draw a circle
}
else
{
e.printStackTrace();
}
}
2) To pass the URI simply declare a field URI and assign it in onActivityResult and in onClick() check if not null, use it and invalidate it
精彩评论