Android中ImageView用法實(shí)例分析
本文實(shí)例分析了Android中ImageView用法。分享給大家供大家參考,具體如下:
猜牌游戲大家可能以前都玩過,這里我們用這個(gè)小游戲來說明ImageView的用法。
首先,在res/drawable中引入三張牌:分別是梅花7,梅花8,梅花9
然后在res/layout/main.xml中配置一個(gè)TextView,三個(gè)ImageView以及一個(gè)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()方法對選擇的不同牌做不同的反應(yīng)
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("親,猜錯(cuò)了,要不要再來一次?");
}
}
});
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("親,猜錯(cuò)了,要不要再來一次?");
}
}
});
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("親,猜錯(cuò)了,要不要再來一次?");
}
}
});
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));
//剛開始的時(shí)候顯示撲克牌的背面
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方法是進(jìn)行隨機(jī)地洗牌
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;
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android ImageView隨手勢變化動(dòng)態(tài)縮放圖片
- Android使用控件ImageView加載圖片的方法
- Android 圓角 ImageView類可設(shè)置弧度(代碼簡單)
- 圖文講解Android的ImageView類中的ScaleType屬性設(shè)置
- Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
- Android手勢滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小
- Android實(shí)現(xiàn)手勢控制ImageView圖片大小
- Android ImageView Src 和Background 區(qū)別
相關(guān)文章
Android13?加強(qiáng)Intent?filters?的安全性
這篇文章主要介紹了Android13?加強(qiáng)Intent?filters?的安全性,文章基于Android13?展開Intent?filters?安全性加強(qiáng)的詳細(xì)介紹,需要的小伙伴可以參考一下2022-05-05
Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問題解決方法
這篇文章主要介紹了Android輸入法與表情面板切換時(shí)的界面抖動(dòng)問題解決方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android文本輸入框(EditText)輸入密碼時(shí)顯示與隱藏
這篇文章主要介紹了Android文本輸入框(EditText)輸入密碼時(shí)顯示與隱藏的方法和示例,需要的朋友可以參考下2014-12-12
Android onTouchEvent事件中onTouch方法返回值(介紹)
下面小編就為大家?guī)硪黄狝ndroid onTouchEvent事件中onTouch方法返回值(介紹)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android編程實(shí)現(xiàn)對話框形式進(jìn)度條功能示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)對話框形式進(jìn)度條功能,結(jié)合具體實(shí)例形式分析了Android對話框形式進(jìn)度條的功能與布局相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09

