国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中ImageView用法實例分析

2020-04-11 10:52:34
字體:
來源:轉載
供稿:網友

本文實例分析了Android中ImageView用法。分享給大家供大家參考,具體如下:

猜牌游戲大家可能以前都玩過,這里我們用這個小游戲來說明ImageView的用法。

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

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

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

程序如下所示:

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開發(fā)入門與進階教程》、《Android基本組件用法總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 中阳县| 建湖县| 嘉定区| 郓城县| 汤原县| 丹江口市| 密山市| 屏东市| 永德县| 泗洪县| 澄迈县| 噶尔县| 都江堰市| 凤山市| 高要市| 乡宁县| 屯留县| 汾阳市| 永济市| 彭阳县| 公安县| 临西县| 泊头市| 泌阳县| 蒲江县| 鄂托克旗| 县级市| 平阴县| 黄冈市| 丹棱县| 睢宁县| 庐江县| 通化市| 浙江省| 高平市| 偃师市| 普格县| 长子县| 崇仁县| 广河县| 峨山|