想要实现一个仿截屏动画,然后截屏图片飞到相册按钮后消失,如动画所示:
myrecording
找了很长时间,也没找到现成的例子,于是拼凑了两种别人实现的动画实现。
首先是截屏动画,参考链接如下:
添加链接描述
第二部分,缩放到指定位置消失,参考链接如下:
添加链接描述
详细代码如下:
public class MainActivity extends AppCompatActivity {
private ImageView ivScreenshots;
private CardView mCardView;
private Button btnScreenshots;
// 截图动画
private Animation mCaptureAnimation;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCardView = findViewById(R.id.cardView);
ivScreenshots = findViewById(R.id.iv_capture);
btnScreenshots = findViewById(R.id.btn_album);
mCaptureAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_animation);
mCaptureAnimation.setFillAfter(true);
mCaptureAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mHandler.postDelayed(mCaptureViewDisappearRunnable, 50);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
Button btnScreenshots = findViewById(R.id.btn_screenshot);
btnScreenshots.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
screenCapture();
}
});
}
private Runnable mCaptureViewDisappearRunnable = new Runnable() {
@Override
public void run() {
mCardView.clearAnimation();
mCardView.setVisibility(View.INVISIBLE);
scaleDownAndTransitionAnimation(mCardView, btnScreenshots);
}
};
private void screenCapture() {
View decorView = getWindow().getDecorView();
Bitmap screenBitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(screenBitmap);
decorView.draw(canvas);
updateImageCapture(screenBitmap);
}
private void updateImageCapture(final Bitmap screenBitmap) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mCardView.setVisibility(View.VISIBLE);
ivScreenshots.setImageBitmap(screenBitmap);
}
});
mCardView.startAnimation(mCaptureAnimation);
}
private void scaleDownAndTransitionAnimation(final View startView, View finishView) {
int startViewLocation[] = new int[2];
startView.getLocationInWindow(startViewLocation);
int finishViewLocation[] = new int[2];
finishView.getLocationInWindow(finishViewLocation);
int startX = startViewLocation[0] + startView.getWidth() / 2;
int startY = startViewLocation[1] + startView.getHeight() / 2;
int endX = finishViewLocation[0] + finishView.getWidth() / 2;
int endY = finishViewLocation[1] + finishView.getHeight() / 2;
ScaleAnimation animation = new ScaleAnimation(1f, 0f, 1, 0f,
Animation.ABSOLUTE, endX - startX + startView.getWidth() / 2,
Animation.ABSOLUTE, endY - startY + startView.getHeight() / 2);
animation.setDuration(500);
final ImageView tempMoveView = new ImageView(this);
tempMoveView.setScaleType(ImageView.ScaleType.FIT_XY);
Bitmap tempBm = getBitmapFromView(startView);
tempMoveView.setImageBitmap(tempBm);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(startView.getMeasuredWidth(), startView.getMeasuredHeight());
params.setMargins((int) startView.getX(), (int) startView.getY(), (int) (startView.getX()+startView.getMeasuredWidth()), (int) (startView.getY()+startView.getMeasuredHeight()));
tempMoveView.setLayoutParams(params);
final FrameLayout frameLayout = (FrameLayout) this.getWindow().getDecorView().getRootView();
frameLayout.addView(tempMoveView);
tempMoveView.startAnimation(animation);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
startView.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
frameLayout.removeView(tempMoveView);
}
});
}
public static Bitmap getBitmapFromView(View view) {
// 创建与该view相同大小的空Bitmap
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
// 使用Canvas进行绘制
Canvas canvas = new Canvas(bitmap);
// 将view绘制在bitmap上
view.draw(canvas);
return bitmap;
}
}
scale_animation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="500"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.9"
android:toYScale="0.9"/>
<alpha
android:duration="200"
android:fromAlpha="0.5"
android:toAlpha="1.0"/>
</set>
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_screenshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="60dp"
android:text="截图"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="@+id/btn_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="60dp"
android:text="相册"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:visibility="gone"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:id="@+id/iv_capture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"/>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>