This article analyzes the usage of ImageView in Android. Share it for your reference, as follows:
You may have played the card guessing game before. Here we use this small game to illustrate the usage of ImageView.
First, three cards are introduced in res/drawable: Plum Blossom 7, Plum Blossom 8, and Plum Blossom 9.
Then configure a TextView, three ImageViews and a Button in res/layout/
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android: android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ImageView android: android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
The program looks like this:
import ; import ; import ; import ; import ; import ; import ; public class A04Activity extends Activity { private ImageView iv07,iv08,iv09; private TextView tv; private Button b; private int[] s={ .puke07, .puke08, .puke09 }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); iv07=(ImageView)findViewById(.iv07); iv08=(ImageView)findViewById(.iv08); iv09=(ImageView)findViewById(.iv09); tv=(TextView)findViewById(); b=(Button)findViewById(); randon(); //The following uses the OnClickListener() method of ImageView to react differently to different cards selected (new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub (getResources().getDrawable(s[0])); (getResources().getDrawable(s[1])); (getResources().getDrawable(s[2])); (100); //Dark the cards that have no choice (100); if(s[0]==.puke08){ //If the card you choose is Plum Blossom 8, you can guess it right ("Congratulations, guessed it right!!!"); } else{ ("Dear, I guessed wrong, should I do it again?"); } } }); (new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub (getResources().getDrawable(s[0])); (getResources().getDrawable(s[1])); (getResources().getDrawable(s[2])); (100); (100); if(s[1]==.puke08){ ("Congratulations, guessed it right!!!"); } else{ ("Dear, I guessed wrong, should I do it again?"); } } }); (new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub (getResources().getDrawable(s[0])); (getResources().getDrawable(s[1])); (getResources().getDrawable(s[2])); (100); (100); if(s[2]==.puke09){ ("Congratulations, guessed it right!!!"); } else{ ("Dear, I guessed wrong, should I do it again?"); } } }); (new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub ("Guess where the 8 plum blossoms are"); (getResources().getDrawable(.puke00)); //Show the back of the playing cards at the beginning (getResources().getDrawable(.puke00)); (getResources().getDrawable(.puke00)); (255);// (255); (255); randon(); } }); } //randon method is to perform random shuffles public void randon(){ for(int i=0;i<;i++){ int tmp=s[i]; int a=(int)(()*2); s[i]=s[a]; s[a]=tmp; } } }
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Summary of the usage of basic Android components"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.