在Android上显示下一张或上一张图片

时间:2014-11-21 11:00:26

标签: android imageview

我的项目中有30个图像,我想在单个图像视图中使用下一个和上一个按钮(如幻灯片)显示其中的10个图像我该怎么做?谢谢!

previousButton = (Button) findViewById(R.id.backButton);
    previousButton.setOnClickListener(this);

    nextButton = (Button) findViewById(R.id.nextButton);
    nextButton.setOnClickListener(this);

set image in onclick method

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view == previousButton) {
        --positionOfSelectedImage;
        // set background image of
    } else if (view == nextButton) {
        ++positionOfSelectedImage;
    }

imageToBeSet.setImageURI(Uri.parse(absolutepathOfImage));
}

2 个答案:

答案 0 :(得分:2)

有一种更简单的方法可以做到这一点。

试试这段代码:

package com.example.jre;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnClickListener {

    Button btprevious, btnext;
    ImageView myImage;
    public int i = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btprevious = (Button) findViewById(R.id.button1);
btnext = (Button) findViewById(R.id.button2);
myImage = (ImageView) findViewById(R.id.myImage);

btprevious.setOnClickListener(this);
btnext.setOnClickListener(this);

}

@Override
public void onClick(View v) {

switch (v.getId()){
case R.id.button1:
i++;

if(i==2) // switch to 11 because you got 10 images
{
    i=1; // switch to 10, same reason
}

changeImage();
break;

case R.id.button2:
i--;

if(i==-1)
{
    i=0; // you can leave it this way or improve it later
}

changeImage();
break;
}
}

public void changeImage()
{
    myImage = (ImageView) findViewById(R.id.myImage);

switch(i)
{

case 0:
myImage.setImageResource(R.drawable.firstimage);
break;

case 1:
myImage.setImageResource(R.drawable.secondimage);
break;
// and then it goes further
}
}

}

在你的xml文件中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.jre.MainActivity" >

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/myImage"
        android:text="Next" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/myImage"
        android:layout_marginTop="32dp"
        android:layout_toLeftOf="@+id/myImage"
        android:text="Previous" />

</RelativeLayout>

那应该做的工作!

答案 1 :(得分:0)

List中定义您的图片地址(例如:R.drawable.xxx),然后:

List <String> imagePath = new ArrayList<String> ();
imagePath.add("image1Path");
imagePath.add("image2Path");
...
imagePath.add("image30Path");
previousButton = (Button) findViewById(R.id.backButton);
previousButton.setOnClickListener(this);

nextButton = (Button) findViewById(R.id.nextButton);
nextButton.setOnClickListener(this);
myImageView = (ImageView) findViewById(R.id.imageView);

int index = 0;
//show first image in list
myImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath.get(0)));


@Override
public void onClick(View view) {
// TODO Auto-generated method stub
if (view.getId() == R.id.backButton) {
        --index;
        // set background image of
    } else if (view.getId() == R.id.nextButton) {
        ++index;
    }

myImageView.setImageBitmap(BitmapFactory.decodeFile(imagePath.get(index)));
}
相关问题