2014-11-21 35 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

相關問題