安卓图片淡入淡出动画

3
当我运行这段代码时,它什么都没做。我确定当我触摸按钮时会调用此事件,但它并没有改变imageView的不透明度。
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f);
        fadeAltAnim.start();
    }
};


findViewById(R.id.dummy_button).setOnTouchListener(mDelayHideTouchListener);

我的代码有问题吗?


1
onTouch() 捕获许多不同的触摸事件。除非您知道自己在做什么,否则永远不要这样做并完全放弃触摸处理。请改用 OnClickListener - Xaver Kapeller
4个回答

4

试试这段代码,它使用的是ViewPropertyAnimator:

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener()   {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {

    view.animate().alpha(0.2f).setDuration(1000);
    }
};

设置动画时长是很有必要的,这样动画就知道需要运行多长时间。

编辑:如果您正在使用onTouchListener,您可能希望将其附加到MotionEvent上,例如:

View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener()   {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if(motionEvent == MotionEvent.ACTION_DOWN)
        view.animate().alpha(0.2f).setDuration(1000);
    }
}; 

编辑2:

如果你想使用一个按钮,最好使用OnClickListener而不是OnTouchListener;如果你想添加一张图片,你需要初始化它(例如在ImageView中):

Button button = (Button) findViewById(R.id.your_button);
ImageView imageView = (ImageView) findViewById(R.id.imgTest);

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            imageView.animate().alpha(0.2f).setDuration(1000);

    }
};

谢谢,我该如何将它附加到按钮上?当我触摸图像时它有效,但是我有一个按钮,我希望在按下按钮时图像会淡出。 - SalmanShariati

2

这是因为你传入了一个id,而该方法需要传入View。尝试传入View

所以基本上将 ObjectAnimator.ofFloat(R.id.imgTest, "alpha", 0.2f); 改为 ObjectAnimator.ofFloat(view, "alpha", 0.2f);

以下是示例

findViewById(R.id.image).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(v, "alpha", 0.2f);
            fadeAltAnim.start();
        }
});

0

你使用 ObjectAnimator 的原因是什么?你能这样做吗:

AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);

0
**try this** 

public class Animationtest extends Activity {

private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.test);
    imageView = (ImageView)findViewById(R.id.text);
    setAnimation();
}
private void setAnimation(){
    imageView.setOnTouchListener(new OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "touch", Toast.LENGTH_LONG).show();
             ObjectAnimator fadeAltAnim = ObjectAnimator.ofFloat(imageView, "alpha", 0.2f);
                fadeAltAnim.start();
            return false;
        }
    });

}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接