我正在尝试实现此代码:
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
}
}
}
我似乎无法弄清楚这行代码的作用
bmp = BitmapFactory.decodeResource(getResources(),R.drawable.four_colors);
当我试图实现它时,eclipse会说它无法找到four_colors是什么,我不知道它是什么,似乎无法弄清楚它。
你们知道它是什么吗?它应该如何使用?
提前谢谢
答案 0 :(得分:5)
R是一个自动生成的文件,用于跟踪项目中的资源。 drawable意味着资源属于drawable类型,通常(但不总是)意味着资源位于您的res/drawables
个文件夹中,例如res/drawables_xhdpi
。 Four_colors指的是资源名称,通常表示您所指的文件是名为'four_colors`的文件(例如PNG文件),例如, res / drawables-xhdpi文件夹。
因此,four_colors指的是(在这种情况下)应用程序试图加载的名称。
当Eclipse说它无法找到资源时,这意味着资源不包含在应该包含它的项目中。例如。您复制了一些代码,但没有复制代码中引用的drawable。
行BitmapFactory.decodeResource(...)
完全按照其说法行事;它将编码图像解码为位图,Android可以实际显示。通常当你使用位图时,它会在引擎盖下进行这种解码;这是手工完成的。
答案 1 :(得分:1)
您需要下载this图片并将其放入./res/drawable
文件夹中。确保右键单击项目并选择刷新。
答案 2 :(得分:1)
four_colors是图像的名称。你必须在res / drawable图像中放置图像应该有名称four_colors.jpg