public class MainActivity extends Activity {
boolean bSwitch = false;
int iSwitch = 0, iSwitch2 = 0;
RelativeLayout rl_1;
Button [] btn = new Button[3];
int [] iResId = {R.id.button1, R.id.button2, R.id.button3};
int [] iResDrawable = {R.drawable.icon_cat, R.drawable.icon_dog, R.drawable.icon_mouse};
Bitmap [] bitmap = new Bitmap[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl_1 = (RelativeLayout) findViewById(R.id.relativeLayout1);
//////////////////////////////////
// 利用 Matrix 和 Bitmap 調整圖片大小(縮放)
Matrix matrix = new Matrix();
matrix.postScale(0.5f,0.5f);
for(int i=0; i<bitmap.length; i++) {
btn[i] = (Button) findViewById(iResId[i]);
bitmap[i] = BitmapFactory.decodeResource(getResources(), iResDrawable[i]);
bitmap[i] = Bitmap.createScaledBitmap(bitmap[i], bitmap[i].getWidth()/2,
bitmap[i].getHeight()/2, false);//縮小四分之一
// bitmap[i] = Bitmap.createBitmap(bitmap[i], 0, 0, bitmap[i].getWidth(), bitmap[i].getHeight(),
// matrix, false);
btn[i].setCompoundDrawablesWithIntrinsicBounds(null,new BitmapDrawable(bitmap[i]),null,null);
}
}
public void clickToSwitch(View v) {
if(v == btn[0]) {
bSwitch = !bSwitch;
if(bSwitch) {
rl_1.setBackgroundColor(Color.RED);
}
else {
rl_1.setBackgroundColor(Color.GREEN);
}
}
else if (v == btn[1]) {
iSwitch++;
iSwitch = iSwitch % 3;
switch(iSwitch) {
case 0:
rl_1.setBackgroundColor(Color.WHITE);
break;
case 1:
rl_1.setBackgroundColor(Color.GRAY);
break;
case 2:
rl_1.setBackgroundColor(Color.BLACK);
break;
}
}
else if (v == btn[2]) {
iSwitch++;
iSwitch = iSwitch % 4;
switch(iSwitch) {
case 0:
rl_1.setBackgroundColor(Color.CYAN);
break;
case 1:
rl_1.setBackgroundColor(Color.MAGENTA);
break;
case 2:
rl_1.setBackgroundColor(Color.YELLOW);
break;
case 3:
rl_1.setBackgroundColor(Color.LTGRAY);
break;
}
}
}
}
留言列表