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 8675881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:03:36+00:00 2026-06-12T20:03:36+00:00

I am looking for an easy way to animate custom views in Android. I

  • 0

I am looking for an easy way to animate custom views in Android. I am trying to avoid using the animator object but want to work with raw threads. What I have done is created a custom view by creating a class that extends android.view.View. I then override the onDraw method and use the canvas to draw a rect. What I would like is the rect to shrink, so I keep a variable that represents the x value of the right hand side of the rectangle. I would then like the the rectangle’s right edge shrink in over time. What I would have liked to do is create a new thread, start it and have it change the value of the rectangle. That all works except the view doesn’t get updated until you call View.invalidate. The problem is that I can’t call that from the thread that I spawned because it is not the UI thread. I read solutions on using Handlers… but I am still unsure if that is the correct solution and then how to use them.

package com.example.practicum;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

public class TimerControl extends View implements Runnable, Handler.Callback
{
    private Paint paint;
    private Rect rect;
    private Thread t;
    private Handler h;

    public TimerControl(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub



        paint = new Paint();
        paint.setColor(Color.BLUE);
        rect = new Rect(0,0,60,60);
        t = new Thread(this);
        t.start();


        h = new Handler(this);
        //h.post(this);
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        canvas.drawRect(rect, paint);
    }

    @Override
    public void run()
    {
        rect.right = rect.right-1;
        while(true)
        {
            rect.right = rect.right-1;
            this.invalidate();
            try
            {
                Thread.sleep(5000);
                h.sendEmptyMessage(0);
            } 
            catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean handleMessage(Message msg)
    {

        return false;
    }

}
  • 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-12T20:03:38+00:00Added an answer on June 12, 2026 at 8:03 pm

    you can define a global Handler (in the UI thread):

    Handler mHandler = new Handler();
    

    then, from your thread, call:

    mHandler.post(new Runnable() {
        public void run() {             
            // call some method on the UI thread
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a easy way to have multiple attachments on one model using
I have been looking for a easy way to add borders to custom made
I'm looking for an easy way that doesn't involve using something like Indy to
I am looking into using High Charts, but I want to populate the data
I am looking for an easy way to check if an object in C#
I'm looking for an easy way to track time I work on a project
Im looking for an easy way to move around a character and rotate it
I'm looking for an easy way in Obj.C to do add a space between
I'm looking for an easy way to generate previews for labels generated as pdfs.
I'm looking for an easy way to get width and height dimensions for image

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.