gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:35:46 24 4
gpt4 key购买 nike

我正在尝试实现此代码:

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 是什么,我不知道它是什么并且似乎无法弄清楚。你们知道那是什么吗?以及应该如何使用它?提前致谢

最佳答案

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

因此,four_colors 指的是您的应用尝试加载的(在本例中)可绘制对象的名称。

当 Eclipse 说找不到资源时,这意味着该资源没有包含在应包含的项目中。例如。您复制了一些代码,但没有复制代码中引用的可绘制对象。

BitmapFactory.decodeResource(...) 行的作用与它所说的完全一样;它将编码的图像解码为位图,这是 Android 可以实际显示的。通常,当您使用位图时,它会在幕后进行这种解码;这里是手动完成的。

关于java - BitmapFactory.decodeResource(getResources(), R.drawable.four_colors);四色是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com