在一个实际的android项目中,遇到这样一个问题:怎样让当前的一个图片变透明呢?
最后解决了,在这里贴上解决方案,当然参考了网上的一些想法.
在这里,我和大家简单的说说我的解决方案:写了一个小小的demo,希望大家都能够理解.
主界面很简单:就是一个ImageView,需要将一个Bitmap的对象set给它.读者现在可以找一张图片,将这张图片放入sd卡中.
程序的声明部分:
1 private ImageView iv_bitmap;
2 private String file_path = "/data/data/android.jpg"; //sd卡中文件的路径
那么怎么样取得SD卡中的文件呢?
1 //检查我放在sd卡中的文件是否存在
2 File f=new File(file_path);
3 if(f.exists()) {
4 //获得Bitmap的对象
5 Bitmap bm = BitmapFactory.decodeFile(file_path);
6 //Bitmap bm2 = makeTheImageTransparent(bm);
7 //iv_bitmap.setImageBitmap(bm2);
8 iv_bitmap.setImageBitmap(bm);
9 } else {
10 Toast.makeText(MainActivity.this, "文件不存在", 1).show();
11 }
最重要的就是让图片变透明的方法的编写了:
1 //使图片透明的方法
2 public Bitmap makeTheImageTransparent(Bitmap bitmap) {
3 bitmap = Bitmap.createBitmap(50, 50, Bitmap.Config.ARGB_4444);
4 bitmap.eraseColor(Color.TRANSPARENT);
5 return bitmap;
6 }
至此全部完成.
2011.10.25.am
jack.li
QQ:523072842
Email:523072842@qq.com
There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.