如何在自定义视图中为 Drawable 制作动画?

问题描述 投票:0回答:3

我目前有一个带有可绘制资源的自定义视图,我想将其从一个位置移动到另一个位置。我知道有两种方法可以实现

PropertyAnimation
,您可以使用
ValueAnimation
ObjectAnimation
来实现。但是对于这两者,我在 Google 文档上没有找到任何有关如何将它们与 Drawable 而不是 View 一起使用的信息。 这是我发现的唯一与我的问题类似的示例,因此我尝试在我的代码中实现它:

主要活动

public class MainActivity extends AppCompatActivity { private Button animate; private CustomView mView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mView = new CustomView(MainActivity.this); animate = (Button) findViewById(R.id.animate); animate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mView.startAnimation(); } }); } }

自定义视图

public class CustomView extends View { private Drawable drawable; private Rect drawableRect; private int newPos; public CustomView(Context context) { super(context); drawable = ContextCompat.getDrawable(context, R.drawable.my_drawable); drawableRect= new Rect(); newPos = 0; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ... } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawableRect.left = 0; drawableRect.top = newPos; drawableRect.right = drawable.getIntrinsicWidth(); drawableRect.bottom = newPos + drawable.getIntrinsicHeight(); drawable.setBounds(drawableRect); drawable.draw(canvas); } public void startAnimation() { ValueAnimator animator = ValueAnimator.ofFloat(0, 200); animator.setDuration(1500); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { newPos = ((Float) valueAnimator.getAnimatedValue()).intValue(); CustomView.this.invalidate(); } }); animator.start(); invalidate(); } }

但是没有效果。

newPos

 的值正确更改(使用日志检查)并且屏幕更新(使用表面更新检查),但 Drawable 不会移动。我究竟做错了什么?希望有任何帮助。

android animation android-animation android-custom-view android-drawable
3个回答
3
投票
我尝试运行你的代码,

CustomView

,它很高效,它有动画。

您确定执行该方法吗

startAnimation

我将

startAnimation()

添加到构造中
NoteCustomView
,当UI加载完成时我可以看到动画。

下面的代码,我使用 ic_launcher 作为可绘制的。我将 NoteCustomView 添加到活动的布局 xml 中。

public class NoteCustomView extends View { private Drawable drawable; private Rect drawableRect; private int newPos; public NoteCustomView(Context context) { this(context, null); } public NoteCustomView(final Context context, @Nullable final AttributeSet attrs) { super(context, attrs); drawable = ContextCompat.getDrawable(context, R.mipmap.ic_launcher_round); drawableRect= new Rect(); newPos = 0; startAnimation(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawableRect.left = 0; drawableRect.top = newPos; drawableRect.right = drawable.getIntrinsicWidth(); drawableRect.bottom = newPos + drawable.getIntrinsicHeight(); drawable.setBounds(drawableRect); drawable.draw(canvas); } public void startAnimation() { ValueAnimator animator = ValueAnimator.ofFloat(0, 200); animator.setDuration(1500); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { newPos = ((Float) valueAnimator.getAnimatedValue()).intValue(); NoteCustomView.this.invalidate(); } }); animator.start(); invalidate(); } }
    

1
投票

周恩旭最终带我找到了解决方案。我在 MainActivity

 中犯了一个愚蠢的错误,因为我正在创建对 
CustomView
:
的新引用

mView = new CustomView(MainActivity.this);

并尝试使用该对象引用来制作动画。通过将其替换为以下内容解决了这个问题:

mView = (CustomView) findViewById(R.id.custom_view);
    

0
投票
视图尚未在布局中添加/更新。创建了一个 CustomView 对象,但它没有放在屏幕上。

frameParentLayout = (FrameLayout) findViewById(R.id.parent_view); mView = new CustomView(MainActivity.this); frameParentLayout.addView(mView);
    
© www.soinside.com 2019 - 2024. All rights reserved.