gpt4 book ai didi

android - 为什么在 Android 中应用 ScaleAnimation 时 drawable 的位置也会发生变化?

转载 作者:行者123 更新时间:2023-11-29 16:18:41 25 4
gpt4 key购买 nike

我正在尝试将缩放动画应用到矩形,使用代码而不是 xml。我想让矩形变高,很简单。

RectDrawableView - 创建一个 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 :- 创建一个新的 RectDrawableView 并添加到布局中。 OnResume 会触发动画,这会使矩形变高。

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

}
}

基本.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>

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

当我使用完全相同的动画代码,但将它应用于 TextView 而不是 ShapeDrawable 时,一切都很好。

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

最佳答案

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

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

关于android - 为什么在 Android 中应用 ScaleAnimation 时 drawable 的位置也会发生变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com