你好,游客 登录 注册 搜索
背景:
阅读新闻

Android:ImageView应用之图片浏览器

[日期:2014-02-06] 来源:Linux社区  作者:orangebook [字体:大 中 小]

一.纯显示图片:

引言:

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

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

效果图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

关键代码片段:

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>
linux
相关资讯       ImageView 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款