Android簡單圖片瀏覽器,

來源:互聯網
上載者:User

Android簡單圖片瀏覽器,

 效果如下:

      

   

代碼編寫如下:

Crize_demo\app\src\main\res\layout\activity_main.xml

1 <!--定義一個線性布局-->2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"3     xmlns:tools="http://schemas.android.com/tools"4     android:id="@+id/root"5     android:layout_width="match_parent"6     android:layout_height="match_parent"7     android:orientation="vertical">8 9 </LinearLayout>

Crize_demo\app\src\main\java\com\ly\crize_demo\MainActivity.java

 1 public class MainActivity extends AppCompatActivity { 2     //定義一個訪問圖片的數組 3     int[] images = new int[]{ 4             R.mipmap.create_hz, 5             R.mipmap.create_jj, 6             R.mipmap.create_pic, 7             R.mipmap.create_tz, 8             R.mipmap.create_wz 9     };10     int currentImg =0;11 12     @Override13     protected void onCreate(Bundle savedInstanceState) {14         super.onCreate(savedInstanceState);15         setContentView(R.layout.activity_main);16         //擷取LinearLayout的版面配置容器17         LinearLayout main = (LinearLayout) findViewById(R.id.root);18         //程式建立ImageView組件19         final ImageView image = new ImageView(this);20         //將ImageView組件放到LinearLayout容器當中21         main.addView(image);22         //初始化顯示第一張圖片23         image.setImageResource(images[0]);24         //設定圖片的點擊事件25         image.setOnClickListener(new View.OnClickListener() {26             @Override27             public void onClick(View v) {28                 if (currentImg >=4) {29                     currentImg =-1;30                 }31                 //改變ImageView裡面顯示的圖片32                 image.setImageResource(images[++currentImg]);33             }34         });35     }36 37 38 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.