这篇文章主要介绍了Android中ImageView用法,结合实例形式较为详细的分析了ImageView控件的功能,属性设置与使用相关技巧,需要的朋友可以参考下
这篇文章主要介绍了Android中ImageView用法,结合实例形式较为详细的分析了ImageView控件的功能,属性设置与使用相关技巧,需要的朋友可以参考下
这篇文章主要介绍了Android中ImageView用法,结合实例形式较为详细的分析了ImageView控件的功能,属性设置与使用相关技巧,需要的朋友可以参考下
本文实例分析了中用法。分享给大家供大家参考,具体如下:
猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明ImageView的用法。
首先,在res/drawable中引入三张牌:分别是梅花7,梅花8,梅花9
然后在res/layout/main.xml中配置一个TextView,三个ImageView以及一个Button
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv07" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv08" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/iv09" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout> |
程序如下所示:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
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<s.length;i++){ int tmp=s[i]; int a=(int)(Math.random()*2); s[i]=s[a]; s[a]=tmp; } }} |
希望本文所述对大家Android程序设计有所帮助。
发表评论