Android Open Source - LearnByHeart Glance Fold Shading






From Project

Back to project page LearnByHeart.

License

The source code is released under:

Apache License

If you think the Android project LearnByHeart 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

package com.mps.learn.pb.ui.views.shading;
/*w  w  w  . ja  v a 2  s  . c o  m*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.Gravity;

public class GlanceFoldShading implements FoldShading {

    private static final int SHADOW_COLOR = Color.BLACK;
    private static final int SHADOW_MAX_ALPHA = 192;

    private final Paint mSolidShadow;

    private final Paint mGlancePaint;
    private final Bitmap mGlance;
    private final Rect mGlanceFrom;
    private final Rect mGlanceTo;

    public GlanceFoldShading(Context context, Bitmap glance) {
        mSolidShadow = new Paint();
        mSolidShadow.setColor(SHADOW_COLOR);

        mGlance = glance;
        mGlancePaint = new Paint();
        mGlancePaint.setDither(true);
        mGlancePaint.setFilterBitmap(true);
        mGlanceFrom = new Rect();
        mGlanceTo = new Rect();
    }

    @Override
    public void onPreDraw(Canvas canvas, Rect bounds, float rotation, int gravity) {
        // NO-OP
    }

    @Override
    public void onPostDraw(Canvas canvas, Rect bounds, float rotation, int gravity) {
        float intencity = getShadowIntencity(rotation, gravity);

        if (intencity > 0) {
            int alpha = (int) (SHADOW_MAX_ALPHA * intencity);
            mSolidShadow.setAlpha(alpha);
            canvas.drawRect(bounds, mSolidShadow);
        }

        boolean isDrawGlance = computeGlance(bounds, rotation, gravity);
        if (isDrawGlance) {
            canvas.drawBitmap(mGlance, mGlanceFrom, mGlanceTo, mGlancePaint);
        }
    }

    private float getShadowIntencity(float rotation, int gravity) {
        float intencity = 0;
        if (gravity == Gravity.TOP) {
            if (rotation > -90 && rotation < 0) { // (-90; 0) - rotation is applied
                intencity = -rotation / 90f;
            }
        }
        return intencity;
    }

    private boolean computeGlance(Rect bounds, float rotation, int gravity) {
        if (gravity == Gravity.BOTTOM) {
            if (rotation > 0 && rotation < 90) { // (0; 90) - rotation is applied
                final float aspect = (float) mGlance.getWidth() / (float) bounds.width();

                // computing glance offset
                final int distance = (int) (bounds.height() * ((rotation - 60f) / 15f));
                final int distanceOnGlance = (int) (distance * aspect);

                // computing "to" bounds
                int scaledGlanceHeight = (int) (mGlance.getHeight() / aspect);
                mGlanceTo.set(bounds.left, bounds.top + distance, bounds.right, bounds.top + distance + scaledGlanceHeight);
                if (!mGlanceTo.intersect(bounds)) {
                    // glance is not visible
                    return false;
                }

                // computing "from" bounds
                int scaledBoundsHeight = (int) (bounds.height() * aspect);
                mGlanceFrom.set(0, -distanceOnGlance, mGlance.getWidth(), -distanceOnGlance + scaledBoundsHeight);
                if (!mGlanceFrom.intersect(0, 0, mGlance.getWidth(), mGlance.getHeight())) {
                    // glance is not visible, should not happen due to previouse check
                    return false;
                }

                return true;
            }
        }

        return false;
    }

}




Java Source Code List

com.mps.learn.pb.App.java
com.mps.learn.pb.PhraseManager.java
com.mps.learn.pb.adapter.AllPhraseAdapter.java
com.mps.learn.pb.alarm.ReminderReceiver.java
com.mps.learn.pb.alarm.ReminderUtil.java
com.mps.learn.pb.db.DataSourcePhrase.java
com.mps.learn.pb.db.DatabaseHelper.java
com.mps.learn.pb.db.PhraseReaderContract.java
com.mps.learn.pb.gcm.GcmBroadcastReceiver.java
com.mps.learn.pb.gcm.GcmIntentService.java
com.mps.learn.pb.http.DataUpdaterCallback.java
com.mps.learn.pb.http.DataUpdaterTask.java
com.mps.learn.pb.http.HttpConstant.java
com.mps.learn.pb.http.PhraseParser.java
com.mps.learn.pb.model.PhraseBook.java
com.mps.learn.pb.model.PhraseModel.java
com.mps.learn.pb.ui.activities.AddPhraseActivity.java
com.mps.learn.pb.ui.activities.MainActivity.java
com.mps.learn.pb.ui.activities.PhraseViewActivity.java
com.mps.learn.pb.ui.activities.SettingActivity.java
com.mps.learn.pb.ui.factivities.BaseActivity.java
com.mps.learn.pb.ui.factivities.FActivity.java
com.mps.learn.pb.ui.factivities.FoldableListActivity.java
com.mps.learn.pb.ui.factivities.ItemsAdapter.java
com.mps.learn.pb.ui.factivities.PaintingsAdapter.java
com.mps.learn.pb.ui.factivities.SpannableBuilder.java
com.mps.learn.pb.ui.factivities.UnfoldableDetailsActivity.java
com.mps.learn.pb.ui.views.FoldableItemLayout.java
com.mps.learn.pb.ui.views.FoldableListLayout.java
com.mps.learn.pb.ui.views.UnfoldableView.java
com.mps.learn.pb.ui.views.shading.FoldShading.java
com.mps.learn.pb.ui.views.shading.GlanceFoldShading.java
com.mps.learn.pb.ui.views.shading.SimpleFoldShading.java
com.mps.learn.pb.util.CommonConstants.java
com.mps.learn.pb.util.Debugger.java