BitmapFactory.decodeResource(getResources(),R.drawable.four_colors); four_colors是什么意思?

osk*_*132 0 java android photo

我正在尝试实现此代码:

package fortyonepost.com.iapa;  

import android.app.Activity;  
import android.graphics.Bitmap;  
import android.graphics.BitmapFactory;  
import android.os.Bundle;  
import android.util.Log;  

public class ImageAsPixelArray extends Activity  
{  
//a Bitmap that will act as a handle to the image  
private Bitmap bmp;  

//an integer array that will store ARGB pixel values  
private int[][] rgbValues;  

/** Called when the activity is first created. */  
@Override  
public void onCreate(Bundle savedInstanceState)  
{  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.main);  

    //load the image and use the bmp object to access it  
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.four_colors);  

    //define the array size  
    rgbValues = new int[bmp.getWidth()][bmp.getHeight()];  

    //Print in LogCat's console each of one the RGB and alpha values from the 4 corners of the image  
    //Top Left  
    Log.i("Pixel Value", "Top Left pixel: " + Integer.toHexString(bmp.getPixel(0, 0)));  
    //Top Right  
    Log.i("Pixel Value", "Top Right pixel: " + Integer.toHexString(bmp.getPixel(31, 0)));  
    //Bottom Left  
    Log.i("Pixel Value", "Bottom Left pixel: " + Integer.toHexString(bmp.getPixel(0, 31)));  
    //Bottom Right  
    Log.i("Pixel Value", "Bottom Right pixel: " + Integer.toHexString(bmp.getPixel(31, 31)));  

    //get the ARGB value from each pixel of the image and store it into the array  
    for(int i=0; i < bmp.getWidth(); i++)  
    {  
        for(int j=0; j < bmp.getHeight(); j++)  
        {  
            //This is a great opportunity to filter the ARGB values  
            rgbValues[i][j] = bmp.getPixel(i, j);  
        }  
    }  

    //Do something with the ARGB value array  
}  
}  

}
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚这行代码是做什么的bmp = BitmapFactory.decodeResource(getResources(),R.drawable.four_colors);
当我试图实现它时,eclipse会说它无法找到four_colors是什么,我不知道它是什么,似乎无法弄明白.你们知道它是什么吗?它应该如何使用?预先感谢

Rei*_*ier 5

R是一个自动生成的文件,用于跟踪项目中的资源.drawable意味着资源属于drawable类型,通常(但并非总是)意味着资源位于您的一个文件res/drawables夹中,例如res/drawables_xhdpi.Four_colors指的是资源名称,通常表示您所引用的文件是例如res/drawables-xhdpi文件夹中名为"four_colors"的文件(例如PNG文件).

因此,four_colors指的是(在这种情况下)绘制您的app试图加载的名称.

当Eclipse说它无法找到资源时,这意味着资源不包含在应该包含它的项目中.例如,您复制了一些代码,但没有复制代码中引用的drawable.

这条线BitmapFactory.decodeResource(...)正如它所说的那样; 它将编码后的图像解码为位图,Android可以实际显示.通常当你使用位图时,它会在引擎盖下进行这种解码; 这是手动完成的.