在此先感谢您的帮助。我是 Java 和 Android 编程的初学者,所以我的第一个应用程序只是在您拍照后尝试从照片中选择 rgb 值。但是,最后一个错误是“令牌“字符串”上的语法错误,删除此令牌”。这让我疯狂!似乎我所有的括号都在那里,每个语句的语法都是正确的,对发生的事情有什么想法吗?如果这有所不同,我正在使用 Eclipse,最新版本。这是完整的代码。问题从评论开始/* SET TEXT FUNCTION TO THE FIELD USING SET TEXT METHOD*/.
package com.example.firstapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity { //1
TextView pixelcord, rgbvals;
ImageView iv;
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) { //2
/* GATHER THE INFORMATION FROM THE LAYOUT TO ORGANIZE APP*/
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* SET ON CLICK LISTENER TO GET CLICK REPSONSE -- LAUNCH CAMERA AND TAKE PHOTO */
/* SET VARIABLES FOR USE FROM EACH VIEW */
iv = (ImageView) findViewById(R.id.imageView); /* USE IMAGE VIEW FIELD*/
pixelcord = (TextView)findViewById(R.id.pixelcord); /* USE TEXT FIELD FOR PIXEL */
rgbvals = (TextView)findViewById(R.id.rgbvals); /* USE TEXT FIELD FOR RGB VALUES*/
btn = (Button) findViewById(R.id.takePhoto);
/* SET INFORMATION OF WHAT TO DO UPON EACH CLIK*/
iv.setOnTouchListener(imgSourceOnTouchListener);
/* =====================================CAMERA BUTTON=====================================*/
btn.setOnClickListener(new OnClickListener() { //3
@Override
public void onClick(View v) { //4
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
} //*4
}); //*3
/* =======================================================================================*/
} /* END OF ON CREATE*/ //*2
/* DECLARATION OF IMG TOUCH FUNCTION*/
OnTouchListener imgSourceOnTouchListener = new OnTouchListener() { //5
@Override
public boolean onTouch(View view, MotionEvent event) { //6
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 = Integer.valueOf((int)eventXY[0]); /* POTENTIALLY REDUNDANT*/
int y = Integer.valueOf((int)eventXY[1]);
/* SET TEXT FUNCTION TO THE FIELD USING SET TEXT METHOD*/
pixelcord.setText("X:" String.valueOf(eventX) + "/ Y:" String.valueOf(eventY) );
int touchedRGB = iv.getDrawingCache().getPixel(x,y);
rgbvals.setText("Color Value" + "#" + Integer.toHexString(touchedRGB));
rgbvals.setTextColor(touchedRGB);
return true;
} //*6
}; //*5
@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data)
{ //7
if(requestCode == 0)
{ //8
Bitmap theImage = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(theImage);
} //*8
} //*7
} //*1