本文實例講述了Android編程實現(xiàn)GridView控件點擊圖片變暗效果的方法。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)建站是創(chuàng)新、創(chuàng)意、研發(fā)型一體的綜合型網(wǎng)站建設公司,自成立以來公司不斷探索創(chuàng)新,始終堅持為客戶提供滿意周到的服務,在本地打下了良好的口碑,在過去的10余年時間我們累計服務了上千家以及全國政企客戶,如成都除甲醛等企業(yè)單位,完善的項目管理流程,嚴格把控項目進度與質量監(jiān)控加上過硬的技術實力獲得客戶的一致贊譽。@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); grid = (GridView) findViewById(R.id.grid); grid.setAdapter(new GridAdapter()); grid.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(MainActivity.this, "ddd", 1000).show(); } }); } class GridAdapter extends BaseAdapter { @Override public int getCount() { return imgs.length; } @Override public Object getItem(int arg0) { return arg0; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View view, ViewGroup arg2) { if (view == null) { holder = new ViewHolder(); view = LayoutInflater.from(MainActivity.this).inflate( R.layout.imgs, null); holder.view = (ImageView) view.findViewById(R.id.gridimg); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.view.setImageResource(imgs[arg0]); holder.view.setOnTouchListener(onTouchListener); return view; } class ViewHolder { ImageView view; } public OnTouchListener onTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: changeLight((ImageView) view, 0); // onclick break; case MotionEvent.ACTION_DOWN: changeLight((ImageView) view, -80); break; case MotionEvent.ACTION_MOVE: // changeLight(view, 0); break; case MotionEvent.ACTION_CANCEL: changeLight((ImageView) view, 0); break; default: break; } return true; } }; } private void changeLight(ImageView imageview, int brightness) { ColorMatrix matrix = new ColorMatrix(); matrix.set(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0, brightness, 0, 0, 0, 1, 0 }); imageview.setColorFilter(new ColorMatrixColorFilter(matrix)); }