2014-11-21 52 views
0

我在我的项目中有30个图像,我想在单个图像视图中使用下一个和上一个按钮(如幻灯片放映)显示其中的10个图像,我该怎么做?谢谢!在List,然后:(R.drawable.xxx如):显示下一个或上一个图像onClick Android

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

首先清除您的问题。 – 2014-11-21 11:02:09

+0

http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/ – 2014-11-21 11:05:07

回答

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> 

这应该做的工作!

+0

邀请我聊天请 – 2014-11-21 11:35:26

+0

请加入:http://chat.stackoverflow.com/rooms/65359/imagechange – Machado 2014-11-21 11:41:09

+0

你好那里的朋友,我问这里另一个简单的问题http://stackoverflow.com/questions/27073636/give-stroke-to-text-view-text-android-hack/27074129#27074129如果你需要一些时间并回答它,我会很高兴。谢谢! – 2014-11-22 05:00:40

0

定义您的影像地址

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

看到我的更新回答! – 2014-11-21 11:14:21

+0

请邀请我聊天 – 2014-11-21 11:22:57

相关问题