Android Open Source - android-training-tutorial Curl Activity






From Project

Back to project page android-training-tutorial.

License

The source code is released under:

MIT License

If you think the Android project android-training-tutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
   Copyright 2012 Harri Smatt//  w  ww  .ja  va 2s.  com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package fi.harism.curl;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

/**
 * Simple Activity for curl testing.
 * 
 * @author harism
 */
public class CurlActivity extends Activity {

  private CurlView mCurlView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    int index = 0;
    if (getLastNonConfigurationInstance() != null) {
      index = (Integer) getLastNonConfigurationInstance();
    }
    mCurlView = (CurlView) findViewById(R.id.curl);

    // Bitmap resources.
    final int[] mBitmapIds = { R.drawable.obama, R.drawable.road_rage,
        R.drawable.taipei_101, R.drawable.world };

    mCurlView.setPageProvider(new PageProvider(mBitmapIds));
    mCurlView.setSizeChangedObserver(new SizeChangedObserver());
    mCurlView.setCurrentIndex(index);
    mCurlView.setBackgroundColor(0xFF202830);

    // This is something somewhat experimental. Before uncommenting next
    // line, please see method comments in CurlView.
    // mCurlView.setEnableTouchPressure(true);
  }

  @Override
  public void onPause() {
    super.onPause();
    mCurlView.onPause();
  }

  @Override
  public void onResume() {
    super.onResume();
    mCurlView.onResume();
  }

  @Override
  public Object onRetainNonConfigurationInstance() {
    return mCurlView.getCurrentIndex();
  }

  /**
   * Bitmap provider.
   */
  private class PageProvider implements CurlView.PageProvider {

    // Bitmap resources.
    private int[] mBitmapIds;

    public PageProvider(int[] bitmapIds) {
      super();
      this.mBitmapIds = bitmapIds;
    }
    
    @Override
    public int getPageCount() {
      return -1;
    }

    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]);

      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, int width, int height, int index) {

      index = index % 5;

      switch (index) {
      // First case is image on front side, solid colored back.
      case 0: {
        Bitmap front = loadBitmap(width, height, 0);
        page.setTexture(front, CurlPage.SIDE_FRONT);
        page.setColor(Color.rgb(180, 180, 180), CurlPage.SIDE_BACK);
        break;
      }
      // Second case is image on back side, solid colored front.
      case 1: {
        Bitmap back = loadBitmap(width, height, 2);
        page.setTexture(back, CurlPage.SIDE_BACK);
        page.setColor(Color.rgb(127, 140, 180), CurlPage.SIDE_FRONT);
        break;
      }
      // Third case is images on both sides.
      case 2: {
        Bitmap front = loadBitmap(width, height, 1);
        Bitmap back = loadBitmap(width, height, 3);
        page.setTexture(front, CurlPage.SIDE_FRONT);
        page.setTexture(back, CurlPage.SIDE_BACK);
        break;
      }
      // Fourth case is images on both sides - plus they are blend against
      // separate colors.
      case 3: {
        Bitmap front = loadBitmap(width, height, 2);
        Bitmap back = loadBitmap(width, height, 1);
        page.setTexture(front, CurlPage.SIDE_FRONT);
        page.setTexture(back, CurlPage.SIDE_BACK);
        page.setColor(Color.argb(127, 170, 130, 255),
            CurlPage.SIDE_FRONT);
        page.setColor(Color.rgb(255, 190, 150), CurlPage.SIDE_BACK);
        break;
      }
      // Fifth case is same image is assigned to front and back. In this
      // scenario only one texture is used and shared for both sides.
      case 4:
        Bitmap front = loadBitmap(width, height, 0);
        page.setTexture(front, CurlPage.SIDE_BOTH);
        page.setColor(Color.argb(127, 255, 255, 255),
            CurlPage.SIDE_BACK);
        break;
      }
    }

  }

  /**
   * CurlView size changed observer.
   */
  private class SizeChangedObserver implements CurlView.SizeChangedObserver {
    @Override
    public void onSizeChanged(int w, int h) {
      if (w > h) {
        mCurlView.setViewMode(CurlView.SHOW_TWO_PAGES);
        mCurlView.setMargins(.1f, .05f, .1f, .05f);
      } else {
        mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);
        mCurlView.setMargins(.1f, .1f, .1f, .1f);
      }
    }
  }

}




Java Source Code List

com.flavienlaurent.vdh.DragActivity.java
com.flavienlaurent.vdh.DragLayout.java
com.flavienlaurent.vdh.HorizontalDragLayout.java
com.flavienlaurent.vdh.MainActivity.java
com.flavienlaurent.vdh.YoutubeActivity.java
com.flavienlaurent.vdh.YoutubeLayout.java
com.flavienlaurent.vdh.listener.SwipeDismissTouchListener.java
com.hb.views.PinnedSectionListView.java
fi.harism.curl.CurlActivity.java
fi.harism.curl.CurlMesh.java
fi.harism.curl.CurlPage.java
fi.harism.curl.CurlRenderer.java
fi.harism.curl.CurlView.java
im.ene.dev.zbroadcastreceiver2.MainActivity.java
im.ene.dev.zbroadcastreceiver2.receivers.CustomBroadcastReceiver.java
im.ene.dev.zbroadcastreceiver2.services.CustomBroadcastService.java
im.ene.dev.zbroadcastreceiver.MainActivity.java
im.ene.dev.zbroadcastreceiver.SecondActivity.java
im.ene.dev.zbroadcastreceiver.receivers.CustomBroadcastReceiver.java
im.ene.dev.zbroadcastreceiver.services.CustomBroadcastService.java
im.ene.dev.zserviceibinder.MainActivity.java
im.ene.dev.zserviceibinder.services.CustomService.java
im.ene.pinnedheaderlistviewsample.MainActivity.java
im.ene.samplevideoplayer.MainActivity.java
im.ene.samplevideoplayer.constant.Constant.java
im.ene.samplevideoplayer.views.CustomSurfaceView.java
im.ene.samplevideoplayer.views.VideoControllerView.java
im.ene.zaidlservicesample.MainActivity.java
im.ene.zaidlservicesample.Second.java
im.ene.zanimationsample.MainActivity.java
im.ene.zcustomvideoview.MainActivity.java
im.ene.zcustomvideoview.views.CustomVideoView.java
im.ene.zcustomvideoview.views.VideoControllerView.java
im.ene.zintenttutorial.MainActivity.java
im.ene.zintenttutorial.broadcastreceiver.CustomBroadcastReceiver.java
im.ene.zintenttutorial.services.CustomService.java
im.ene.zviewpager.MainActivity.java