Android 图像淡入淡出动画
Android image fade animation
当我 运行 这段代码时,它什么也没做,我确定当我触摸按钮时会调用此事件。但它不会改变 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);
我的代码有问题吗?
您使用 ObjectAnimator 有什么原因吗?你能做到吗:
AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);
这是因为您在方法需要 View
时传入了一个 id。尝试传入 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();
}
});
试试这段代码,它使用 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:
如果你想使用 Button,最好使用 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);
}
};
**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;
}
});
}
}
当我 运行 这段代码时,它什么也没做,我确定当我触摸按钮时会调用此事件。但它不会改变 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);
我的代码有问题吗?
您使用 ObjectAnimator 有什么原因吗?你能做到吗:
AlphaAnimation anim = new AlphaAnimation(startAlphaValue, endAlphaValue);
anim.setDuration(duration);
view.startAnimation(anim);
这是因为您在方法需要 View
时传入了一个 id。尝试传入 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();
}
});
试试这段代码,它使用 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:
如果你想使用 Button,最好使用 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);
}
};
**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;
}
});
}
}