Android:ImageView应用之图片浏览器

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

}
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>
View Code

 

二.通过使用其它控件控制图片:

 

关键代码:

next.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                image1.setImageResource(images[++currentImg%images.length]);
            }
            
        });
        OnClickListener listener=new OnClickListener()
        {
            @SuppressWarnings("deprecation")
            public void onClick(View v) 
            {
                if(v == plus)
                {
                    alpha += 20;
                    if(alpha >=255)
                    {
                        alpha=255;
                    }
                }
                else if(v==minus)
                {
                    alpha -= 20;
                    if(alpha <= 0)
                    {
                        alpha = 0;
                    }
                }
                image1.setAlpha(alpha);        
            }
            
        };
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
    }

简而言之:在监听中添加对ImageView属性的控制。

全部代码:

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;

public class MainActivity extends Activity {
    private int[] images=new int[]{R.drawable.lrp1,
                                   R.drawable.lrp2,
                                   R.drawable.ls,
                                   R.drawable.mr};
    private int currentImg=0;
    private int alpha=255;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //得到引用
        final ImageView image1 = (ImageView) findViewById (R.id.image1);
        final Button plus = (Button) findViewById(R.id.button1);
        final Button minus = (Button) findViewById(R.id.button2);
        final Button next = (Button) findViewById(R.id.button3);
        //设置监听按钮
        next.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                image1.setImageResource(images[++currentImg%images.length]);
            }
            
        });
        OnClickListener listener=new OnClickListener()
        {
            @SuppressWarnings("deprecation")
            public void onClick(View v) 
            {
                if(v == plus)
                {
                    alpha += 20;
                    if(alpha >=255)
                    {
                        alpha=255;
                    }
                }
                else if(v==minus)
                {
                    alpha -= 20;
                    if(alpha <= 0)
                    {
                        alpha = 0;
                    }
                }
                image1.setAlpha(alpha);        
            }
            
        };
        plus.setOnClickListener(listener);
        minus.setOnClickListener(listener);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
View Code
<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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="增大透明度"
            android:gravity="left" />
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="降低透明度" 
            android:gravity="left"/>
        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:text="下一张" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/image1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitCenter"
            android:src="@drawable/lrp1" />

    </LinearLayout>
</LinearLayout>
View Code 
posted @ 2014-01-29 14:29  Talbot3  阅读(834)  评论(0编辑  收藏  举报