Android中ImageView用法實例分析
本文實例分析了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()方法對選擇的不同牌做不同的反應(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("親,猜錯了,要不要再來一次?");
}
}
});
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;
}
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進階教程》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
- Android使用CircleImageView實現(xiàn)圓形頭像的方法
- Android ImageView隨手勢變化動態(tài)縮放圖片
- Android使用控件ImageView加載圖片的方法
- Android 圓角 ImageView類可設(shè)置弧度(代碼簡單)
- 圖文講解Android的ImageView類中的ScaleType屬性設(shè)置
- Android實現(xiàn)ImageView圖片雙擊放大及縮小
- Android手勢滑動實現(xiàn)ImageView縮放圖片大小
- Android實現(xiàn)手勢控制ImageView圖片大小
- Android ImageView Src 和Background 區(qū)別
相關(guān)文章
Android開發(fā)之a(chǎn)ctiviti節(jié)點跳轉(zhuǎn)
這篇文章主要介紹了Android開發(fā)之a(chǎn)ctiviti節(jié)點跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2016-04-04
Flutter?彈性布局基石flex算法flexible示例詳解
這篇文章主要為大家介紹了Flutter?彈性布局基石flex算法flexible示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Kotlin+buildSrc更好的管理Gradle依賴譯文
這篇文章主要為大家介紹了Kotlin+buildSrc更好的管理Gradle依賴譯文及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Android TextView字體顏色設(shè)置方法小結(jié)
這篇文章主要介紹了Android TextView字體顏色設(shè)置方法,結(jié)合實例形式總結(jié)分析了Android開發(fā)中TextView設(shè)置字體顏色的常用技巧,需要的朋友可以參考下2016-02-02

