手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android中ImageView用法实例分析
Android中ImageView用法实例分析
摘要:本文实例分析了Android中ImageView用法。分享给大家供大家参考,具体如下:猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明...

本文实例分析了Android中ImageView用法。分享给大家供大家参考,具体如下:

猜牌游戏大家可能以前都玩过,这里我们用这个小游戏来说明ImageView的用法。

首先,在res/drawable中引入三张牌:分别是梅花7,梅花8,梅花9

然后在res/layout/main.xml中配置一个TextView,三个ImageView以及一个Button

<"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>

程序如下所示:

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程序设计有所帮助。

【Android中ImageView用法实例分析】相关文章:

基于Android ListView之加载使用技巧

Android CheckBox 的使用案例分析

Android在JNI中使用ByteBuffer的方法

Android开场动画实例类Java代码

在Android中 获取正在运行的Service 实例

Android笔记之:App调试的几个命令的实践与分析

Android用户界面开发之:TextView的使用实例

Android补间动画效果

Android开发之SQLite的使用方法

Andorid TextView字幕效果实例

精品推荐
分类导航