如何用Whatsapp等动画放大图像

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

我想要像 WhatsApp 动画一样打开图像。在我的例子中,我有图像列表。当我点击列表中的特定图像时,我想用动画放大图像,例如 Whatsapp 在该聊天列表中打开 profilePhoto

我使用警报对话框打开放大图像。并添加动画。但我无法像whatsapp那样控制效果。

最终的 AlertDialog.Builder 构建器;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder = new AlertDialog.Builder(Objects.requireNonNull(context), android.R.style.Theme_Material_Light_Dialog_NoActionBar);
    } else {
        builder = new AlertDialog.Builder(Objects.requireNonNull(context));
    }

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View dialogLayout = inflater.inflate(R.layout.profile_layout, null);

    ImageView ivProfile = dialogLayout.findViewById(R.id.ivProfileImage);

    ivProfile.getLayoutParams().height = (int) (width / 1.2);
    ivProfile.getLayoutParams().width = (int) (width / 1.2);

    Glide.with(context).load(url)
            .apply(RequestOptions.centerInsideTransform().diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.drawable.ic_profile_fill)).into(ivProfile);

    builder.setView(dialogLayout);

    AlertDialog alertDialog = builder.create();

Objects.requireNonNull(alertDialog.getWindow()).setWindowAnimations(R.style.ProfileImageAnimation);

    alertDialog.setCancelable(true);
    alertDialog.show();

//动画文件:

<!--<translate
    android:fromXDelta="-100%p"
    android:fromYDelta="0"
    android:duration="500"/>-->

<scale
    android:fromXScale="0.3"
    android:fromYScale="0.3"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="150"
    android:toXScale="1.0"
    android:toYScale="1.0" />

android animation android-alertdialog
1个回答
0
投票

你可以试试这个:

类:AnimationDetailActivity

class AnimationActivity : AppCompatActivity() {
        
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_animation_start)
        
                showAnimationButton.setOnClickListener { showAnimation() }
            }
        
            private fun showAnimation() {
                val activityOptionsCompat =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(this, showAnimationButton, "TRANSITION_NAME")
                intent = Intent(this, AnimationDetailActivity::class.java)
                startActivity(intent, activityOptionsCompat.toBundle())
            }
        
        }
                 

布局:activity_animation_start

<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:context=".AnimationActivity">
        
            <ImageView
                    android:transitionName="TRANSITION_NAME"
                    android:id="@+id/showAnimationButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/ic_launcher"/>
        
        </RelativeLayout>

                 

类:AnimationDetailActivity

class AnimationDetailActivity : AppCompatActivity() {
        
            override fun onCreate(savedInstanceState: Bundle?) {
                super.onCreate(savedInstanceState)
                setContentView(R.layout.activity_animation_detail)
            }
        }

                            

布局:活动_动画_细节

<?xml version="1.0" encoding="utf-8"?>
        <android.support.constraint.ConstraintLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent" 
                android:layout_height="match_parent">
        
            <ImageView android:layout_width="300dp"
                       android:layout_height="300dp"
                       android:transitionName="TRANSITION_NAME"
                       android:scaleType="fitCenter"
                       android:background="@mipmap/ic_launcher"
                       app:layout_constraintTop_toTopOf="parent"
                       app:layout_constraintBottom_toBottomOf="parent"
                       app:layout_constraintStart_toStartOf="parent"
                       app:layout_constraintEnd_toEndOf="parent"/>
        </android.support.constraint.ConstraintLayout>

注意:确保两个布局中的名称转换名称保持相同。

© www.soinside.com 2019 - 2024. All rights reserved.