Android中ImageView用法實(shí)例分析
本文實(shí)例分析了Android中ImageView用法。分享給大家供大家參考,具體如下:
猜牌游戲大家可能以前都玩過(guò),這里我們用這個(gè)小游戲來(lái)說(shuō)明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()方法對(duì)選擇的不同牌做不同的反應(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); //對(duì)沒(méi)有選擇的牌做灰暗處理 iv09.setAlpha(100); if(s[0]==R.drawable.puke08){ //如果選擇的牌是梅花8的話(huà)就猜對(duì)了 tv.setText("恭喜你,猜對(duì)了?。?!"); } else{ tv.setText("親,猜錯(cuò)了,要不要再來(lái)一次?"); } } }); 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("恭喜你,猜對(duì)了?。。?); } else{ tv.setText("親,猜錯(cuò)了,要不要再來(lái)一次?"); } } }); 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("恭喜你,猜對(duì)了?。?!"); } else{ tv.setText("親,猜錯(cuò)了,要不要再來(lái)一次?"); } } }); 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)); //剛開(kāi)始的時(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)容感興趣的讀者可查看本站專(zhuān)題:《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android使用CircleImageView實(shí)現(xiàn)圓形頭像的方法
- Android ImageView隨手勢(shì)變化動(dòng)態(tài)縮放圖片
- Android使用控件ImageView加載圖片的方法
- Android 圓角 ImageView類(lèi)可設(shè)置弧度(代碼簡(jiǎn)單)
- 圖文講解Android的ImageView類(lèi)中的ScaleType屬性設(shè)置
- Android實(shí)現(xiàn)ImageView圖片雙擊放大及縮小
- Android手勢(shì)滑動(dòng)實(shí)現(xiàn)ImageView縮放圖片大小
- Android實(shí)現(xiàn)手勢(shì)控制ImageView圖片大小
- Android ImageView Src 和Background 區(qū)別
相關(guān)文章
Android?LineChart繪制折線(xiàn)圖的示例詳解
這篇文章主要為大家想想介紹了Android?RecyclerLineChart實(shí)現(xiàn)繪制折線(xiàn)圖的相關(guān)資料,有需要的朋友可以借鑒參考下,希望能夠有所幫助2023-03-03Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)隨手指移動(dòng)的小球,隨著手指觸摸移動(dòng)而移動(dòng)的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)
這篇文章主要介紹了Android開(kāi)發(fā)之a(chǎn)ctiviti節(jié)點(diǎn)跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2016-04-04Flutter?彈性布局基石flex算法flexible示例詳解
這篇文章主要為大家介紹了Flutter?彈性布局基石flex算法flexible示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Android繪制旋轉(zhuǎn)動(dòng)畫(huà)方法詳解
這篇文章主要介紹了Android如何采用RotateAnimation繪制一個(gè)旋轉(zhuǎn)動(dòng)畫(huà),文中的實(shí)現(xiàn)方法講解詳細(xì),感興趣的小伙伴可以跟隨小編一起試一試2022-01-01Android如何用自定義View實(shí)現(xiàn)雪花效果
這篇文章主要介紹了Android如何用自定義View實(shí)現(xiàn)雪花效果,對(duì)特效感興趣的同學(xué)可以參考下2021-04-04Kotlin+buildSrc更好的管理Gradle依賴(lài)譯文
這篇文章主要為大家介紹了Kotlin+buildSrc更好的管理Gradle依賴(lài)譯文及示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06Android TextView字體顏色設(shè)置方法小結(jié)
這篇文章主要介紹了Android TextView字體顏色設(shè)置方法,結(jié)合實(shí)例形式總結(jié)分析了Android開(kāi)發(fā)中TextView設(shè)置字體顏色的常用技巧,需要的朋友可以參考下2016-02-02