2015-10-31 11 views
0

我正在做一個在viewpager中顯示服務器端圖像的代碼。圖像在viewpager中無法正常顯示(持續改變其位置)

但根據我的代碼,所有的圖像都成功加載,但問題是圖像位置不是恆定的。我的意思是圖像的位置在滑動後不斷變化。

請解決我的問題。

Custom_swipe_adapter.java

public class Custom_swipe_adapter extends PagerAdapter { 
private String[] img; 
private Context ctx; 
Bitmap[] bitmap; 
ImageView imgofer; 
Bitmap b1,b2,b3,b4,b5; 
private LayoutInflater layoutInflater; 
public Custom_swipe_adapter(Context ctx,String [] img) 
{ 
    this.ctx=ctx; 
    this.img=img; 
} 
@Override 
public int getCount() { 
    return img.length; 
} 

@Override 
public boolean isViewFromObject(View view, Object object) { 
    return (view ==(LinearLayout)object); 
} 

@Override 
public Object instantiateItem(ViewGroup container, int position) { 

    layoutInflater= (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View itemview=layoutInflater.inflate(R.layout.swipe_layout,container,false); 
    bitmap=new Bitmap[img.length]; 
    imgofer= (ImageView) itemview.findViewById(R.id.swipeimg); 
    Log.v("abhi", img[position]); 


    ImageView i1=imgofer; 
    if (imgofer != null) { 
     new ImageDownloaderTask(i1).execute(img[position]); 

    } 

    imgofer.setImageBitmap(b1); 

    container.addView(itemview); 
    Log.v("TAG", "aaaaaaaaaaaa"+img[position]); 
    return itemview; 
} 

@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
    container.removeView((LinearLayout)object); 
} 

class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> { 

    private final WeakReference<ImageView> imageViewReference; 

    public ImageDownloaderTask(ImageView imageView) { 
     imageViewReference = new WeakReference<ImageView>(imageView); 
    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     try { 
      b1 = BitmapFactory.decodeStream((InputStream) new URL(params[0]).getContent()); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     b1 = Bitmap.createScaledBitmap(b1, 270, 375, true); 
     return b1; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
     if (isCancelled()) { 
      bitmap = null; 
     } 

     if (imageViewReference != null) { 
      ImageView imageView = imageViewReference.get(); 
      if (imageView != null) { 
       if (bitmap != null) { 
        imageView.setImageBitmap(b1); 
       } else { 
        Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.loadingoffer); 
        imageView.setImageDrawable(placeholder); 
       } 
      } 

     } 
    } 


} 


} 

我的圖片瀏覽:

<ImageView 
    android:id="@+id/swipeimg" 
    android:scaleType="centerCrop" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:background="@drawable/loadingoffer" /> 

我查看傳呼機:

<android.support.v4.view.ViewPager 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/slideimg"> </android.support.v4.view.ViewPager> 

回答

1

在XML文件中,轉到您的ImageView,並嘗試玩其scaleType。我覺得android:scaleType="centerCrop"可能適合你。

檢查here瞭解更多信息。

P.S.你的問題的格式非常糟糕。

+0

抱歉的格式,但我是新來的機器人,所以請幫助我 –

+0

你試過我的解決方案嗎? – YASOU

+0

是的,它不工作 –

相關問題