Jump to content

Xml android


gamarra

Recommended Posts

pessoal estou a tentar fazer um programa que tem um ficheiro json, e que dentro desse ficheiro json tem as cores rgb em hex e vários cenários.

depois quando tocar numa imagem ele vai comparar as cores rgb ao ficheiro json e mostra a cor numa surfaceview

so que devo ter um grande erro no codigo so que não consigo descobrir

try {
 BufferedReader cr = new BufferedReader(new InputStreamReader(this.getAssets().open("themeScenariosInfo.json")));
 String line;
 while ((line = cr.readLine()) != null) {
 sb.append(line);
 }
 //Log.d("",sb.toString());
}catch (IOException e) {
 e.printStackTrace();
}
try {
 JSONObject json = new JSONObject(new JSONTokener(sb.toString()));
 JSONArray jsonRgb = json.getJSONArray("themeScenariosInfo");

 for(int i=0;i<jsonRgb.length();i++){
 JSONObject jsonObject = (JSONObject) jsonRgb.get(i);
 String jsonRgb1 = jsonObject.getString("scenarioId");
 JSONArray jsonRgb2 = jsonObject.getJSONArray("scenarioSpotables");
 for(int e=0;i<jsonRgb2.length();i++){
 JSONObject jsonObject1 = (JSONObject) jsonRgb2.get(e);
 String jsonRgb3 = jsonObject1.getString("spotableId");
 final JSONArray jsonRgb4 = jsonObject1.getJSONArray("spotableRGBs");
 textView.setText(String.valueOf(jsonObject1));
 //JSONObject jsonRgb5 = jsonRgb4.getJSONObject(Color.rgb(e, e, e));
 //Log.d("","kjfjf");
 for(int r=0;r<jsonRgb4.length();r++){
 textView.setText(String.valueOf(jsonRgb4));

	 image.setOnTouchListener(new ImageView.OnTouchListener(){
	 public boolean onTouch(View v, MotionEvent event) {
	 // TODO Auto-generated method stub
	 // textView.setText("Touch coordinates : " +
	 // String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
	 ImageView imageView = ((ImageView)v);
	 Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
	 //Log.d("FF",bitmap.getHeight()+"");
	 float escla = bitmap.getWidth()/image.getWidth();
	 float escala= bitmap.getHeight()/image.getHeight();
	 int color = bitmap.getPixel(((int)(event.getX()*escla)),((int)(event.getY()*escala)));		
	 int redValue = ((color >> 16) & 0xFF);
	 int blueValue = ((color >> 8) & 0xFF);
	 int greenValue = ((color >> 0) & 0xFF);
	 if("color".equals(jsonRgb4)){
	 textView.invalidate();
	 }
	 return true;
	 }



 });
 }
 }
 }

} catch (JSONException e) {
 e.printStackTrace();
}
}
Link to comment
Share on other sites

JSONArray jsonRgb4
if("color".equals(jsonRgb4))
Citação

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#equals(java.lang.Object)

equals

public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Overrides:equals in class Object

Parameters:an Object - the object to compare this String against.

Returns: true if the String are equal; false otherwise.

See Also:compareTo(java.lang.String), equalsIgnoreCase(java.lang.String)

 

Estás a tentar compara a string "color" com um objecto. Assim à cabeça detecto esse erro, não quer dizer que não existam mais.

Link to comment
Share on other sites

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_get_xml);
 final TextView textView = (TextView)findViewById(R.id.textView1);
 final ImageView image = (ImageView)findViewById(R.id.imageView1);
 final SurfaceView viewcolor = (SurfaceView)findViewById(R.id.surfaceView1);
 final StringBuilder sb = new StringBuilder(50);
 try {
  BufferedReader cr = new BufferedReader(new InputStreamReader(this.getAssets().open("themeScenariosInfo.json")));
  String line;
  while ((line = cr.readLine()) != null) {
   sb.append(line);
  }
  //Log.d("",sb.toString());
 }catch (IOException e) {
  e.printStackTrace();
 }
 try {
  JSONObject json = new JSONObject(new JSONTokener(sb.toString()));
  JSONArray jsonRgb = json.getJSONArray("themeScenariosInfo");

  for(int i=0;i<jsonRgb.length();i++){
   JSONObject jsonObject = (JSONObject) jsonRgb.get(i);
   String jsonRgb1 = jsonObject.getString("scenarioId");
   JSONArray jsonRgb2 = jsonObject.getJSONArray("scenarioSpotables");
   for(int e=0;i<jsonRgb2.length();i++){
    JSONObject jsonObject1 = (JSONObject) jsonRgb2.get(e);
    String jsonRgb3 = jsonObject1.getString("spotableId");
    JSONArray jsonRgb4 = jsonObject1.getJSONArray("spotableRGBs");
    textView.setText(String.valueOf(jsonObject1));
    for(int r=0;r<jsonRgb4.length();r++){
     textView.setText(String.valueOf(jsonRgb4));

     final String jsonrgb5 = ((JSONObject)jsonRgb4.get(r)).getString("rgb");
     image.setOnTouchListener(new ImageView.OnTouchListener(){
      public boolean onTouch(View v, MotionEvent event) {
       textView.setText("Touch coordinates : " +
         String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
       ImageView imageView = ((ImageView)v);
       Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
       //Log.d("FF",bitmap.getHeight()+"");
       float escla = bitmap.getWidth()/image.getWidth();
       float escala= bitmap.getHeight()/image.getHeight();
       int color = bitmap.getPixel(((int)(event.getX()*escla)),((int)(event.getY()*escala)));
       int redValue = Color.red(color);//((color >> 16) & 0xFF);
       int blueValue = Color.blue(color);//((color >> 8) & 0xFF);
       int greenValue = Color.green(color);//((color >> 0) & 0xFF);
       String hexadecimal;   
       if (color < 0)  
        hexadecimal =  Integer.toHexString(Math.abs(color));  
       else 
        hexadecimal = Integer.toHexString(color);        
       if (hexadecimal.equals(jsonrgb5)){
        viewcolor.setBackgroundColor(color);
       }
       return true;
      }
Link to comment
Share on other sites

tens de começar a escrever as tags do geshi corretas

no teu caso (como estás a usar Java) seria [ code=java] (sem o espaço claro)

final String jsonrgb5 = ((JSONObject)jsonRgb4.get(r)).getString("rgb");
image.setOnTouchListener(new ImageView.OnTouchListener(){
 // ...
 if (hexadecimal.equals(jsonrgb5)){ // tenho um felling que "jsonrgb5" não está definido no scope do listener
 // ..
}
Edited by HappyHippyHippo
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

pessoal estou aqui com uma dificuldade, estou a tentar mudar o valor inteiro para hexa decimal so que ele esta sempre a dar o mesmo valor que e ff009d mas nao e o valor da cor, alguem me pode ajudar

int color = bitmap.getPixel(((int)(event.getX()*escla)),((int)(event.getY()*escala)));
 int redValue = Color.red(color);//((color >> 16) & 0xFF);
 int blueValue = Color.blue(color);//((color >> 8) & 0xFF);
 int greenValue = Color.green(color);//((color >> 0) & 0xFF);

 String hexadecimal;
 if (color < 0)
	 hexadecimal = Integer.toHexString(Math.abs(color));
 else
	 hexadecimal = Integer.toHexString(color);
Link to comment
Share on other sites

pessoal estou aqui com uma dificuldade, estou a tentar mudar o valor inteiro para hexa decimal so que ele esta sempre a dar o mesmo valor que e ff009d mas nao e o valor da cor, alguem me pode ajudar

int color = bitmap.getPixel(((int)(event.getX()*escla)),((int)(event.getY()*escala)));
 int redValue = Color.red(color);//((color >> 16) & 0xFF);
 int blueValue = Color.blue(color);//((color >> 8) & 0xFF);
 int greenValue = Color.green(color);//((color >> 0) & 0xFF);

 String hexadecimal;
 if (color < 0)
	 hexadecimal = Integer.toHexString(Math.abs(color));
 else
	 hexadecimal = Integer.toHexString(color);

Não é o valor da cor? Como sabes? Mete branco e verifica se dá FFFFF ou preto 000000
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.