android - 为什么 setPivotX() 只是替换视图?
问题描述
在代码中 - 在视图的动画枢轴的第一部分发生变化并且......视图位置也是如此!(这是奇怪的行为)
这是代码(规定 - 一个ValueAnimator):
ValueAnimator animator = ValueAnimator.ofFloat(0,180);
float firstAnimLineX = ((47.5f * Resources.getSystem().getDisplayMetrics().density));
float firstAnimLineY = ((2.5f * Resources.getSystem().getDisplayMetrics().density));
float secondAnimLineX = ((47.5f * Resources.getSystem().getDisplayMetrics().density));
float secondAnimLineY = ((47.5f * Resources.getSystem().getDisplayMetrics().density));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
view.setPivotX((Float) animator.getAnimatedValue()>180/2?firstAnimLineX : secondAnimLineX);
view.setPivotY((Float) animator.getAnimatedValue()>180/2?firstAnimLineY : secondAnimLineY);
view.setRotation((Float) animator.getAnimatedValue());
}
});
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/frameLayout">
<ImageView
android:layout_width="50dp"
android:layout_height="5dp"
android:layout_gravity="center_horizontal"
android:id="@+id/line"
/>
</FrameLayout>
</RelativeLayout>
这就是我想要做的(左边部分;右边部分 - 这是真正发生的事情)(黄色点 - 枢轴): 我看了 setPivotX 的源代码,但它什么也没说。也许我应该打电话给有无效观点的人?
解决方案
我重新创建了这段代码并在我的 Android 手机上运行它。它提供了您正在寻找的确切动画。
我创建了一个图像来匹配您的“火柴棍”图像。例如,我将其设置为 400 px 宽 x 40 高。
我给了它 id: 'bar'
ImageView bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar = findViewById(R.id.bar);
bar.getLayoutParams().width = 400; // I set the dimensions here.
bar.getLayoutParams().height = 40; // I set the dimensions here.
bar.setX(0); bar.setY(0); // Then I positioned it in upper left corner.
bar.setPivotX(380); // I set the pivot a little inside the image so that it looks kinda like it is pivoting on a nail.
bar.setPivotY(20);
bar.animate().rotation(-90).setDuration(2000); // Here it animates from your first image above, to your second image in 2 seconds.
// Here I used a postDelay to allow the first animation to finish its' 2 seconds 'before' calling the second animation. No need for an animate() - listener now.
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
bar.setY(360); // This re-positions the original image to the
// location where the animation ended.
bar.setRotation(90); // Because the original image was horizontal, this turns it vertical.
// Now we finish the animation to your 3rd image above.
bar.animate().rotation(0).setDuration(2000);
}
},2100);
}
推荐阅读
- angular - 在 ng-packagr 构建中包含 json 文件
- php - PHP 500(内部服务器错误) - 遇到非数字值?
- php - Typo3:在没有 Repo 的情况下获取 sys_file_metadata.title
- android - 使用Zxing库生成二维码
- node.js - 通过远程 http-client 测试 Node Express API 路由
- cakephp - CakePHP 3 - 依靠深度关联
- firebase - 如何将 Firebase 数据导入欧盟的 Bigquery 数据集?
- google-cloud-platform - 有什么方法可以为 Google Cloud 的网络流量设置配额?
- sql-server - SQL DB Stacking 中的单个数据库开始
- android - 什么是 android-sdk 和 android-auth libs 开源许可证?