playButton.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
switch(arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
playButton.setImageBitmap(res.getDrawable(R.drawable.play_pushed));
break;
}
case MotionEvent.ACTION_CANCEL:
{
playButton.setImageBitmap(res.getDrawable(R.drawable.play));
break;
}
}
return true;
}
});
两个 res 关键字用红色下划线标出。只需在触摸时尝试将图像更改为自身的较暗版本。所有必要的图像都位于 res 文件夹中。知道问题是什么吗?
更新:稍微更新了代码,现在它可以完美运行:
playButton.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
switch(arg1.getAction())
{
case MotionEvent.ACTION_DOWN:
{
playButton.setImageDrawable(getResources().getDrawable(R.drawable.play_pushed));
break;
}
case MotionEvent.ACTION_UP:
{
playButton.setImageDrawable(getResources().getDrawable(R.drawable.play));
break;
}
}
return true;
}
});