这看起来很简单,我试图设置一个位图图像但是从资源中,我在drawable文件夹中的应用程序中.
bm = BitmapFactory.decodeResource(null, R.id.image);
Run Code Online (Sandbox Code Playgroud)
它是否正确?
xan*_*ndy 731
假设您在Activity类中调用它
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Run Code Online (Sandbox Code Playgroud)
第一个参数是Resources,是必需的.它通常可以在任何Context(和Activity之类的子类)中获得.
Par*_*han 33
试试这个
这是来自SD卡
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
Run Code Online (Sandbox Code Playgroud)
这来自资源
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Run Code Online (Sandbox Code Playgroud)
如果资源正在显示并且是视图,您也可以捕获它.像截图:
View rootView = ((View) findViewById(R.id.yourView)).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
Run Code Online (Sandbox Code Playgroud)
这实际上抓住了整个布局,但你可以随意改变.
| 归档时间: |
|
| 查看次数: |
256628 次 |
| 最近记录: |