Step 1
Android Page Curl Effect is 2D view which provides a page curl illusion while swapping through the images. In this tutorial, we are going implement the page curl effect through a simple example using numAndroidPageCurlEffect library. In this library, only canvas has been used so that it can be used in any Android application. The numAndroidPageCurlEffect library is inspired by android-page-curl repositiory.
Step 1
Adding support library in the dependencies
To make the page curl effect for image, we need to add the numAndroidPageCurl library in the dependencies. Open your build.gradle(app) and add the library in the dependencies.
dependencies {
compile 'app.num.numandroidpagecurleffect:numandroidpagecurleffect:1.0'}
Step 3
Creating Layout
We need to add the app.num.numandroidpagecurleffect.PageCurlView tag in layout xml file to make the page curl effect for image. We must add the first page in the background. Use the following code:<?xml version="1.0" encoding="utf-8"?><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="app.pagecurleffect.MainActivity"> <app.num.numandroidpagecurleffect.PageCurlView android:layout_width="match_parent" android:layout_height="fill_parent" android:id="@+id/pagecurl_view" android:background="@drawable/page1"/> </RelativeLayout>Step 4
Initializing the PageCurlView
Now, we will initialize the id of PageCurlView to use the properties of class PageCurlView. It will help to display the curl effect on image.
123456789<br />publicclassMainActivityextendsAppCompatActivity {</p><p>@Override<br />protectedvoidonCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.activity_main);</p><p> PageCurlView pageCurlView = (PageCurlView) findViewById(R.id.pagecurl_view);<br />}<br />}<br />Step 5
Creating an ArrayList
We need to create an ArrayList which is an integer type. We will add the id of image drawables to get the images from source. Use the following code to create an ArrayLsit:
123456789101112<br />publicclassMainActivityextendsAppCompatActivity {</p><p>@Override<br />protectedvoidonCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.activity_main);</p><p> PageCurlView pageCurlView = (PageCurlView) findViewById(R.id.dcgpagecurlPageCurlView1);</p><p> List<Integer> pages_id =newArrayList<>();<br />pages_id.add(R.drawable.page1);<br />pages_id.add(R.drawable.page2);</p><p> }<br />}<br />Step 6
Call the setCurlView() method
We need to pass the list of image in the setCurlView() method. It sets list of images in PageCurlView class and this class make the curl effect on images.
12345678910111213<br />publicclassMainActivityextendsAppCompatActivity {</p><p>@Override<br />protectedvoidonCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.activity_main);</p><p> PageCurlView pageCurlView = (PageCurlView) findViewById(R.id.dcgpagecurlPageCurlView1);</p><p> List<Integer> pages_id =newArrayList<>();<br />pages_id.add(R.drawable.page1);<br />pages_id.add(R.drawable.page2);</p><p> pageCurlView.setCurlView(pages_id);</p><p> }<br />}<br />Step 8
Add the curl speed
We can set the curl speed via setCurlSpeed() method. It is optional and by default it is set to 62.
1234567891011121314<br />publicclassMainActivityextendsAppCompatActivity {</p><p>@Override<br />protectedvoidonCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.activity_main);</p><p> PageCurlView pageCurlView = (PageCurlView) findViewById(R.id.dcgpagecurlPageCurlView1);</p><p> List<Integer> pages_id =newArrayList<>();<br />pages_id.add(R.drawable.page1);<br />pages_id.add(R.drawable.page2);</p><p> pageCurlView.setCurlView(pages_id);<br />pageCurlView.setCurlSpeed(65);</p><p> }<br />}<br />Final MainActivity.javaimport android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.util.ArrayList; import java.util.List; import app.num.numandroidpagecurleffect.PageCurlView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PageCurlView pageCurlView = (PageCurlView) findViewById(R.id.pagecurl_view); List<Integer> pages_id = new ArrayList<>(); pages_id.add(R.drawable.page1); pages_id.add(R.drawable.page2); pageCurlView.setCurlView(pages_id); pageCurlView.setCurlSpeed(65); } }
No comments:
Post a Comment