Question

Je suis en train d'appliquer une animation à grande échelle à un rectangle, en utilisant le code xml non. Je veux faire GROW rectangle plus grand, assez simple.

RectDrawableView - crée un ShapeDrawable RectShape

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);
}

}

Activité: - Crée une nouvelle RectDrawableView et ajoute à la mise en page. OnResume une animation est déclenchée, ce qui devrait faire le plus grand rectangle de culture.

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>

Résultat: Le rect est de plus en plus grand, bon, mais l'emplacement du rect est en train de changer, mauvais. Le rectangle entier est placé plus haut de l'écran.

Quand j'utilise ce même code d'animation exacte, mais l'appliquer à un TextView au lieu d'un ShapeDrawable, tout est bon.

J'ai chalutée à travers tous les articles connexes sur, mais je suis encore aux prises avec celui-ci. Toute aide serait appréciée, merci.

Était-ce utile?

La solution

Code complet, j'avais changé la hauteur du 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);
}
}

Autres conseils

Essayez ce code. Gardez le pivotYType comme ABSOLUTE avec la valeur 550 ( y + height )

 Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, 
                                ScaleAnimation.RELATIVE_TO_SELF, 0,
                                ScaleAnimation.ABSOLUTE, 550);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top