Android中的圖片查看器

來源:互聯網
上載者:User

標籤:android   圖形   

       本案例,使用Eclipse來開發Android2.1版本的圖片查看器。

1)首先,開啟Eclipse,建立一個Android2.1版本的項目ShowTu,開啟res/values中目錄下的strings.xml,將其中代碼替換成一下代碼:

     路徑:ShowTu/res/valus/string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, ShowTuActivity!</string>    <string name="app_name">ShowTu</string><string name="pre">上一副</string><string name="after">下一副</string></resources>


2)開啟res/layout檔案下的main.xml,布局檔案中有兩個按鈕控制項和一個ImageView控制項。其中的代碼如下:

       路徑:ShowTu/res/layout/main.xml

<?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" ><!-- 水平線性布局 --><LinearLayout     android:id="@+id/LinearLayout01" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_gravity="center" android:layout_height="wrap_content"> <!-- 按鈕控制項 --><Button android:text="@string/pre" android:id="@+id/Button01" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button><Button android:text="@string/after" android:id="@+id/Button02" android:textColor="#000000" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button></LinearLayout><ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> </ImageView>    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    </LinearLayout>


3)接著,主Activity檔案ShowTuActivity.java,代碼如下:

     路徑:src/com.tu.ui/ShowTuActivity.java

package com.tu.ui;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.Toast;public class ShowTuActivity extends Activity {    /** Called when the activity is first created. */ImageView iv;//聲明ImageView控制項int count;//計數器int drawableIds[]={//圖片Id數組R.drawable.pic0,R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5,R.drawable.pic6,R.drawable.pic7,R.drawable.pic8,R.drawable.pic9};    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);//設定布局        iv=(ImageView)findViewById(R.id.ImageView01);//擷取ImageView的引用        iv.setImageResource(R.drawable.pic0);//設定映像        Button pre_btn=(Button)findViewById(R.id.Button01);//擷取前一個按鈕的引用        pre_btn.setOnClickListener(//添加監聽器        new OnClickListener()        {@Overridepublic void onClick(View v){if(count>0){iv.setImageResource(drawableIds[--count]);//設定映像}else{Toast.makeText(ShowTuActivity.this,"這是第一幅圖片",Toast.LENGTH_SHORT).show();}}        }        );        Button after_btn=(Button)findViewById(R.id.Button02);//擷取下一個按鈕的引用        after_btn.setOnClickListener(//添加監聽器        new OnClickListener()        {@Overridepublic void onClick(View v){if(count<drawableIds.length-1){iv.setImageResource(drawableIds[++count]);//設定映像}else{Toast.makeText(ShowTuActivity.this,"這是最後一幅圖片",Toast.LENGTH_SHORT).show();}}        }        );    }}

效果如下:

圖(1)

圖(2)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.