Android:ImageView应用之图片浏览器


一.纯显示图片:

引言:

读者在做这个东西的时候,需要自己把图片在源程序中导入。

读者要注意:所有导入的图片之前,图片的命名只可以是小写英文和数字

效果图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

关键代码片段:

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;
    }

}


   
<?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>
  • 1
  • 2
  • 下一页

相关内容

    暂无相关文章