生活随笔
收集整理的這篇文章主要介紹了
使用OpenGL实现翻书动画
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
上一篇文章介紹了如何通過紋理渲染繪制地球儀,當然OpenGL的三維圖形處理能力是很強大的,只要善于利用OpenGL,就能很方便地虛擬各種現實生活中的動畫效果。本文再來談談使用OpenGL實現瀏覽電子書時候的翻書動畫。
博主早期的博文《Android開發筆記(十八)書籍翻頁動畫》已經介紹了如何通過貝塞爾曲線實現翻書動畫的過程,不過該方式展示動畫時存在卡頓的現象,并且在書頁范圍之外還會經常拖著長長的影子,實在是有礙觀瞻。現在有了OpenGL,借助三維圖形技術能夠讓翻書動畫顯得更為平滑、更加逼真。正好博主偶然間淘到了一個外國人寫的OpenGL翻書動畫,感覺顯示效果還不錯,故而簡單改造了一下貢獻出來,方便有需要的朋友。
通過OpenGL描繪三維圖形的原理,可參見前面幾篇文章,這里就不啰嗦了,下面直接觀看使用OpenGL實現翻書動畫的效果。
首先是從前往后翻頁的效果動畫:
然后是從后往前翻頁的效果動畫:
怎么樣,還比較流暢吧,其實就是翻書的時候把圖片展示為翻卷的立體效果罷了。
具體的Activity實現代碼如下所示:
public class GlTurnActivity extends Activity {private final static String TAG = "GlTurnActivity";private CurlView cv_content;private String[] imgArray = {"000.jpg", "001.jpg", "002.jpg", "003.jpg"};private int cv_height;private ArrayList<String> imgList = new ArrayList<String>();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_gl_turn);cv_content = (CurlView) findViewById(R.id.cv_content);copyImage();showImage();}// 把圖片文件從assets目錄復制到SD卡private void copyImage() {cv_height = cv_content.getMeasuredHeight();String dir = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/";for (int i=0; i<imgArray.length; i++) {String imgName = imgArray[i];String imgPath = dir + imgName;AssetsUtil.Assets2Sd(this, imgName, imgPath);imgList.add(imgPath);if (i == 0) {Bitmap bitmap = BitmapFactory.decodeFile(imgPath);cv_height = (int) (bitmap.getHeight() * 1.2);}}}// 載入并顯示書籍的圖片,一開始顯示第一頁private void showImage() {LayoutParams params = cv_content.getLayoutParams();params.height = cv_height;cv_content.setLayoutParams(params);cv_content.setPageProvider(new PageProvider(imgList));cv_content.setSizeChangedObserver(new SizeChangedObserver());cv_content.setCurrentIndex(0);cv_content.setBackgroundColor(Color.LTGRAY);}// 定義頁面的提供者,傳入圖片文件的路徑數組private class PageProvider implements CurlView.PageProvider {private ArrayList<String> mPathArray = new ArrayList<String>();public PageProvider(ArrayList<String> pathArray) {mPathArray = pathArray;}@Overridepublic int getPageCount() {return mPathArray.size();}private Bitmap loadBitmap(int width, int height, int index) {Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);b.eraseColor(0xFFFFFFFF);Canvas c = new Canvas(b);//Drawable d = getResources().getDrawable(mBitmapIds[index]);Bitmap image = BitmapFactory.decodeFile(mPathArray.get(index));BitmapDrawable d = new BitmapDrawable(getResources(), image);int margin = 0;int border = 1;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;}@Overridepublic void updatePage(CurlPage page, int width, int height, int index) {Bitmap front = loadBitmap(width, height, index);page.setTexture(front, CurlPage.SIDE_BOTH);}}// 定義書籍尺寸的變化監聽器private class SizeChangedObserver implements CurlView.SizeChangedObserver {@Overridepublic void onSizeChanged(int w, int h) {cv_content.setViewMode(CurlView.SHOW_ONE_PAGE);cv_content.setMargins(0f, 0f, 0f, 0f);}}}
完整的工程代碼見博主的github,該工程的demo地址為 https://github.com/aqi00/note/tree/master/ExmOpenGL
?
轉載于:https://my.oschina.net/ouysh1981/blog/1618440
總結
以上是生活随笔為你收集整理的使用OpenGL实现翻书动画的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。