Android动画:如何自动触发并连续进行缩放

时间:2015-10-02 19:00:43

标签: android android-animation

我正在学习Android中的动画,并且有以下代码(基于Android-Developer教程),当我点击ImageButton时它会起作用。点击后,它会放大,然后在下一次点击时,它会缩小。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.container_layout);

    final View thumb1View = findViewById(R.id.thumb_button_1);
    thumb1View.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            zoomImageFromThumb(thumb1View, R.mipmap.ic_launcher);
        }
    });

    mShortAnimationDuration = getResources().getInteger(android.R.integer.config_longAnimTime);
}

这就是布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:padding="16dp">

        <ImageButton
            android:id="@+id/thumb_button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@mipmap/ic_launcher"
            android:scaleType="centerCrop"
            android:layout_gravity="center"
            android:background="@null"
            android:contentDescription="@string/description_image_1" />

    </LinearLayout>


    <!-- This initially-hidden ImageView will hold the expanded/zoomed version of
         the images above. Without transformations applied, it takes up the entire
         screen. To achieve the "zoom" animation, this view's bounds are animated
         from the bounds of the thumbnail button above, to its final laid-out
         bounds.
         -->

    <ImageView
        android:id="@+id/expanded_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="100dp"
        android:visibility="invisible"
        android:contentDescription="@string/description_zoom_touch_close" />
</FrameLayout>

但是,我试图让它在加载活动后继续放大/缩小而不需要点击激活它,然后点击停止放大。

非常感谢,

1 个答案:

答案 0 :(得分:1)

我通过在res / anim目录中使用动画xml来解决这个问题。在res / anim目录中添加名为my_alpha_animation.xml的新xml,如果anim不存在,则创建它:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXScale="0.1"
    android:fromYScale="0.1"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0"
    android:repeatCount="infinite"
    android:repeatMode="reverse">
</scale>

并将其加载到活动onResume():

@Override
protected void onResume(){
    super.onResume();
    doAlpha(btnAlpha);
}

以下是执行此操作的方法:

private void doAlpha(View v){
    Animation alphaAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.my_alpha_animation);
    image.startAnimation(alphaAnimation); //image is my ImageView
}

上面,“image”是我的ImageView,我从我的mipmap目录中设置了名为“scanweb”的图像,并清除了我的Activity onCreate()中可能正在进行的任何动画:

image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.mipmap.scanweb);
image.clearAnimation();

最终结果是,一旦创建了Activity,我的图像“scanweb”就会无限地开始脉动(放大/缩小)。