歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android:ImageView應用之圖片浏覽器

Android:ImageView應用之圖片浏覽器

日期:2017/3/1 9:48:49   编辑:Linux編程

一.純顯示圖片:

引言:

讀者在做這個東西的時候,需要自己把圖片在源程序中導入。

讀者要注意:所有導入的圖片之前,圖片的命名只可以是小寫英文和數字。

效果圖

關鍵代碼片段:

imageView.setOnClickListener(new OnClickListener()

{

public void onClick(View v)

{ imageView.setImageResource(images[++currentImg%images.length]);

}

});

其中加了黃色背景的代碼循環顯示圖片。

全部代碼:

import Android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends Activity {
    
    int[] images=new int[]{R.drawable.lrp1,
                           R.drawable.lrp2,
                           R.drawable.ls,
                           R.drawable.mr};
    int currentImg=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //LinearLayout main= (LinearLayout) findViewById(R.id.root);
        final ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageResource(images[0]);
        imageView.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) {
                // TODO Auto-generated method stub
                imageView.setImageResource(images[++currentImg%images.length]);
            }
        });
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


View Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    >
    <ImageView 
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#00f"
        android:layout_marginTop="10dp"/>
</LinearLayout>
Copyright © Linux教程網 All Rights Reserved