猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明ImageView的用法。
首先,在res/drawable中引入三张牌:分别是梅花7,梅花8,梅花9
然后在res/layout/main.xml中配置一个TextView,三个ImageView以及一个Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
程序如下面所示:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class A04Activity extends Activity {
private ImageView iv07,iv08,iv09;
private TextView tv;
private Button b;
private int[] s={
R.drawable.puke07,
R.drawable.puke08,
R.drawable.puke09
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv07=(ImageView)findViewById(R.id.iv07);
iv08=(ImageView)findViewById(R.id.iv08);
iv09=(ImageView)findViewById(R.id.iv09);
tv=(TextView)findViewById(R.id.tv);
b=(Button)findViewById(R.id.button);
randon();
//下面以ImageView的OnClickListener()方法对选择的不同牌做不同的反应
iv07.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv07.setImageDrawable(getResources().getDrawable(s[0]));
iv08.setImageDrawable(getResources().getDrawable(s[1]));
iv09.setImageDrawable(getResources().getDrawable(s[2]));
iv08.setAlpha(100); //对没有选择的牌做灰暗处理
iv09.setAlpha(100);
if(s[0]==R.drawable.puke08){ //如果选择的牌是梅花8的话就猜对了
tv.setText("恭喜你,猜对了!!!");
}
else{
tv.setText("亲,猜错了,要不要再来一次?");
}
}
});
iv08.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv07.setImageDrawable(getResources().getDrawable(s[0]));
iv08.setImageDrawable(getResources().getDrawable(s[1]));
iv09.setImageDrawable(getResources().getDrawable(s[2]));
iv07.setAlpha(100);
iv09.setAlpha(100);
if(s[1]==R.drawable.puke08){
tv.setText("恭喜你,猜对了!!!");
}
else{
tv.setText("亲,猜错了,要不要再来一次?");
}
}
});
iv09.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
iv07.setImageDrawable(getResources().getDrawable(s[0]));
iv08.setImageDrawable(getResources().getDrawable(s[1]));
iv09.setImageDrawable(getResources().getDrawable(s[2]));
iv07.setAlpha(100);
iv08.setAlpha(100);
if(s[2]==R.drawable.puke09){
tv.setText("恭喜你,猜对了!!!");
}
else{
tv.setText("亲,猜错了,要不要再来一次?");
}
}
});
b.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
tv.setText("猜猜梅花8在哪里");
iv07.setImageDrawable(getResources().getDrawable(R.drawable.puke00));
//刚开始的时候显示扑克牌的背面
iv08.setImageDrawable(getResources().getDrawable(R.drawable.puke00));
iv09.setImageDrawable(getResources().getDrawable(R.drawable.puke00));
iv07.setAlpha(255);//
iv08.setAlpha(255);
iv09.setAlpha(255);
randon();
}
});
}
//randon方法是进行随机地洗牌
public void randon(){
for(int i=0;i
int a=(int)(Math.random()*2);
s[i]=s[a];
s[a]=tmp;
}
}
}