Thursday, July 1, 2010

android.graphics.Color

The Color class defines methods for creating and converting color ints. Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributes from red, gree, blue, and opaque-white would be 0xFFFFFFFF.

android.graphics.Color

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:id="@+id/background"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Spinner
android:id="@+id/colorspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>


AndroidColor.java
package com.exercise.AndroidColor;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class AndroidColor extends Activity {

LinearLayout background;
Spinner spinnerColor;

private static final String[] color =
{ "BLACK",
"BLUE",
"CYAN",
"DKGRAY",
"GRAY",
"GREEN",
"LTGRAY",
"MAGENTA",
"RED",
"TRANSPARENT",
"WHITE",
"YELLOW"};
private ArrayAdapter<String> adapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

background = (LinearLayout)findViewById(R.id.background);

spinnerColor = (Spinner)findViewById(R.id.colorspinner);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, color);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(adapter);
spinnerColor.setSelection(0);

spinnerColor.setOnItemSelectedListener(
new Spinner.OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
setBackgroundColor(spinnerColor.getSelectedItem().toString());
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}});
}


private void setBackgroundColor(String strColor){
Toast.makeText(this, strColor, Toast.LENGTH_LONG).show();
if (strColor == "BLACK"){
background.setBackgroundColor(Color.BLACK);
}else if(strColor=="BLUE"){
background.setBackgroundColor(Color.BLUE);
}else if(strColor=="CYAN"){
background.setBackgroundColor(Color.CYAN);
}else if(strColor=="DKGRAY"){
background.setBackgroundColor(Color.DKGRAY);
}else if(strColor=="GRAY"){
background.setBackgroundColor(Color.GRAY);
}else if(strColor=="GREEN"){
background.setBackgroundColor(Color.GREEN);
}else if(strColor=="LTGRAY"){
background.setBackgroundColor(Color.LTGRAY);
}else if(strColor=="MAGENTA"){
background.setBackgroundColor(Color.MAGENTA);
}else if(strColor=="RED"){
background.setBackgroundColor(Color.RED);
}else if(strColor=="TRANSPARENT"){
background.setBackgroundColor(Color.TRANSPARENT);
}else if(strColor=="WHITE"){
background.setBackgroundColor(Color.WHITE);
}else if(strColor=="YELLOW"){
background.setBackgroundColor(Color.YELLOW);
}
}
}


Download the files.

No comments: