如何获取当前显示的图像位置?

时间:2011-12-12 07:31:44

标签: java android android-imageview

我想显示前后病房的图像。像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;
             }
            }   

    });

}}            

它向前工作正常,但当我向后点击它不起作用。我在哪里弄错了?有人能告诉我吗?

提前致谢。

2 个答案:

答案 0 :(得分:3)

请改变这样

int choice1=--j;

编辑: 这里的问题是在执行此代码后int choice1=j--;,choice1包含值6,这不在切换案例中

答案 1 :(得分:1)

您好,现在代码工作正常。我的代码是

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>