我正在尝试实现以下代码:
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是什么,我不知道它是什么,似乎也搞不清楚它。你们知道这是什么吗?它应该如何使用呢?提前感谢
发布于 2012-09-17 06:06:57
R是一个自动生成的文件,用于跟踪项目中的资源。drawable表示资源的类型为drawable,通常(但不总是)表示资源位于某个res/drawables-folders中,例如res/drawables_xhdpi。Four_colors指的是资源名称,通常表示您引用的文件是res/drawables-xhdpi文件夹中名为'four_colors`的文件(例如PNG文件)。
因此,four_colors指的是(在本例中)您的应用程序试图加载的可绘制文件的名称。
当Eclipse说找不到该资源时,这意味着该资源没有包含在应该包含该资源的项目中。例如,您复制了一些代码,但没有复制代码中引用的可绘制内容。
代码行BitmapFactory.decodeResource(...)做了它所说的事情;它将编码的图像解码成位图,这是安卓可以实际显示的东西。通常,当您使用位图时,它会在幕后进行这种解码;在这里,它是手动完成的。
发布于 2012-09-17 10:54:13
您需要下载this镜像并将其放入您的./res/drawable文件夹。确保右键单击项目并选择refresh。
发布于 2016-08-11 01:24:22
four_colors是镜像的名称。您必须将图像放置在res / drawable图像中,该图像应具有名称four_colors.jpg
https://stackoverflow.com/questions/12451112
复制相似问题