在 android 中动画缩放和裁剪 Imageview
Animate Zoom and Cropout an Imageview in android
谁能帮我制作这个动画。我试过 ScaleAnimation
但一点用都没有
这是动画
答案很长,但我想这就是您想要的。
预览 - https://giphy.com/gifs/S78V44TmVHrXOrrkQH
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>
</style>
<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
<item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
</style>
创建一个动画文件夹并创建两个动画文件
slide_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
以上所有代码将用于两个activity
之间的转换
现在让我们使用 Motion Layout 创建黑色图像的动画。为了首先使用运动布局,我们需要实现 dependency
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
为了在我们的布局中使用 MotionLayout,我们应该制作根布局 MotionLayout(在布局的设计页面上找到组件树并右键单击您的根布局,然后在那里你会看到选项说 'Convert to MotionLayout'。建议这样做。因为它会自动生成 xml 文件夹,其中包含 MotionScene
)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
android:background="#fff"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/iame" />
</androidx.constraintlayout.motion.widget.MotionLayout>
activity_main_scene.xml(必须在'xml'文件夹里面,也就是在'res'文件夹里面。如果不是自动生成的,你必须自己生成)
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:autoTransition="animateToEnd"
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="2000">
<KeyFrameSet>
<KeyAttribute
motion:motionTarget="@+id/imageView"
motion:framePosition="21"
android:scaleX="5"
android:scaleY="5"/>
<KeyAttribute
android:scaleX="10"
android:scaleY="10"
motion:framePosition="40"
motion:motionTarget="@+id/imageView" />
<KeyAttribute
android:scaleX="20"
android:scaleY="20"
motion:framePosition="100"
motion:motionTarget="@+id/imageView" />
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="@+id/start">
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
</ConstraintSet>
</MotionScene>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
public void run() {
// do something...
startActivity(new Intent(MainActivity.this, Second.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
}, 2000);
}}
谁能帮我制作这个动画。我试过 ScaleAnimation
但一点用都没有
这是动画
答案很长,但我想这就是您想要的。
预览 - https://giphy.com/gifs/S78V44TmVHrXOrrkQH
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowAnimationStyle">@style/CustomActivityAnimation</item>
</style>
<style name="CustomActivityAnimation" parent="@android:style/Animation.Activity">
<item name="android:activityOpenEnterAnimation">@anim/slide_in_right</item>
<item name="android:activityOpenExitAnimation">@anim/slide_out_left</item>
</style>
创建一个动画文件夹并创建两个动画文件
slide_in_right.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="100%p" android:toXDelta="0"
android:duration="@android:integer/config_mediumAnimTime"/>
slide_out_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="-100%p"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
以上所有代码将用于两个activity
之间的转换现在让我们使用 Motion Layout 创建黑色图像的动画。为了首先使用运动布局,我们需要实现 dependency
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta6'
为了在我们的布局中使用 MotionLayout,我们应该制作根布局 MotionLayout(在布局的设计页面上找到组件树并右键单击您的根布局,然后在那里你会看到选项说 'Convert to MotionLayout'。建议这样做。因为它会自动生成 xml 文件夹,其中包含 MotionScene
)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/activity_main_scene"
android:background="#fff"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/iame" />
</androidx.constraintlayout.motion.widget.MotionLayout>
activity_main_scene.xml(必须在'xml'文件夹里面,也就是在'res'文件夹里面。如果不是自动生成的,你必须自己生成)
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:autoTransition="animateToEnd"
motion:constraintSetEnd="@+id/end"
motion:constraintSetStart="@id/start"
motion:duration="2000">
<KeyFrameSet>
<KeyAttribute
motion:motionTarget="@+id/imageView"
motion:framePosition="21"
android:scaleX="5"
android:scaleY="5"/>
<KeyAttribute
android:scaleX="10"
android:scaleY="10"
motion:framePosition="40"
motion:motionTarget="@+id/imageView" />
<KeyAttribute
android:scaleX="20"
android:scaleY="20"
motion:framePosition="100"
motion:motionTarget="@+id/imageView" />
</KeyFrameSet>
</Transition>
<ConstraintSet android:id="@+id/start">
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
</ConstraintSet>
</MotionScene>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Handler().postDelayed(new Runnable() {
public void run() {
// do something...
startActivity(new Intent(MainActivity.this, Second.class));
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
}
}, 2000);
}}