【问题标题】:show next or previous image onClick Android在点击 Android 时显示下一张或上一张图片
【发布时间】:2014-11-21 11:00:25
【问题描述】:

我的项目中有 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));
}

【问题讨论】:

标签: android imageview


【解决方案1】:

有一个更简单的方法来做到这一点。

试试这个代码:

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>

这应该做的工作!

【讨论】:

【解决方案2】:

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

【讨论】:

  • 请邀请我聊天
【解决方案3】:

为图像创建数组以存储可绘制的所有图像以及您的后退和下一步按钮以及要预览所有图像的图像视图,并从零开始 int n

ImageView previewImg,backImg,nextImg;
int[] mario = new int[]{R.drawable.nature_07,R.drawable.nature_08,R.drawable.nature_09,
        R.drawable.nature_10,R.drawable.nature_11,R.drawable.nature_12,
        R.drawable.nature_13,R.drawable.nature_14
};
int n =0;

**in the oncreate method do it by the following code**
 previewImg = findViewById(R.id.previewImg);
    backImg = findViewById(R.id.backBtn);
    nextImg = findViewById(R.id.nextBtn);

    //your default image to preview when ist time its is open
    previewImg.setImageResource(R.drawable.nature_12);

    nextImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         //the n value is 7 cause the array size is 7
            if(n < 7)
                n++;
            previewImg.setImageResource(mario[n]);
        }
    });
    backImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            previewImg.setImageResource(mario[n]);
            if(n>0)
                n--;
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 2021-12-14
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多