Curl page from left to right android

核能气质少年 提交于 2019-12-17 19:41:56

问题


So , I am using page curl by harism , https://github.com/harism/android_page_curl and have successfully implemented it for loading images via web streaming. But can't get it work when I flip back to the previous images or pages as the images are not coming with the right index. i.e. they are not refreshing properly. I can't figure this out.

This is my implementation where I load images to the PageProvider

private class PageProvider implements CurlView.PageProvider {


        @Override
        public int getPageCount() {
            return data1.size()-1;
        } 

        private Bitmap loadBitmap(int width, int height, final int index) throws MalformedURLException, IOException {
            Bitmap b = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
            b.eraseColor(0xFFFFFFFF);
            Canvas c = new Canvas(b);

            System.out.println("value of current page index "+mCurlView.getCurrentIndex()+" and index is "+index);

            System.out.println("url forward");
            aq.ajax(data1.get(index+1), Bitmap.class,0, new AjaxCallback<Bitmap>() {

                @Override
                public void callback(String url, Bitmap object, AjaxStatus status) {
                    if(object!=null)
                           try {
                                System.out.println("url image downloaded "+url);
                                y=object;

                                aq.ajax(data1.get(index).replace(".png", ".mp3"), File.class,0,new AjaxCallback<File>() {
                                    @Override
                                    public void callback(String url, File object, AjaxStatus status) {

                                        System.out.println("url sound downloaded "+url);
                                        try {
                                              if(object!=null)
                                               {    
                                                FileInputStream inputStream = new FileInputStream(object);

                                                if(index>0)
                                                 {  
                                                  mPlayer.stop();
                                                  mPlayer.reset();
                                                 }

                                                prepareMediaPlayer(inputStream.getFD());
                                                inputStream.close();
                                               }
                                            }
                                        catch (Exception e) {}
                                   }
                                });     
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                }
            }); 


            d = new BitmapDrawable(getResources(),y);

            if(y!=null)
            {   
            int margin = 7; 
            int border = 3;
            Rect r = new Rect(margin, margin, width - margin, height - margin);

            int imageWidth = r.width() - (border * 2);
            int imageHeight = imageWidth * d.getIntrinsicHeight()
                    / d.getIntrinsicWidth();
            if (imageHeight > r.height() - (border * 2)) {
                imageHeight = r.height() - (border * 2);
                imageWidth = imageHeight * d.getIntrinsicWidth()
                        / d.getIntrinsicHeight();
            }

            r.left += ((r.width() - imageWidth) / 2) - border;
            r.right = r.left + imageWidth + border + border;
            r.top += ((r.height() - imageHeight) / 2) - border;
            r.bottom = r.top + imageHeight + border + border;

            Paint p = new Paint();
            p.setColor(0xFFC0C0C0);
            c.drawRect(r, p);
            r.left += border;
            r.right -= border;
            r.top += border;
            r.bottom -= border;

            d.setBounds(r);
            d.draw(c);
            }
            //}
            if(y==null)
            return null;

            else
            return b;   
        }

        @Override
        public void updatePage(CurlPage page, final int width, final int height, final int index) {

                Bitmap front = null;

                System.out.println("motion / index value /countIteration / size of map "+motionDirection+"/"+index+"/"+countIteration+"/"+imageFilexxSm.size());

                try {


                     front=loadBitmap(width, height,index);

                    if(front!=null)
                    {
                    page.setTexture(front, CurlPage.SIDE_FRONT);
                    page.setColor(Color.rgb(180, 180, 180), CurlPage.SIDE_BACK);
                    }

                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }}}

Also I have tried setting the index using getCurrentIndex method that is provided inside the CurlView class , but it even doesn't work. Have found that the index gets passed correctly but the bitmaps are not getting refreshed.

More clearly the problem is:

When I move in forward direction i.e. 1st , 2nd , 3rd , 4th , 5th ... the images and sounds works correctly , but when I do the reverse 5th , 4th , 3rd , 2nd , 1st then only 5th to 4th curl is right but 3rd , 2nd and 1st are out of order. Why is this happening?


回答1:


hm... curlpage by harism has behavior to load image more than one when flipping page, example if you flipping page from 0 to 1 curlpage will load image for page 1 and page 2, and if you flip to page 5 curlpage will load image for page 5 and 6, but if you flipping back to page 4, curlpage will load image for page 4, page 3 and page 2.

loadImage() triggered when onTouch ACTION.MOVE, so you have to prepared the image before you move/flip the page or your page will be get null, you have to load manually by calling updatePage() method in CurlView class.

for getCurrentIndex() you can check it in method startCurl() and updatePage() in CurlView class.




回答2:


I hope this would save someone's time if using an imageloading library with harism page curl.

So, after a lot of research I found that the imageloading library I was using (aquery) has got methods i.e. getCachedImage(String url) , getCachedFile(String url) which doesn't work on the threads and return null if the file is not already cached into any storage (sdcard , cache directory).

The piece of code that was actual did it for me is ,

private class PageProvider implements CurlView.PageProvider {


    @Override
    public int getPageCount() {
        return data1.size()-1;
    } 

    private Bitmap loadBitmap(int width, int height, final int index) throws MalformedURLException, IOException {
        Bitmap b = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
        b.eraseColor(0xFFFFFFFF);
        Canvas c = new Canvas(b);

        System.out.println(" Index is : "+index+" Curl state : "+CurlActivity.motionDirection);

        if(index==data1.size())
        {
        lastPage=false; 
        }

        else
        {
        lastPage=true;  
        }   

        if(CurlActivity.motionDirection==11)
             {
                y=aq.getCachedImage(data1.get(index));
                f=aq.getCachedFile(data1.get(index+1).replace(".png", ".mp3"));

                if(f!=null)
                {
                FileInputStream inputStream = new FileInputStream(f);

                if(index>0)
                     {  
                        mPlayer.stop();
                        mPlayer.reset();
                     }

                 prepareMediaPlayer(inputStream.getFD());
                 inputStream.close();
               }
            }

            else
            {
                y=aq.getCachedImage(data1.get(index));

                if(jump==0)
                 { 
                  f=aq.getCachedFile(data1.get(index).replace(".png", ".mp3"));
                 }

                else
                 {
                  f=aq.getCachedFile(data1.get(index+1).replace(".png", ".mp3"));
                 }


                if(y!=null&&f!=null)
                {
                FileInputStream inputStream = new FileInputStream(f);

                if(index>0)
                     {  
                        mPlayer.stop();
                        mPlayer.reset();
                     }
                 prepareMediaPlayer(inputStream.getFD());
                 inputStream.close();
                }

                else if(y!=null && f == null)
                {
                    duration=1000;
                }

                else
                {   
                aq.progress(R.id.progress).ajax(data1.get(index+1), Bitmap.class,0, new AjaxCallback<Bitmap>() {
                @Override
                public void callback(final String urlimg, final Bitmap object1, AjaxStatus status) {
                    if(object1!=null)
                           try {
                                y=object1;

                                aq.ajax(data1.get(index).replace(".png", ".mp3"), File.class,0,new AjaxCallback<File>() {
                                    @Override
                                    public void callback(String url, File object, AjaxStatus status) {

                                        System.out.println("url sound downloaded "+url+status.getCode());
                                        try {
                                              if(object!=null||status.getCode()==404)
                                               {
                                                FileInputStream inputStream = new FileInputStream(object);

                                                if(index>0)
                                                 {  
                                                  mPlayer.stop();
                                                  mPlayer.reset();
                                                 }

                                                prepareMediaPlayer(inputStream.getFD());
                                                inputStream.close();
                                               }
                                            }
                                        catch (Exception e) {}
                                   }
                                });     
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                }
            });
           }    
        }

        d = new BitmapDrawable(getResources(),y);

        if(y!=null)
        {   
        int margin = 7; 
        int border = 3;
        Rect r = new Rect(margin, margin, width - margin, height - margin);

        int imageWidth = r.width() - (border * 2);
        int imageHeight = imageWidth * d.getIntrinsicHeight()
                / d.getIntrinsicWidth();
        if (imageHeight > r.height() - (border * 2)) {
            imageHeight = r.height() - (border * 2);
            imageWidth = imageHeight * d.getIntrinsicWidth()
                    / d.getIntrinsicHeight();
        }

        r.left += ((r.width() - imageWidth) / 2) - border;
        r.right = r.left + imageWidth + border + border;
        r.top += ((r.height() - imageHeight) / 2) - border;
        r.bottom = r.top + imageHeight + border + border;

        Paint p = new Paint();
        p.setColor(0xFFC0C0C0);
        c.drawRect(r, p);
        r.left += border;
        r.right -= border;
        r.top += border;
        r.bottom -= border;

        d.setBounds(r);
        d.draw(c);
        }

        return b;   
    }

    @Override
    public void updatePage(CurlPage page, final int width, final int height, final int index) {
            mPlayer.stop();
            mPlayer.reset();

            Bitmap front = null;

            try {

                front=loadBitmap(width, height,index);

                if(front!=null)
                {
                page.setTexture(front, CurlPage.SIDE_FRONT);
                page.setColor(Color.rgb(180, 180, 180), CurlPage.SIDE_BACK);
                }

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

      } 
}


来源:https://stackoverflow.com/questions/16772734/curl-page-from-left-to-right-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!