2011-12-12 6 views
4

앞뒤 병상을 표시하고 싶습니다. Windows 그림 및 팩스 뷰어와 유사합니다. 내 코드입니다.현재 표시된 이미지 위치를 얻는 방법은 무엇입니까?

package com.my.imagechange; 

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

    public class ImageChangeDemo extends Activity 
    { 
/** Called when the activity is first created. */ 

// private Gallery gallery; 
private ImageView imgView; 
int i=0; 
int j=6; 
ImageButton btrt,btlt; 
Button bt1,bt2; 
private Integer[] Imgid ={R.drawable.androidlogo,R.drawable.androids, R.drawable.cool, R.drawable.cupcake2009, R.drawable.donut2009, R.drawable.eclair2009}; 
int imglength=Imgid.length; 
//System.out.println(imglength); 
@Override 

public void onCreate(Bundle savedInstanceState) 
{ 

    super.onCreate(savedInstanceState); 

     setContentView(R.layout.imagechange); 
     System.out.println(imglength); 

     btlt=(ImageButton)findViewById(R.id.bt1); 
     btrt=(ImageButton)findViewById(R.id.bt2); 


     imgView = (ImageView)findViewById(R.id.ImageView01); 
    imgView.setImageResource(Imgid[0]); 
    btlt.setOnClickListener(new View.OnClickListener() 
     { 

      @Override 
      public void onClick(View v) 
      { 

       int choice1=j--; 
       switch(choice1) 
       { 
       case 5:imgView.setImageResource(Imgid[5]); 
         break; 
       case 4:imgView.setImageResource(Imgid[4]); 
         break; 
       case 3:imgView.setImageResource(Imgid[3]); 
         break; 
       case 2:imgView.setImageResource(Imgid[2]); 
         break; 
       case 1:imgView.setImageResource(Imgid[1]); 
         break; 

       } 

      } 
     }); 
     btrt.setOnClickListener(new View.OnClickListener() 
     { @SuppressWarnings("unused") 
     @Override 


      public void onClick(View v) 
      { 
       int choice2=i++; 
      switch(choice2) 
      { 
      case 1:imgView.setImageResource(Imgid[1]); 
        break; 
      case 2:imgView.setImageResource(Imgid[2]); 
        break; 
      case 3:imgView.setImageResource(Imgid[3]); 
        break; 
      case 4:imgView.setImageResource(Imgid[4]); 
        break; 
      case 5:imgView.setImageResource(Imgid[5]); 
        break; 
      } 
      } 

    }); 

}}    

잘 처리되지만 앞으로 누르면 제대로 작동하지 않습니다. 내 실수는 어디에서 했습니까? 누가 말해 줄 수 있니?

미리 감사드립니다.

답변

1

안녕하세요 이제 코드는 미세하게 작동하는 스위치 케이스에없는 int choice1=j--; choice1 값 6을 포함하는이 코드의 실행 후입니다 . 내 코드는

package com.my.imagechange; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.ImageView; 

public class ImageChange extends Activity 
{ 

private Gallery gallery; 
private ImageView imgView; 
private ImageButton btlt,btrt; 
private Integer[] Imgid= { 
     R.drawable.androidlogo, R.drawable.androids, R.drawable.cool, R.drawable.cupcake2009, R.drawable.donut2009, R.drawable.eclair2009}; 
int imglength=Imgid.length; 

int img_position; 
int img_minus; 
int img_plus; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    imgView = (ImageView)findViewById(R.id.ImageView01);  
    imgView.setImageResource(Imgid[0]); 

    btlt=(ImageButton)findViewById(R.id.bt1); 
    btrt=(ImageButton)findViewById(R.id.bt2); 

    System.out.println("----->"+imglength +"----->"); 
    btlt.setOnClickListener(new View.OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 

      // TODO Auto-generated method stub 
      btrt.setEnabled(true); 
      System.out.println("left button has been clicked"); 
      System.out.println("position: "+img_position); 
      img_minus=--img_position; 
      System.out.println("minus:"+img_minus); 
      imgView.setImageResource(Imgid[img_minus]); 
      //imgView.setBackgroundDrawable(getResources().getDrawable(id)); 
      if(img_minus==0) 
      { 
       btlt.setEnabled(false); 
      } 


     } 
    }); 

    btrt.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 

      // TODO Auto-generated method stub 
      btlt.setEnabled(true); 
      System.out.println("right button has been clicked"); 
      System.out.println("position: "+img_position); 
      img_plus=++img_position; 
      System.out.println("plus: "+img_plus); 
      imgView.setImageResource(Imgid[img_plus]); 
      if(img_plus==5) 
      { 
       btrt.setEnabled(false); 
      } 

     } 
    }); 


} } 

main.xml에

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout android:id="@+id/LinearLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:background="#ffffff"> 
    <ImageView android:id="@+id/ImageView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:background="@drawable/androidlogo"/> 

    <RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="bottom" 
    android:layout_gravity="fill_horizontal" 
    > 
    <ImageButton android:id="@+id/bt1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/butleft" 
    android:layout_alignParentLeft="true"> 
    </ImageButton> 
    <ImageButton android:id="@+id/bt2" 
    android:src="@drawable/butright" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_alignParentRight="true"> 
    </ImageButton> 
    </RelativeLayout> 
    </LinearLayout> 
3

int choice1=--j; 

편집과 같이 변경하십시오 여기에 문제가

관련 문제