Android的GridView控件点击图片变暗效果_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Android的GridView控件点击图片变暗效果

Android的GridView控件点击图片变暗效果

 2013/12/17 13:09:09  wangzhigang2  程序员俱乐部  我要评论(0)
  • 摘要:@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);grid=(GridView)findViewById(R.id.grid);grid.setAdapter(newGridAdapter());grid.setOnItemClickListener
  • 标签:android view 图片 控件
class="java" name="code">
@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));

}

声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息
原文作者: w13798976975
原文地址: http://my.eoe.cn/wgblwss/archive/15797.html
发表评论
用户名: 匿名