Reputation: 1
In Eclipse IDE for Android I am fighting with one simple problem right now. I am using code like this:
public class Reader extends Activity
{
// declarations
private class GraphView extends View
{
protected void onDraw(Canvas canvas)
{
synchronized(this)
{
final Paint paint = mPaint;
// some drawings
myvoid(canvas,paint);
// some drawings
}
}
}
void myvoid(Canvas c,Paint p)
{
int i=0;
do //or for cycle
{
i++;
c.drawText(Integer.toString(i),0,100,p); // <<<<<<<<<<<<<<<< Problem
}
while (i<100000);
}
}
The canvas and text is not updating,until the while loop is finished. It seems,that the canvas is "locked" during that time. How can I achieve,that I can see the progress inside of the loop on the canvas?
Thanks,Tomas.
Upvotes: 0
Views: 3748
Reputation: 16537
The problem is that you're not giving any time for Android to present the results of the drawing method. You should draw one step of the loop and call invalidate to trigger next step. Android will draw your text on the screen and then it will try to redraw it with increased counter once again. Please compare your code and my version:
private class GraphView extends View {
protected void onDraw(Canvas canvas) { // some drawings
myvoid(canvas, paint);
// some drawings
}
void myvoid(Canvas c, Paint p) {
int i = 0;
do // or for cycle
{
i++;
c.drawText(Integer.toString(i), 0, 100, p);
} while (i < 100000);
}
}
My version with invalidate():
private class GraphView extends View {
int i = 0;
Paint paint = new Paint();
@Override
public void draw(Canvas canvas) {
canvas.drawText(Integer.toString(i), 0, 100, paint);
i++;
invalidate();
super.draw(canvas);
}
}
Upvotes: 1
Reputation: 1920
Follow this steps :
this example maybe helps : https://github.com/hamilton-lima/Bolinha/tree/master/src/com/example/bolinha
Upvotes: 0
Reputation: 4204
I think you can try this
imageView = (ImageView) this.findViewById(R.id.imageView1);
Display currentDisplay = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
//float dw = currentDisplay.getWidth();
// float dh = currentDisplay.getHeight();
bitmap = Bitmap.createBitmap((int) dw, (int) dh,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.RED);
imageView.setImageBitmap(bitmap);
You can try to make your canvas a bitmap image then set bitmap to your imageview
Upvotes: 0