我正在尝试使用代码而非XML将秤动画应用于矩形。我想让矩形变得更高,非常简单。

RectDrawableView-创建一个矩形形状

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

}

活动: - 创建一个新的RectDrawableView并添加到布局中。触发动画的元素,这应该使矩形变得更高。

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>

结果:矩形越来越高,好,但是一个高的位置也在变化,不好。整个矩形都位于屏幕上。

当我使用此完全相同的动画代码时,但是将其应用于文本视图而不是可求的,一切都很好。

我已经浏览了所有相关文章,但是我仍在为此而苦苦挣扎。任何帮助将不胜感激,谢谢。

有帮助吗?

解决方案

完整的代码,我更改了矩形的高度。

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

其他提示

尝试此代码。保持 pivotYType 绝对具有价值 550 ( y + height )

 Animation rectAnim1 = new ScaleAnimation(1, 1, 1, 1.3f, 
                                ScaleAnimation.RELATIVE_TO_SELF, 0,
                                ScaleAnimation.ABSOLUTE, 550);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top