欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android编程实现GridView控件点击图片变暗效果的方法

程序员文章站 2023-11-29 19:21:04
本文实例讲述了android编程实现gridview控件点击图片变暗效果的方法。分享给大家供大家参考,具体如下: @override public void o...

本文实例讲述了android编程实现gridview控件点击图片变暗效果的方法。分享给大家供大家参考,具体如下:

@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));
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android控件用法总结》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android数据库操作技巧总结》及《android资源操作技巧汇总

希望本文所述对大家android程序设计有所帮助。