0

私はこのコードを実装しようとしています:

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が何であるかを見つけることができないと言うでしょう. あなたたちはそれが何であるか知っていますか?どのように使用する必要がありますか?少し早いですがお礼を

4

3 に答える 3

5

R は、プロジェクト内のリソースを追跡する自動生成ファイルです。ドローアブルとは、リソースがドローアブル タイプであることを意味します。通常は (常にではありませんが) リソースがres/drawables-foldersのいずれかにあることを意味しますres/drawables_xhdpi。four_colors はリソース名を指します。通常、参照しているファイルは、たとえば res/drawables-xhdpi フォルダー内の「four_colors」というファイル (たとえば、PNG ファイル) であることを示します。

したがって、four_colors は、アプリがロードしようとしている (この場合) ドローアブルの名前を参照します。

Eclipse でリソースが見つからないというメッセージが表示された場合は、そのリソースが含まれるべきプロジェクトに含まれていないことを意味します。たとえば、いくつかのコードをコピーしましたが、コードで参照されているドローアブルはコピーしていません。

この行BitmapFactory.decodeResource(...)は、まさにそのとおりです。エンコードされた画像をビットマップにデコードします。これは、Android が実際に表示できるものです。通常、ビットマップを使用すると、内部でこの種のデコードが行われます。ここでは手動で行います。

于 2012-09-16T22:06:56.970 に答える
1

You'll need to download this image and put it in your ./res/drawable folder. Be sure to right click the project and select refresh.

于 2012-09-17T02:54:12.903 に答える