Here is a short program where my intent is to scale up or down the
size of a circle when the zoom controls are used. In addition to my
intended behavior, I am getting two "extra" (unintended) calls to the
onDraw() function in response to my zoomControl onClick callback.  (I
have used DDMS to log my debug statements.)

Can anyone explain the source of the onDraw() calls besides the
zoomControl onClick() method?

---------------------------------------------------------------------------------------
package com.example.simpledraw;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.ZoomControls;

public class SimpleDraw extends Activity {

        private static final String TAG = "SimpleDrawActivity";
        public static double scale;

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

        scale = 2.0;
        RelativeLayout relativeLayout = new RelativeLayout
(this);
        setContentView(relativeLayout);
        final MyView myView = new MyView(this);
        RelativeLayout.LayoutParams myViewLayoutParams =
                new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.FILL_PARENT,RelativeLayout.LayoutParams.FILL_PARENT
 );
        relativeLayout.addView(myView, myViewLayoutParams);
        RelativeLayout.LayoutParams zoomControlsLayoutParams =
                new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT
 );
        zoomControlsLayoutParams.addRule
(RelativeLayout.ALIGN_PARENT_BOTTOM);
        zoomControlsLayoutParams.addRule
(RelativeLayout.CENTER_HORIZONTAL);
        ZoomControls zoomControls = new ZoomControls(this);

        relativeLayout.addView
(zoomControls,zoomControlsLayoutParams);
        myView.setEnabled(true);
        zoomControls.setIsZoomInEnabled(true);
        zoomControls.setIsZoomOutEnabled(true);
        zoomControls.setOnZoomInClickListener(new OnClickListener()
{
            public void onClick(View v) {
                myView.invalidate();
                scale *= 1.25;
                Log.v(TAG, "The scale is  " + scale);
            }
        });
        zoomControls.setOnZoomOutClickListener(new OnClickListener()
{
            public void onClick(View v) {
                myView.invalidate();
                scale *= 0.8;
                Log.v(TAG, "The scale is  " + scale);
            }
        });
    }

    static public class MyView extends View {

        Integer number = 0;

        public MyView(Context context) {
                super(context);
        }
        @Override
        protected void onDraw(Canvas canvas){
                //super.onDraw(canvas);
                Log.w(TAG, "The onDraw() method is called for the " + number + "
time.");
                //Log.w(TAG, "The view calling OnDraw is " + this.getId() );
                canvas.drawColor(0);
                Path circle = new Path();
                circle.addCircle(150, 150, (int)(scale*10.), Direction.CW);
                Paint cPaint = new Paint();
                cPaint.setColor(Color.LTGRAY);
                canvas.drawPath(circle, cPaint);
                Paint textPaint = new Paint();
                textPaint.setColor(Color.GREEN);
                canvas.drawText(number.toString(), 150, 250, textPaint);
                number++;

        }
    }


}

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to