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

  • Home
  • SEARCH
  • 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 6867929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:22:13+00:00 2026-05-27T03:22:13+00:00

I am trying to apply a scale animation to a rectangle, using code not

  • 0

I am trying to apply a scale animation to a rectangle, using code not xml. I want to make the rectangle grow taller, pretty simple.

RectDrawableView – creates a RectShape ShapeDrawable

public class RectDrawableView extends View {

Paint paint; 

public RectDrawableView(Context context) {
    super(context);
    paint = new Paint();
    paint.setColor(Color.parseColor("#aacfcce4"));
    paint.setStyle(Paint.Style.FILL);
}

protected void onDraw(Canvas canvas) {

    int x = 35;
    int y = 250;
    int width = 50;
    int height = 300;

    ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());

    Rect rect = new Rect(x, y, x+width, y+height);
    shapeDrawable.setBounds(rect);
    shapeDrawable.getPaint().set(paint);
    shapeDrawable.draw(canvas);
}

}

Activity: – Creates a new RectDrawableView and adds to layout. OnResume an animation is triggered, which should make the rectangle grow taller.

public class RectActivity extends Activity {

final String TAG = "RectActivity";

TextView rect1;
RectDrawableView rectView;


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

    setContentView(R.layout.basic);

    LinearLayout linear = (LinearLayout) findViewById(R.id.basicLayout);
    rectView = new RectDrawableView(this);
    linear.addView(rectView);
}

@Override
public void onResume() {
    super.onResume();
    RunAnimations();
}


private void RunAnimations() {


    Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, ScaleAnimation.RELATIVE_TO_SELF, 0,
            ScaleAnimation.RELATIVE_TO_SELF, 1);
    rectAnim1.setFillBefore(false);
    rectAnim1.setFillAfter(true);
    rectAnim1.setStartOffset(500);
    rectAnim1.setDuration(500);

    rectView.startAnimation(rectAnim1);

}
}

basic.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/basicLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

</LinearLayout>

Result: The rect is growing taller, good, but the location of the rect is also changing, bad. The whole rectangle is being positioned higher up the screen.

When I use this exact same animation code, but apply it to a TextView instead of a ShapeDrawable, all is good.

I’ve trawled through all the related articles on SO, but I’m still struggling with this one. Any help would be appreciated, thanks.

  • 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-05-27T03:22:14+00:00Added an answer on May 27, 2026 at 3:22 am

    Complete code, i had changed the height of the rectangle.

    public class RectActivity extends Activity {
    
    final String TAG = "RectActivity";
    
    TextView rect1;
    RectDrawableView rectView;
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.basic);
    
        LinearLayout linear = (LinearLayout) findViewById(R.id.rectangle);
        rectView = new RectDrawableView(this);
        linear.addView(rectView);
        Button sbutton = (Button)findViewById(R.id.sbutton);
        sbutton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                RectActivity.this.RunAnimations();
            }   
        });
        Button tbutton = (Button)findViewById(R.id.tbutton);
        tbutton.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                RectActivity.this.runTranslateAnimation();
            }   
        });
    
    }
    
    @Override
    public void onResume() {
        super.onResume();
        RunAnimations();
    }
    
    
    private void RunAnimations() {
        Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, 
                       ScaleAnimation.RELATIVE_TO_SELF, 0,
                ScaleAnimation.ABSOLUTE, 250);
    
        rectAnim1.setFillBefore(false);
        rectAnim1.setFillAfter(true);
        rectAnim1.setStartOffset(500);
        rectAnim1.setDuration(500);
        rectView.startAnimation(rectAnim1);
    
    }
    private void runTranslateAnimation() {
    
        float x = rectView.getX();
        float y = rectView.getY();
    
        TranslateAnimation trans = new TranslateAnimation(0, 0, 0, (90));
        trans.setFillAfter(true);
        trans.setStartOffset(500);
        trans.setDuration(500);
        rectView.startAnimation(trans);
    }
    }
    
    public class RectDrawableView extends View {
    
    Paint paint; 
    Rect rect;
    
    public RectDrawableView(Context context) {
        super(context);
        paint = new Paint();
        paint.setColor(Color.parseColor("#aacfcce4"));
        paint.setStyle(Paint.Style.FILL);
    }
    
    protected void onDraw(Canvas canvas) {
    
        int x = 50;
        int y = 150;
        int width = 50;
        int height = 100;
    
        ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
    
        rect = new Rect(x, y, x+width, y+height);
        shapeDrawable.setBounds(rect);
        shapeDrawable.getPaint().set(paint);
        shapeDrawable.draw(canvas);
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to apply some transformations on images using a CGContextRef. I am
I am trying to apply a method to an existing object which involves using
I'm trying to write a simple PDF viewer using CGPDFDocument , based on QuartzDemo.
I'm using Scala 2.8.0 and trying to read pipe delimited file like in code
I am trying to apply Mike Chen's answer here , using SDK 3.0. In
I'm trying to make a simple 2D editor with the following capabilities: Create/delete rectangles,
i am trying to scale and rotate a CCSprite using the following methods [rectangleSprite
Im trying to apply different background color to even and odd items in a
I am trying to apply styles to HTML tags dynamically by reading in the
I'm trying to apply a transform to a 3D object in a STL File

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.