我一直在使用毕加索从drawable加载我的图像,所有工作都很好,只是第三张图像不见了,我得到了一个空白的屏幕。一段时间后,图像加载。我无法理解为什么会出现这种图像的延迟。感谢任何帮助和解释。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.home);
viewPager = (ViewPager) findViewById(R.id.pager);
final ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
Button yourButton = (Button)findViewById(R.id.button_previous);
yourButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(getItem(-1), true); //getItem(-1) for prev
}
protected int getItem(int i) {
// TODO Auto-generated method stub
return viewPager.getCurrentItem() + i;
}
});
Button nextButton = (Button)findViewById(R.id.button_next);
nextButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(getItem(+1), true); //getItem(-1) for prev
}
protected int getItem(int i) {
// TODO Auto-generated method stub
return viewPager.getCurrentItem() + i;
}
});
facebookButton = (Button)findViewById(R.id.fb);
facebookButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
try {
//Write file
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
String filename = "bitmap.png";
FileOutputStream stream = getApplicationContext().openFileOutput(filename, Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
//Cleanup
stream.close();
bmp.recycle();
//Pop intent
Intent in1 = new Intent(getApplicationContext(), FacebookUpload.class);
in1.putExtra("image", filename);
startActivity(in1);
} catch (Exception e) {
e.printStackTrace();
}
}
});
Button btnrand = (Button)findViewById(R.id.button2);
btnrand.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view = (View) viewPager.findViewWithTag("myview" + viewPager.getCurrentItem());
Log.d("Inside rand", "i am here");
try{
//You can use any view of your View instead of TextView
Random rand = new Random();
int rndInt = rand.nextInt(141) + 1;
String drawableName = "c"+ rndInt;
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
((ImageView) view).setImageResource(resID);
}
catch (Exception e)
{
}
}
});
}
class ImagePagerAdapter extends PagerAdapter {
public int[] mImages = new int[] {
R.drawable.c1 ,
R.drawable.c2 ,
R.drawable.c3 ,
R.drawable.c4 ,
R.drawable.c5 ,
R.drawable.c6 ,
R.drawable.c7 ,
R.drawable.c8 ,
R.drawable.c9 ,
R.drawable.c11 ,
R.drawable.c12 ,
R.drawable.c13 ,
R.drawable.c14 ,
R.drawable.c15 ,
R.drawable.c16 ,
R.drawable.c17 ,
R.drawable.c18 ,
R.drawable.c19 ,
R.drawable.c20 ,
R.drawable.c21 ,
R.drawable.c22 ,
R.drawable.c23 ,
R.drawable.c24 ,
R.drawable.c25 ,
R.drawable.c26 ,
R.drawable.c27 ,
R.drawable.c28 ,
R.drawable.c29 ,
R.drawable.c30 ,
R.drawable.c31 ,
R.drawable.c32 ,
R.drawable.c33 ,
R.drawable.c34 ,
R.drawable.c35 ,
R.drawable.c36 ,
R.drawable.c37 ,
R.drawable.c38 ,
R.drawable.c39 ,
R.drawable.c40 ,
R.drawable.c41 ,
R.drawable.c42 ,
R.drawable.c43 ,
R.drawable.c44 ,
R.drawable.c45 ,
R.drawable.c46 ,
R.drawable.c47 ,
R.drawable.c48 ,
R.drawable.c49 ,
R.drawable.c50 ,
R.drawable.c51 ,
R.drawable.c52 ,
R.drawable.c53 ,
R.drawable.c54 ,
R.drawable.c55 ,
R.drawable.c56 ,
R.drawable.c57 ,
R.drawable.c58 ,
R.drawable.c59 ,
R.drawable.c60 ,
R.drawable.c61 ,
R.drawable.c62 ,
R.drawable.c63 ,
R.drawable.c64 ,
R.drawable.c65 ,
R.drawable.c66 ,
R.drawable.c67 ,
R.drawable.c68 ,
R.drawable.c69 ,
R.drawable.c70 ,
R.drawable.c71 ,
R.drawable.c72 ,
R.drawable.c73 ,
R.drawable.c74 ,
R.drawable.c75 ,
R.drawable.c76 ,
R.drawable.c77 ,
R.drawable.c78 ,
R.drawable.c79 ,
R.drawable.c80 ,
R.drawable.c81 ,
R.drawable.c82 ,
R.drawable.c83 ,
R.drawable.c84 ,
R.drawable.c85 ,
R.drawable.c86 ,
R.drawable.c87 ,
R.drawable.c88 ,
R.drawable.c89 ,
R.drawable.c90 ,
R.drawable.c91 ,
R.drawable.c92 ,
R.drawable.c93 ,
R.drawable.c94 ,
R.drawable.c95 ,
R.drawable.c96 ,
R.drawable.c97 ,
R.drawable.c98 ,
R.drawable.c99 ,
R.drawable.c100 ,
R.drawable.c101 ,
R.drawable.c102 ,
R.drawable.c104 ,
R.drawable.c105 ,
R.drawable.c106 ,
R.drawable.c107 ,
R.drawable.c108 ,
R.drawable.c109 ,
R.drawable.c110 ,
R.drawable.c111 ,
R.drawable.c112 ,
R.drawable.c113 ,
R.drawable.c114 ,
R.drawable.c115 ,
R.drawable.c116 ,
R.drawable.c117 ,
R.drawable.c118 ,
R.drawable.c119 ,
R.drawable.c120 ,
R.drawable.c121 ,
R.drawable.c122 ,
R.drawable.c123 ,
R.drawable.c124 ,
R.drawable.c125 ,
R.drawable.c126 ,
R.drawable.c127 ,
R.drawable.c128 ,
R.drawable.c129 ,
R.drawable.c130 ,
R.drawable.c131 ,
R.drawable.c132 ,
R.drawable.c133 ,
R.drawable.c134 ,
R.drawable.c135 ,
R.drawable.c136 ,
R.drawable.c137 ,
R.drawable.c138 ,
R.drawable.c139 ,
R.drawable.c140 ,
R.drawable.c141
};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Context context =getApplicationContext();
final ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(
R.dimen.padding_medium);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
Picasso.with(context).load(mImages[position]).resize(350, 350)
.centerCrop().into(imageView); // use ImageView
((ViewPager) container).addView(imageView, 0);
imageView.setTag("myview" + position);
return imageView;
}
@Override
public void destroyItem(View container, int position,
Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView (viewPager.getChildAt(position));
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
View view = (View) viewPager.findViewWithTag("myview" + viewPager.getCurrentItem());
super.onDestroy();
((ImageView) view).setImageDrawable(null);
}
}
这是我的代码