废话不多说了,直接给大家贴代码了,具体代码如下所示:
import android.content.Context; import android.graphics.ColorMatrixColorFilter; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; public class MyImageView extends ImageView{ public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub this.setOnTouchListener(VIEW_TOUCH_DARK); } public static final OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() { //变暗(三个-50,值越大则效果越深) public final float[] BT_SELECTED_DARK = new float[] { 1, 0, 0, 0, -50, 0, 1, 0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 }; /* //变亮 public final float[] BT_SELECTED_LIGHT = new float[] { 1, 0, 0, 0, 50, 0, 1, 0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0 }; //恢复 public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }; */ @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { ImageView iv = (ImageView) v; iv.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED_DARK)); } else if (event.getAction() == MotionEvent.ACTION_UP) { ImageView iv = (ImageView) v; iv.clearColorFilter(); } return false; //如为false,执行ACTION_DOWN后不再往下执行 } }; }
以上所述是小编给大家介绍的ImageView点击可变暗的实例代码(android代码技巧),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对呐喊教程网站的支持!