我在ViewPager中顺利滑动有问题。我的目标是使用Volley Library从URL下载图像,并且必须在ViewPager中显示下载的图像,在从左到右或从右到左滑动时没有任何滞后问题。我附加了在扫描图像时滞后的代码。
这是我的Activity类,其中包含view pager:
ViewPagerActivity.java
public class ViewPagerActivity extends Activity {
ViewPager pager;
String[] URL;
PagerAdapter objPagerAdapter;
ArrayList<String> _arrayImagePath=new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
pager=(ViewPager)findViewById(R.id.pagerView);
// Setting the 70 images URL
URL= new String[] { "http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg",
"http://vignette3.wikia.nocookie.net/dragonball/images/7/71/PollutionAura2.png/revision/latest?cb=20101211235024",
"http://vignette2.wikia.nocookie.net/aread/images/6/6f/V02_Back.jpg/revision/latest?cb=20140501154842",
"http://vignette1.wikia.nocookie.net/tmnt/images/d/d5/Tnmt-disneyscreencaps_com-5817.jpg/revision/latest?cb=20130428155629",
"https://upload.wikimedia.org/wikipedia/commons/2/27/Sunset_at_Selsey_beach_4.jpg",
"http://img1.wikia.nocookie.net/__cb20140806200752/disney/images/d/d8/Little-mermaid-1080p-disneyscreencaps.com-782.jpg",
"http://img3.wikia.nocookie.net/__cb20141022183927/howtotrainyourdragon/images/c/c5/When_gothi_drops_that_flag_we_race.jpg",
"http://img.gawkerassets.com/img/17jy4w1lonvyijpg/original.jpg"
};
objPagerAdapter=new ViewPagerAdapter(ViewPagerActivity.this, _arrayImagePath);
pager.setAdapter(objPagerAdapter);
// Calling Volley Library to Download the Image for each URLi
for(int i=0;i<URL.length;i++)
{
DownloadImages(URL[i], i);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_pager, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
void DownloadImages(String imageURL_in,final int counter_in)
{
final String counter=String.valueOf(counter_in);
ImageRequest imageRequest = new ImageRequest(imageURL_in,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(final Bitmap response) {
try{
Log.v("Response Received =",""+counter_in);
// Saved in SD Card and getting the Path
String imagePath = SavedInSDCard(response, counter + "_hires");
//Added ImagePath
_arrayImagePath.add(imagePath);
// Notifying the Adapter
objPagerAdapter.notifyDataSetChanged();
} catch (Exception e) {e.printStackTrace();}
}
}, 0, 0, null, null);
ApplicationController.getInstance().addToRequestQueue(imageRequest);
}
public String SavedInSDCard(Bitmap bitmap2, String dishId) {
File dir = new File(ApplicationController.getAppContext()
.getExternalFilesDir("ViewPager_POC") + "/Cache");
// Create the storage directory if it does not exist
if (!dir.exists()) {
dir.mkdirs();
}
String stDishID = dishId;
String formatedDsihID = stDishID.replace(":", "R");
File file = new File(dir, formatedDsihID);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap2.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String imgPath = dir + "/" + formatedDsihID;
return imgPath;
}
这是我的xml文件:
activity_view_pager.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.poc_viewpager.ViewPagerActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/pagerView"
android:layout_width="match_parent"
android:layout_height="match_parent" /></RelativeLayout>
这是我的ViewPager项目xml文件:
view_pager_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="44dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
这是View Pager的适配器类:
ViewPagerAdapter.class
public class ViewPagerAdapter extends PagerAdapter{
Context _context;
LayoutInflater inflater;
ArrayList<String> _arrayImagePath;
public ViewPagerAdapter(Context context_in, ArrayList<String> imagePath_in) {
_arrayImagePath=imagePath_in;
_context = context_in;
}
@Override
public int getCount() {
return _arrayImagePath.size();
}
@Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
((ViewPager) container).removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView txtCounter;
ImageView imgView;
inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.view_pager_item, container,
false);
txtCounter = (TextView) itemView.findViewById(R.id.counter);
imgView= (ImageView) itemView.findViewById(R.id.images);
//Setting the values for Image and textview
txtCounter.setText(""+(position+1));
Bitmap bitmap = BitmapFactory.decodeFile(_arrayImagePath.get(position));
imgView.setImageBitmap(bitmap);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
}
建议解决这个滞后问题?
答案 0 :(得分:0)
在UI线程上使用decodeFile
并不是一个好主意,因为它可能需要一段时间。例如,您可以使用AsyncTask。
看一下android开发者方面,有一个非常好的指南: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
并在您的代码中添加类似的内容:
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public BitmapWorkerTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(String... params) {
String path = params[0];
return BitmapFactory.decodeFile(path);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
在您的instantiateItem
中,您可以使用:
BitmapWorkerTask task = new BitmapWorkerTask(imgView);
task.execute(_arrayImagePath.get(position));
Haven没有对它进行过测试,但它应该有助于理解这个问题。 如果您发现错误,请编辑代码。