Android编程实现GridView控件点击图片变暗效果的方法
本文实例讲述了Android编程实现GridView控件点击图片变暗效果的方法。分享给大家供大家参考,具体如下:
@Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); grid=(GridView)findViewById(R.id.grid); grid.setAdapter(newGridAdapter()); grid.setOnItemClickListener(newOnItemClickListener(){ @Override publicvoidonItemClick(AdapterView>arg0,Viewarg1,intarg2, longarg3){ Toast.makeText(MainActivity.this,"ddd",1000).show(); } }); } classGridAdapterextendsBaseAdapter{ @Override publicintgetCount(){ returnimgs.length; } @Override publicObjectgetItem(intarg0){ returnarg0; } @Override publiclonggetItemId(intarg0){ returnarg0; } @Override publicViewgetView(intarg0,Viewview,ViewGrouparg2){ if(view==null){ holder=newViewHolder(); 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); returnview; } classViewHolder{ ImageViewview; } publicOnTouchListeneronTouchListener=newView.OnTouchListener(){ @Override publicbooleanonTouch(Viewview,MotionEventevent){ switch(event.getAction()){ caseMotionEvent.ACTION_UP: changeLight((ImageView)view,0); //onclick break; caseMotionEvent.ACTION_DOWN: changeLight((ImageView)view,-80); break; caseMotionEvent.ACTION_MOVE: //changeLight(view,0); break; caseMotionEvent.ACTION_CANCEL: changeLight((ImageView)view,0); break; default: break; } returntrue; } }; } privatevoidchangeLight(ImageViewimageview,intbrightness){ ColorMatrixmatrix=newColorMatrix(); matrix.set(newfloat[]{1,0,0,0,brightness,0,1,0,0, brightness,0,0,1,0,brightness,0,0,0,1,0}); imageview.setColorFilter(newColorMatrixColorFilter(matrix)); }
希望本文所述对大家Android程序设计有所帮助。