构建可在“图像查看”时检测颜色的应用程序。点击。不幸的是,在我摆脱了getResource()。getContent()'的所有警告后,输出没有显示。或者我的'如果'是错的?也许有人可以帮助我:)。
Button button;
ImageView imageView;
static final int CAM_REQUEST = 1;
TextView colorRGB;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.capimg);
colorRGB = (TextView)findViewById(R.id.colorrgb);
button = (Button) findViewById(R.id.button);
imageView = (ImageView) findViewById(R.id.image_view);
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);
}
});
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) {
int blue1 = ContextCompat.getColor(context, R.color.Blue1);
int blue2 = ContextCompat.getColor(context, R.color.Blue2);
int blue3 = ContextCompat.getColor(context, R.color.Blue3);
int blue4 = ContextCompat.getColor(context, R.color.Blue4);
int blue5 = ContextCompat.getColor(context, R.color.Blue5);
int blue6 = ContextCompat.getColor(context, R.color.Blue6);
int blue7 = ContextCompat.getColor(context, R.color.Blue7);
int blue8 = ContextCompat.getColor(context, R.color.Blue8);
int blue9 = ContextCompat.getColor(context, R.color.Blue9);
int blue10 = ContextCompat.getColor(context, R.color.Blue10);
int blue11 = ContextCompat.getColor(context, R.color.Blue11);
int blue12 = ContextCompat.getColor(context, R.color.Blue12);
int blue13 = ContextCompat.getColor(context, R.color.Blue13);
int blue14 = ContextCompat.getColor(context, R.color.Blue14);
int blue15 = ContextCompat.getColor(context, R.color.Blue15);
int blue16 = ContextCompat.getColor(context, R.color.Blue16);
int blue17 = ContextCompat.getColor(context, R.color.Blue17);
int blue18 = ContextCompat.getColor(context, R.color.Blue18);
int blue19 = ContextCompat.getColor(context, R.color.Blue19);
int blue20 = ContextCompat.getColor(context, R.color.Blue20);
int blue21 = ContextCompat.getColor(context, R.color.Blue21);
int blue22 = ContextCompat.getColor(context, R.color.Blue22);
int blue23 = ContextCompat.getColor(context, R.color.Blue23);
int blue24 = ContextCompat.getColor(context, R.color.Blue24);
int blue25 = ContextCompat.getColor(context, R.color.Blue25);
int green1 = ContextCompat.getColor(context, R.color.Green1);
int green2 = ContextCompat.getColor(context, R.color.Green2);
int green3 = ContextCompat.getColor(context, R.color.Green3);
int green4 = ContextCompat.getColor(context, R.color.Green4);
int green5 = ContextCompat.getColor(context, R.color.Green5);
int green6 = ContextCompat.getColor(context, R.color.Green6);
int green7 = ContextCompat.getColor(context, R.color.Green7);
int green8 = ContextCompat.getColor(context, R.color.Green8);
int green9 = ContextCompat.getColor(context, R.color.Green9);
int green10 = ContextCompat.getColor(context, R.color.Green10);
int green11 = ContextCompat.getColor(context, R.color.Green11);
int green12 = ContextCompat.getColor(context, R.color.Green12);
int green13 = ContextCompat.getColor(context, R.color.Green13);
int green14 = ContextCompat.getColor(context, R.color.Green14);
int green15 = ContextCompat.getColor(context, R.color.Green15);
int green16 = ContextCompat.getColor(context, R.color.Green16);
int green17 = ContextCompat.getColor(context, R.color.Green17);
int green18 = ContextCompat.getColor(context, R.color.Green18);
int green19 = ContextCompat.getColor(context, R.color.Green19);
int green20 = ContextCompat.getColor(context, R.color.Green20);
int white1 = ContextCompat.getColor(context, R.color.White1);
int white2 = ContextCompat.getColor(context, R.color.White2);
int white3 = ContextCompat.getColor(context, R.color.White3);
int white4 = ContextCompat.getColor(context, R.color.White4);
int white5 = ContextCompat.getColor(context, R.color.White5);
int white6 = ContextCompat.getColor(context, R.color.White6);
int white7 = ContextCompat.getColor(context, R.color.White7);
int white8 = ContextCompat.getColor(context, R.color.White8);
int brown1 = ContextCompat.getColor(context, R.color.Browns1);
int brown2 = ContextCompat.getColor(context, R.color.Browns2);
int brown3 = ContextCompat.getColor(context, R.color.Browns3);
int brown4 = ContextCompat.getColor(context, R.color.Browns4);
int brown5 = ContextCompat.getColor(context, R.color.Browns5);
int brown6 = ContextCompat.getColor(context, R.color.Browns6);
int brown7 = ContextCompat.getColor(context, R.color.Browns7);
int brown8 = ContextCompat.getColor(context, R.color.Browns8);
int brown9 = ContextCompat.getColor(context, R.color.Browns9);
int brown10 = ContextCompat.getColor(context, R.color.Browns10);
int brown11 = ContextCompat.getColor(context, R.color.Browns11);
int brown12 = ContextCompat.getColor(context, R.color.Browns12);
int brown13 = ContextCompat.getColor(context, R.color.Browns13);
int orange1 = ContextCompat.getColor(context, R.color.Orange1);
int orange2 = ContextCompat.getColor(context, R.color.Orange2);
int orange3 = ContextCompat.getColor(context, R.color.Orange3);
int orange4 = ContextCompat.getColor(context, R.color.Orange4);
int orange5 = ContextCompat.getColor(context, R.color.Orange5);
int orange6 = ContextCompat.getColor(context, R.color.Orange6);
int orange7 = ContextCompat.getColor(context, R.color.Orange7);
int orange8 = ContextCompat.getColor(context, R.color.Orange8);
int orange9 = ContextCompat.getColor(context, R.color.Orange9);
int orange10 = ContextCompat.getColor(context, R.color.Orange10);
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);
//colour for blue
if (touchedRGB == blue1)
{
//colorRGB.setText(R.string.blue_colour);
colorRGB.setText("Blue");
}
else if (touchedRGB == blue2)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue3)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue4)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue5)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue6)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue7)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue8)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue9)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue10)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue11)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue12)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue13)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue14)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue15)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue16)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue17)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue18)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue19)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue20)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue21)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue22)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue23)
{
colorRGB.setText(R.string.blue_colour);
}
else if (touchedRGB == blue24)
{
colorRGB.setText(R.string.blue_colour);
}
else if(touchedRGB == blue25)
{
colorRGB.setText(R.string.blue_colour);
}
//color for white
else if (touchedRGB == white1) {
colorRGB.setText(R.string.white_colour);
}
else if (touchedRGB == white2) {
colorRGB.setText(R.string.white_colour);
}
else if (touchedRGB == white3) {
colorRGB.setText(R.string.white_colour);
}
else if (touchedRGB == white4) {
colorRGB.setText(R.string.white_colour);
}
else if (touchedRGB == white5) {
colorRGB.setText(R.string.white_colour);
}
else if (touchedRGB == white6) {
colorRGB.setText(R.string.white_colour);
}
else if (touchedRGB == white7) {
colorRGB.setText(R.string.white_colour);
}
else if(touchedRGB == white8 ) {
colorRGB.setText(R.string.white_colour);
}
//colour for green
else if (touchedRGB == green1 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green2 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green3 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green4 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green5 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green6 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green7 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green8 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green9 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green10 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green11 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green12 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green13 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green14 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green15 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green16 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green17 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green18 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green19 ) {
colorRGB.setText(R.string.green_colour);
}
else if (touchedRGB == green20 ) {
colorRGB.setText(R.string.green_colour);
}
//colour for brown
else if (touchedRGB == brown1 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown2 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown3 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown4 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown5 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown6 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown7 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown8 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown9 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown10 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown11 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown12 ) {
colorRGB.setText(R.string.brown_colour);
}
else if (touchedRGB == brown13 ) {
colorRGB.setText(R.string.brown_colour);
}
//colour for orange and
else if (touchedRGB == orange1 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange2 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange3 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange4 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange5 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange6 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange7 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange8 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange9 ) {
colorRGB.setText(R.string.orange_colour);
}
else if (touchedRGB == orange10 ) {
colorRGB.setText(R.string.orange_colour);
}
return true;
}};
@Override
protected void onPause() {
super.onPause();
DJ.iAmLeaving();
}
@Override
public void onResume() {
super.onResume();
DJ.iAmIn(this);
}
这是&#39; .xml&#39;
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.zaidi.frame2.MainActivity2"
android:background="@drawable/background3"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Capture"
android:textSize="55sp"
android:id="@+id/CreditButton"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image"
android:id="@+id/button"
android:layout_below="@+id/CreditButton"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:background="@drawable/custom_button"/>
<ImageView
android:layout_width="350dp"
android:layout_height="300dp"
android:id="@+id/image_view"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/colorrgb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#ebcaca" />
最后是我的&#39; color.xml&#39;
<color name = "White1">#ffffff</color>
<color name = "White2">#f0ffff</color>
<color name = "White3">#f0f8ff</color>
<color name = "White4">#e6e6fa</color>
<color name = "White5">#fff0f5</color>
<color name = "White6">#f5fffa</color>
<color name = "White7">#eeeee0</color>
<color name = "White8">#eee8dc</color>
<color name = "Green1">#7fffd4</color>
<color name = "Green2">#66cdaa</color>
<color name = "Green3">#006400</color>
<color name = "Green4">#556b2f</color>
<color name = "Green5">#8fbc8f</color>
<color name = "Green6">#2e8b57</color>
<color name = "Green7">#3cb371</color>
<color name = "Green8">#20b2aa</color>
<color name = "Green9">#98fb98</color>
<color name = "Green10">#00ff7f</color>
<color name = "Green11">#7cfc00</color>
<color name = "Green12">#7fff00</color>
<color name = "Green13">#00fa9a</color>
<color name = "Green14">#adff2f</color>
<color name = "Green15">#32cd32</color>
<color name = "Green16">#9acd32</color>
<color name = "Green17">#228b22</color>
<color name = "Green18">#6b8e23</color>
<color name = "Green19">#bdb76b</color>
<color name = "Green20">#f0e68c</color>
<color name = "Blue1">#191970</color>
<color name = "Blue2">#000080</color>
<color name = "Blue3">#6495ed</color>
<color name = "Blue4">#483d8b</color>
<color name = "Blue5">#6a5acd</color>
<color name = "Blue6">#7b68ee</color>
<color name = "Blue7">#8470ff</color>
<color name = "Blue8">#0000cd</color>
<color name = "Blue9">#4169e1</color>
<color name = "Blue10">#0000ff</color>
<color name = "Blue11">#1e90ff</color>
<color name = "Blue12">#00bfff</color>
<color name = "Blue13">#87ceeb</color>
<color name = "Blue14">#87cefa</color>
<color name = "Blue15">#4682b4</color>
<color name = "Blue16">#b0c4de</color>
<color name = "Blue17">#add8e6</color>
<color name = "Blue18">#b0e0e6</color>
<color name = "Blue19">#afeeee</color>
<color name = "Blue20">#00ced1</color>
<color name = "Blue21">#48d1cc</color>
<color name = "Blue22">#40e0d0</color>
<color name = "Blue23">#00ffff</color>
<color name = "Blue24">#e0ffff</color>
<color name = "Blue25">#5f9ea0</color>
<color name = "Browns1">#bc8f8f</color>
<color name = "Browns2">#cd5c5c</color>
<color name = "Browns3">#8b4513</color>
<color name = "Browns4">#a0522d</color>
<color name = "Browns5">#cd853f</color>
<color name = "Browns6">#deb887</color>
<color name = "Browns7">#f5f5dc</color>
<color name = "Browns8">#f5deb3</color>
<color name = "Browns9">#f4a460</color>
<color name = "Browns10">#d2b48c</color>
<color name = "Browns11">#d2691e</color>
<color name = "Browns12">#b22222</color>
<color name = "Browns13">#a52a2a</color>
<color name = "Orange1">#e9967a</color>
<color name = "Orange2">#fa8072</color>
<color name = "Orange3">#ffa07a</color>
<color name = "Orange4">#ffa500</color>
<color name = "Orange5">#ff8c00</color>
<color name = "Orange6">#ff7f50</color>
<color name = "Orange7">#f08080</color>
<color name = "Orange8">#ff6347</color>
<color name = "Orange9">#ff4500</color>
<color name = "Orange10">#ff0000</color>
答案 0 :(得分:1)
您要从rgb TextView
为string.xml
设置文字,因此您应该更改
colorRGB.setText(R.string.blue_colour);
到
colorRGB.setText(getResources().getString(R.string.blue_colour));
希望这个帮助
答案 1 :(得分:0)
肯定是的!您错误地使用了If
,您正在初始化x
int x = 0;
并检查
if(x < 0)
总是返回false。