Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:48:30+00:00 2026-06-15T01:48:30+00:00

I have a app that allows users to drag shapes on the screen. I

  • 0

I have a app that allows users to drag shapes on the screen. I use a custom surface view for the dragging. Every time the surface view gets a OnTouchEvent, it changes the postion of the graphic image and calls OnDraw to update the screen.

So the bitmap image does not get drawn until a touchevent happens. I tried to invalidate the Costume surface view, but according to the debugger, OnDraw never gets called.

So right now my screen is blank until some one touches the screen

// code

public class cPlay extends  cBase  implements OnClickListener {
    /** Called when the activity is first created. */
    // sound
    private SoundPool soundPool;
    private int soundID;
    boolean loaded = false;  

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
      //      setContentView(R.layout.play);


            int w=getWindowManager().getDefaultDisplay().getWidth();
            int h=getWindowManager().getDefaultDisplay().getHeight();

            BallView ballView=new BallView(this,w,h);
            setContentView(ballView);

            // Does not update screen??????
            ballView.invalidate();
                 ....
...
....} // end of class

public class BallView  extends SurfaceView implements SurfaceHolder.Callback {
    private Bitmap bitmap ;
    private int x=20,y=20;int width,height;
    cShapeParent MyShapes;   
    cTarget MyTargetShapes;
    Context context;
    int counter=0;   

    // sound
    private SoundPool soundPool;
    private int soundID;
    boolean loaded = false;

    public BallView(Context InContext,int w,int h) {
        super(InContext);

        context=InContext;
        int counter=0;
        int Page=0;
        width=w;
        height=h;
        getHolder().addCallback(this);
        setFocusable(true);
        MyShapes=new cShapeParent(context,w,h);
        MyTargetShapes= new cTarget(context,w,h);
        MyShapes.SetTargets(  MyTargetShapes );
        // load in bitmaps



    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);




        bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.ball_green);
        canvas.drawColor(Color.BLUE);//To make background 
  //      canvas.drawBitmap(bitmap,x-(bitmap.getWidth()/2),y-(bitmap.getHeight()/2),null);

        // See if we shoyld drag it
        for(int i=MyShapes.Amount-1; i>=0; i--)
        {
          if (MyShapes.Intersect(i,x,y))        
          {
             // see if we can drag it
              if ( MyShapes.getDragFlag(i)==false)
                 break;

              // yes drag this image
              MyShapes.SetCenter(i, x, y);

              // if the target was found
              // make it so we can no loner drag it
              if (MyShapes.CheckTarget(i))
              {
                  MyShapes.SetDrag(i,false);
                  MyShapes.SetToTarget(i);
                  if (++counter>2)
                      DisplayNextScreen();
              }
              break;
          } 
        }     

        // Draw target shapes
        // NOTE: draw these first so they are on the bottom
        for(int i=0; i<MyShapes.Amount;i++)
        {
            int x=MyTargetShapes.GetX(i);
            int y=MyTargetShapes.GetY(i);
            int w=MyTargetShapes.GetW(i);
            int h=MyTargetShapes.GetH(i);

            Rect br=new Rect( x, y,w+x,  h+y);
            Bitmap m= MyTargetShapes.MyImages[i];
            canvas.drawBitmap(m, null, br, null);
        }      

        // Draw shapes that can be dragged
        for(int i=0; i<MyShapes.Amount;i++)
        {
            int x=MyShapes.GetX(i);
            int y=MyShapes.GetY(i);
            int w=MyShapes.GetW(i);
            int h=MyShapes.GetH(i);
            Rect br=new Rect( x, y, x+w,y+h);
            Bitmap m= MyShapes.MyImages[i];
            canvas.drawBitmap(m, null, br, null);
        }




    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        x=(int)event.getX();
        y=(int)event.getY();

        if(x<25)
            x=25;
        if(x> width)   
            x=width;
        if(y <25)
            y=25;
        if(y > height)
            y=height;

        updateBall();

        return true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    private void updateBall() {
        Canvas canvas = null;
        try {
            canvas = getHolder().lockCanvas(null);
            synchronized (getHolder()) {
                this.onDraw(canvas);
            }
        }
        finally {
            if (canvas != null) {
                getHolder().unlockCanvasAndPost(canvas);
            }
        }
    }   

  void  DisplayNextScreen()
    {
           /////////////////////////////////////
        // call up dialog
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog

        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                MyTargetShapes.SetUp(1);
                MyShapes.SetUp(1);
                MyShapes.SetTargets(  MyTargetShapes );
                counter =0;
            }
        });

        dialog.show();
    } // end methed


  void StartSound(int soundID )
  {
       AudioManager audioManager = (AudioManager) context.getSystemService("audio");
        float actualVolume = (float) audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        float volume = actualVolume / maxVolume;
        // Is the sound loaded already?

            soundPool.play(soundID, volume, volume, 1, 0, 1f);
        //  Log.e("Test", "Playe");

  } 

} // end class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:48:31+00:00Added an answer on June 15, 2026 at 1:48 am

    I faced a similar issue in my app. invalidate() does not redraw the shape. So the solution which i found was to extract the shape into the class displaying the components on UI and then to update the component from the main activity by accessing it from the UI class.

    SurfaceView Class:

    @Override
    public void onDraw(Canvas canvas) {

        Log.d("check","="+color);
        Log.e("check","after set:"+buf);
        Log.i("new view ", " on draw ");
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        paint.setColor(Color.WHITE);
    
        path = new Path();
        path.moveTo(0, 30);                             /*For Borders*/
        path.lineTo(30, 0);
        path.lineTo(-30, 0);
        path.close();
        path.offset(20, -5);
        canvas.drawPath(path, paint);
    

    UI class:

    public void show(View anchor,int color) {
    preShow();

        int xPos, yPos, arrowPos;
    
        mDidAction = false;
        }
    
        showArrow(((onTop) ? R.id.arrow_down : R.id.arrow_up), arrowPos,color);
    
        setAnimationStyle(screenWidth, anchorRect.centerX(), onTop);
        mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);
    
    
    }
    

    R.id.arrow_down is the shape drawn using the NewView class.

    In the main activity on the event call the function like this:

        btn1 = (Button) this.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
    
    
                Log.d("In","View");
                quickAction.show(v,Color.BLUE);
    
    
            }
        });
    

    This will update the view as required.
    Hope This Helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that allows a user to drag views onto the screen
I have an app that allows users to upload large data files and then
I have an iPhone app that allows users to record videos and I'd like
I have a topics app that allows users to create topics. For each individual
We have a WPF app that allows our users to download encrypted content and
We have an app that allows users to send e-mails from our system. It
I have a Rails app that allows users to build up a network structure
I have a rails 3 app that allows users to share files by uploading
I have an app that allows users to generate objects, and store them (in
I have an app that allows users to enter, edit and search for projects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.