2012-02-07 3 views
0

나는 안드로이드 ImageView에 의심의 여지가있다. Drawable 폴더에 이미지를 표시하는 샘플 응용 프로그램을 만들고 있습니다. 처음에는 이미지를 표시하기 위해 GalleryviewImageview을 사용했습니다. GalleryView에서 이미지를 클릭하면 ImageView에 해당 이미지가 표시됩니다. 이제 내가 전면 버튼을 클릭하면, 내 res/drawable 폴더에 10 개 개의 이미지로 설정되어 enter image description here안드로이드에서 해당 버튼을 클릭하면 앞으로 또는 뒤로 움직일 수 있습니까?

이미지가 해당 버튼을 클릭 사이에 표시되며, 다음과 같이 구현하고자, ImageView의 이미지가 표시됩니다 앞으로, 뒤로 버튼을 클릭하면 ImageView의 이미지가 뒤로 이동합니다. 이렇게 할 수 있습니다.

나는 다음과 같은 코드를 사용하지만 이미지가 표시되지 않습니다

main.xml에 :

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="fill_parent" 
     android:layout_height="333dp" 
     android:src="@drawable/ic_launcher" /> 

    <Gallery 
     android:id="@+id/galleryView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/backBTN" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:text="B" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_centerVertical="true" 
      android:text="imagePosition" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

     <Button 
      android:id="@+id/frontBTN" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/backBTN" 
      android:layout_alignBottom="@+id/backBTN" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:text="F" /> 
    </RelativeLayout> 

</LinearLayout> 

내 자바 코드 MainActivity.jav A는,

package com.test.button.wallpaper; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Button; 
import android.widget.Gallery; 
import android.widget.ImageView; 

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    Button front, back; 
    ImageView mImage; 
    Gallery mGallery; 
    int image_postion; 
    private int[] gal = { R.drawable.img_2, R.drawable.img_3, R.drawable.img_4, 
      R.drawable.img_5 }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     front = (Button) findViewById(R.id.frontBTN); 
     back = (Button) findViewById(R.id.backBTN); 
     mImage = (ImageView) findViewById(R.id.imageView); 
     mGallery = (Gallery) findViewById(R.id.galleryView); 
     mGallery.setAdapter(new ImageAdapter(this)); 
     mGallery.setVisibility(View.GONE); 
     mGallery.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int loc, 
        long arg3) { 
       // TODO Auto-generated method stub 
       image_postion = loc; 
       System.out.println("Inside the Gallery"); 

      } 
     }); 

     back.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       int position = mGallery.getSelectedItemPosition(); 
       if (position < mGallery.getCount() - 1) 
        mGallery.setSelection(position + 1); 

      } 
     }); 

     front.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 

       int position = mGallery.getSelectedItemPosition(); 
       if (position >= 1) 
        mGallery.setSelection(position - 1); 

      } 
     }); 
    } 

    public class ImageAdapter extends BaseAdapter { 
     private Context ctx; 
     int imageBackground; 

     public ImageAdapter(Context c) { 
      ctx = c; 
      // TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1); 
      // imageBackground = ta.getResourceId(
      // R.styleable.Gallery1_android_galleryItemBackground, 1); 
      // ta.recycle(); 
     } 

     @Override 
     public int getCount() { 

      return gal.length; 
     } 

     @Override 
     public Object getItem(int arg0) { 

      return arg0; 
     } 

     @Override 
     public long getItemId(int arg0) { 

      return arg0; 
     } 

     @Override 
     public View getView(int arg0, View arg1, ViewGroup arg2) { 
      ImageView iv = new ImageView(ctx); 
      iv.setImageResource(gal[arg0]); 
      iv.setScaleType(ImageView.ScaleType.FIT_XY); 
      iv.setLayoutParams(new Gallery.LayoutParams(150, 120)); 
      iv.setBackgroundResource(imageBackground); 
      return iv; 
     } 

    } 
} 

답변

2

예, 그냥 세트로 선택 위치를

btnNext.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View view) 
    { 
      int position=gallery.getSelectedIndex(); 
      if(position<gallery.getCount()-1) 
      gallery.setSelection(position+1); 
    } 
}); 


btnPrevious.setOnClickListener(new View.OnClickListener() 
{ 
    public void onClick(View view) 
    { 
      int position=gallery.getSelectedIndex(); 
      if(position>=1) 
       gallery.setSelection(position-1); 
    } 
}); 
+0

: 내가) (난 getSelectedIndex에 오류가있어이 코드를 삽입 할 때; – Aerrow

+1

getSelectedItemPosition()으로 methPod 변경 – jeet

1

는 내가 같은 일이 내 애플 리케이션 perfectlyin 작동합니까

이 XML

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:ns="http://schemas.android.com/apk/res/com.colors.abc" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" >   
    <RelativeLayout 
     android:id="@+id/item" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" >   
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"    
     android:background="@drawable/image" /> 
    <Button 
     android:id="@+id/back" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:text="small" /> 
    <Button 
     android:id="@+id/forword" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:text="foeword" /> 
    </RelativeLayout> 
</RelativeLayout> 

및 자바

  setimage(present);   
     back.setEnabled(false);  
     back.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) {    

      if (past==1) { 
       back.setEnabled(false); 
      }else { 
       back.setEnabled(true); 
      } 
      if(past<=10) forword.setEnabled(true); 

      setimage(past);      

      }     
      }); 

       forword.setOnClickListener(new OnClickListener() {    
      public void onClick(View v) {     

       if(future==10){     
        forword.setEnabled(false); 
       }else { 
        forword.setEnabled(true); 
       } 
       if(future>=2) { 
        back.setEnabled(true); 
       }     
       setimage(future);      
      }    
     });  
관련 문제