无法解析context.getResources Resources API

时间:2016-03-17 06:09:59

标签: android api resources

为什么“context.getResources”无法解决?这段代码来自Is there any shortcut on Android Studio to get the HEX value of color from text or something like this? 我添加了color.xml,我尝试从类中获取颜色。请帮帮我..我是新手:(

公共类CapImg扩展了Activity {

Button button,button2;
ImageView imageView;
static final int CAM_REQUEST = 1;
static final int RQS_IMAGE1 = 1;
TextView touchedXY, invertedXY, imgSize, colorRGB;
private static final int SELECT_PHOTO = 100;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.capimg);


    touchedXY = (TextView)findViewById(R.id.xy);
    invertedXY = (TextView)findViewById(R.id.invertedxy);
    imgSize = (TextView)findViewById(R.id.size);
    colorRGB = (TextView)findViewById(R.id.colorrgb);
    button = (Button) findViewById(R.id.button);
    button2 = (Button) findViewById(R.id.button2);
    imageView = (ImageView) findViewById(R.id.image_view);

    Resources res = context.getResources();
    int color = res.getColor(R.color.Blue1, res.getTheme());

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File file = getFile();
            camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
            startActivityForResult(camera_intent, CAM_REQUEST);




        }
    });

    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(intent, RQS_IMAGE1);
        }
    });




    imageView.setOnTouchListener(imgSourceOnTouchListener);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){

    String path = "sdcard/UTP_app/cam_image.jpg";
    imageView.setImageDrawable(Drawable.createFromPath(path));

}



private File getFile() {
    File folder = new File("sdcard/UTP_app");
    if (!folder.exists()) {
        folder.mkdir();
    }

    File image_file = new File(folder,"cam_image.jpg");
    return image_file;
    //return new File(folder, new Date().getTime()+".jpg");
}


OnTouchListener imgSourceOnTouchListener
        = new OnTouchListener(){

    @Override
    public boolean onTouch(View view, MotionEvent event) {

        float eventX = event.getX();
        float eventY = event.getY();
        float[] eventXY = new float[] {eventX, eventY};

        Matrix invertMatrix = new Matrix();
        ((ImageView)view).getImageMatrix().invert(invertMatrix);

        invertMatrix.mapPoints(eventXY);
        int x = 0;
        int y = 1;



        Drawable imgDrawable = ((ImageView)view).getDrawable();
        Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();


        //Limit x, y range within bitmap
        if(x < 0){
            x = 0;
        }else if(x > bitmap.getWidth()-1){
            x = bitmap.getWidth()-1;
        }

        if(y < 0){
            y = 0;
        }else if(y > bitmap.getHeight()-1){
            y = bitmap.getHeight()-1;
        }

        int touchedRGB = bitmap.getPixel(x, y);






        if (touchedRGB == Color.
        {
            colorRGB.setText(" RED");
        }
        if (touchedRGB == Color.BLUE) {
            colorRGB.setText(" Blue");
        }
        if (touchedRGB == Color.GREEN) {
            colorRGB.setText(" Green");
        }

        return true;
    }};

@Override
protected void onPause() {
    super.onPause();
    DJ.iAmLeaving();

}

@Override
public void onResume() {
    super.onResume();
    DJ.iAmIn(this);
}

}

4 个答案:

答案 0 :(得分:1)

由于您已经在“活动”中,因此直接将其设置为获取资源

Resources res = getResources();

答案 1 :(得分:1)

我对buttontext的字符串有同样的问题。

也许这有帮助:

getApplicationContext().getString(R.string.canceltext)

或在您的情况下为颜色:

getApplicationContext().getColor(R.color.Blue1);

答案 2 :(得分:0)

而不是context.getResources();getApplicationcontext().getresources();

答案 3 :(得分:0)

活动使用然后此代码使用:

int color = getResources().getColor(R.color.Blue1);

片段使用然后此代码使用:

int color = getActivity().getResources().getColor(R.color.Blue1);