我正在执行用于在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>
答案 0 :(得分:1)
在您的XML文件中,转到ImageView并尝试使用其scaleType。
我认为android:scaleType="centerCrop"
可能适合您。
查看here以获取更多信息。
P.S。问题的格式非常糟糕。