我想淡出ImageView

时间:2019-06-23 09:07:25

标签: android kotlin

我想通过点击淡出ImageView。我尝试的方式是否正确,或者有人可以建议我如何做得更好? 我现在有 添加“ private”,“ new”,“ Animation”,“ fadeOutAndHideImage”,“ fadeOut”,“ public”均为红色。我做错了什么?我每次都在代码方面遇到问题,无法完成简单的应用程序。

package com.example.fairytale_quest

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import android.view.animation.AlphaAnimation
import android.view.animation.Animation
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main5.*

class Main5Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main5)
        val actionBar = supportActionBar

        actionBar!!.hide()
       var listener = OnTouchListener(function = { view, motionEvent ->

            if (motionEvent.action == MotionEvent.ACTION_MOVE) {

                view.y = motionEvent.rawY - view.height / 2
                view.x = motionEvent.rawX - view.width / 2
            }

            true
        })
        private void fadeOutAndHideImage(final ImageView draggableImage)
        {
            Animation fadeOut = new AlphaAnimation(1, 0);
            fadeOut.setInterpolator(new AccelerateInterpolator());
            fadeOut.setDuration(1000);

            fadeOut.setAnimationListener(new AnimationListener()
            {
                public void onAnimationEnd(Animation animation)
                {
                    draggableImage.setVisibility(View.GONE);
                }
                public void onAnimationRepeat(Animation animation) {}
                public void onAnimationStart(Animation animation) {}
            });

            draggableImage.startAnimation(fadeOut);
        }
            draggableImage.setOnTouchListener(listener)
            draggableImage2.setOnTouchListener(listener)

        }

    }
}

1 个答案:

答案 0 :(得分:1)

参考此答案:Making a smooth fade out for imageview in android

通过调用fadeOutAndHideImage(img)替换代码中的img.setVisibility(View.GONE),如下所示:

  private void fadeOutAndHideImage(final ImageView img)
  {
    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator());
    fadeOut.setDuration(1000);

    fadeOut.setAnimationListener(new AnimationListener()
    {
            public void onAnimationEnd(Animation animation) 
            {
                  img.setVisibility(View.GONE);
            }
            public void onAnimationRepeat(Animation animation) {}
            public void onAnimationStart(Animation animation) {}
    });

    img.startAnimation(fadeOut);
    }

它将首先应用淡出动画,然后隐藏图像视图。

科特琳

       private fun fadeOutAndHideImage(img: ImageView) {
    val fadeOut = AlphaAnimation(1F, 0F)
    fadeOut.setInterpolator(AccelerateInterpolator())
    fadeOut.setDuration(1000)

    fadeOut.setAnimationListener(object: Animation.AnimationListener {
       override fun onAnimationEnd(animation:Animation) {
            img.setVisibility(View.GONE)
        }
        override fun onAnimationRepeat(animation:Animation) {}
        override  fun onAnimationStart(animation:Animation) {}
    })
    img.startAnimation(fadeOut)
}