我在drawable文件夹中有两个图像。我想以全屏显示它们,如果你滑动,那么下一张图像应该会出现。这是我正在使用的代码:
import com.ssb.crackssb.R;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class FullScreenImageAdapter extends PagerAdapter {
private Activity _activity;
private int[] _imagePaths= {R.drawable.piq1,R.drawable.piq2};
private LayoutInflater inflater;
// constructor
public FullScreenImageAdapter(Activity activity,
int[] imagePaths) {
this._activity = activity;
this._imagePaths = imagePaths;
}
@Override
public int getCount() {
return this._imagePaths.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imgDisplay;
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewLayout = inflater.inflate(R.layout.piq, container,
false);
imgDisplay = (ImageView) viewLayout.findViewById(R.id.imageview);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths(position), options);
imgDisplay.setImageBitmap(bitmap);
// close button click event
((ViewPager) container).addView(viewLayout);
return viewLayout;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((RelativeLayout) object);
}
}
这一行给出了错误:
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths(position), options);
错误是:
The method _imagePaths(int) is undefined for the type FullScreenImageAdapter
我想从drawable文件夹中将图像加载到我的位图中。请帮帮我。
答案 0 :(得分:0)
您收到该错误的原因是因为您将_imagePaths视为方法而不是数组。 _imagePaths(position)应该是_imagePaths [position]。
你不需要乱用于创建位图。由于您在drawable目录中拥有资源,因此您只需将它们作为参考添加到ImageView:
替换
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths(position), options);
imgDisplay.setImageBitmap(bitmap);
与
imgDisplay.setImageDrawable(getResources()getDrawable(_imagePaths [位置]));
答案 1 :(得分:0)
没有必要让它如此复杂,只是简单地说:
viewLayout.setBackgroundResource(R.drawable.piq1);
修改强>
R.
对象只是一堆整数,你引用它们并不重要。代码如下:
viewLayout.setBackgroundResource(_imagePaths[position]);