Android Open Source - RZAndroidBaseUtils Image Mix View






From Project

Back to project page RZAndroidBaseUtils.

License

The source code is released under:

MIT License

If you think the Android project RZAndroidBaseUtils 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.raizlabs.widget;
//from  w ww. j  ava2 s .co m
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

/**
 * View which mixes between two images by showing some portion of one image and
 * the rest of another.
 * <br><br>
 * Note: The view is sized based off the first image.
 * @author Dylan James
 *
 */
public class ImageMixView extends View {
  /**
   * Possible directions for the mix.
   * @author Dylan James
   *
   */
  public static class MixDirection {
    public static final int VERTICAL = 0; // 0b0
    public static final int HORIZONTAL = 2; // 0b10
    static final int REVERSE = 1; // 0b01
    
    public static final int VERTICAL_REVERSE = VERTICAL | REVERSE;
    public static final int HORIZONTAL_REVERSE = HORIZONTAL | REVERSE;
  }
  
  private int mixDirection;
  /**
   * Sets the mix direction for this view.
   * @param direction One of {@link MixDirection} which represents the
   * direction to do the mix.
   */
  public void setMixDirection(int direction) {
    mixDirection = direction;
  }
  
  // Rectangles to sample from in the source images
  Rect src1, src2;
  // Rectangles to draw to in the output canvas
  Rect dst1, dst2;
  
  private Bitmap bitmap1, bitmap2;
  /**
   * Sets the first image of the mix.
   * @param bmp The {@link Bitmap} to use as the first image.
   */
  public void setFirstImage(Bitmap bmp) {
    bitmap1 = bmp;
    src1 = new Rect(0, 0, bitmap1.getWidth(), bitmap1.getHeight());
    requestLayout();
    postInvalidate();
  }
  /**
   * Sets the first image of the mix.
   * @param id The resource id of the image to use as the first image.
   */
  public void setFirstImageResource(int id) {
    setFirstImage(BitmapFactory.decodeResource(getResources(), id));
  }
  
  /**
   * Sets the second image of the mix.
   * @param bmp The {@link Bitmap} to use as the second image.
   */
  public void setSecondImage(Bitmap bmp) {
    bitmap2 = bmp;
    src2 = new Rect(0, 0, bitmap2.getWidth(), bitmap2.getHeight());
    postInvalidate();
  }
  /**
   * Sets the second image of the mix.
   * @param id The resource id of the image to use as the second image.
   */
  public void setSecondImageResource(int id) {
    setSecondImage(BitmapFactory.decodeResource(getResources(), id));
  }
  
  private float mixValue;
  /**
   * Sets the current mix value.
   * @param mix A value between 0 and 1 representing an empty or full
   * mix respectively.
   */
  public void setMixValue(float mix) {
    mixValue = com.raizlabs.baseutils.Math.clamp(mix, 0, 1);
    postInvalidate();
  }
  
  /**
   * @return The current mix value - a number between 0 and 1 representing
   * an empty or full mix respectively.
   */
  public float getMixValue() { return mixValue; }
  
  public ImageMixView(Context context) {
    super(context);
    init();
  }
  public ImageMixView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }
  public ImageMixView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }
  
  private void init() {
    setMixDirection(MixDirection.VERTICAL);
    setMixValue(0);
    dst1 = new Rect();
    dst2 = new Rect();
  }
  
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    
    int imageWidth = (bitmap1 == null ? 0 : bitmap1.getWidth());
    int imageHeight = (bitmap1 == null ? 0 : bitmap1.getHeight());
    
    switch (widthMode) {
    case MeasureSpec.EXACTLY:
      break;
    case MeasureSpec.AT_MOST:
      width = Math.min(width, imageWidth);      
      break;
    case MeasureSpec.UNSPECIFIED:
      width = imageWidth;
      break;
    }
    
    switch (heightMode) {
    case MeasureSpec.EXACTLY:
      break;
    case MeasureSpec.AT_MOST:
      height = Math.min(height, imageHeight);      
      break;
    case MeasureSpec.UNSPECIFIED:
      height = imageHeight;
      break;
    }
    
    setMeasuredDimension(width, height);
  }
  
  @Override
  protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    // Resize our output rectangles
    dst1.right = dst2.right = getWidth();
    dst1.bottom = dst2.bottom = getHeight();
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (bitmap1 == null || bitmap2 == null) return;
    // Read the bitmask to see if this is set to run in reverse
    final boolean reversed = (mixDirection & MixDirection.REVERSE) > 0;
    // If reversed, flip the mix value
    final float currMix = reversed ? 1 - mixValue : mixValue;
    
    // Get the non-reverse bits
    switch (mixDirection & ~MixDirection.REVERSE) {
    case MixDirection.HORIZONTAL:
      // Update the input rectangles
      final int srcLeft = (int) (currMix * bitmap1.getWidth());
      src1.left = srcLeft;
      src2.right = srcLeft;
      
      // Update the output rectangles
      final int dstLeft = (int) (currMix * getWidth());
      dst1.left = dstLeft;
      dst2.right = dstLeft;
      break;
    case MixDirection.VERTICAL:
      // Update the input rectangles
      final int srcHeight = bitmap1.getHeight();
      final int srcBottom = (int) (srcHeight - (currMix * srcHeight));
      src1.bottom = srcBottom;
      src2.top = srcBottom;
      
      // Update the output rectangles
      final int dstHeight = getHeight();
      final int dstBottom = (int) (dstHeight - (currMix * dstHeight));
      dst1.bottom = dstBottom;
      dst2.top = dstBottom;
      break;
    }
    
    canvas.drawBitmap(bitmap1, src1, dst1, null);
    canvas.drawBitmap(bitmap2, src2, dst2, null);
  }
}




Java Source Code List

com.raizlabs.baseutils.CompatibilityUtils.java
com.raizlabs.baseutils.IOUtils.java
com.raizlabs.baseutils.Logger.java
com.raizlabs.baseutils.Math.java
com.raizlabs.baseutils.StringUtils.java
com.raizlabs.baseutils.ThreadingUtils.java
com.raizlabs.baseutils.Wrapper.java
com.raizlabs.baseutils.examples.MainActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableExampleActivity.java
com.raizlabs.baseutils.examples.asyncdrawable.AsyncDrawableListExampleActivity.java
com.raizlabs.baseutils.examples.simplegenericadapter.SimpleGenericAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewgroupadapter.ViewGroupAdapterExampleActivity.java
com.raizlabs.baseutils.examples.viewholderstrategy.SimpleViewHolderStrategyExampleActivity.java
com.raizlabs.collections.ListUtils.java
com.raizlabs.collections.MappableSet.java
com.raizlabs.collections.TransactionalHashSet.java
com.raizlabs.concurrent.BasePrioritizedRunnable.java
com.raizlabs.concurrent.ConcurrencyUtils.java
com.raizlabs.concurrent.PrioritizedRunnable.java
com.raizlabs.concurrent.Prioritized.java
com.raizlabs.content.sharing.SharingUtils.java
com.raizlabs.database.CursorIterable.java
com.raizlabs.database.CursorIterator.java
com.raizlabs.events.EventListener.java
com.raizlabs.events.Event.java
com.raizlabs.events.ProgressListener.java
com.raizlabs.events.SimpleEventListener.java
com.raizlabs.functions.Delegate.java
com.raizlabs.functions.Predicate.java
com.raizlabs.functions.Provider.java
com.raizlabs.graphics.ImageFactory.java
com.raizlabs.graphics.drawable.async.AsyncDrawableTask.java
com.raizlabs.graphics.drawable.async.AsyncDrawableUtils.java
com.raizlabs.graphics.drawable.async.AsyncDrawableWrapper.java
com.raizlabs.graphics.drawable.async.AsyncDrawable.java
com.raizlabs.graphics.drawable.async.BaseAsyncDrawableTask.java
com.raizlabs.imagecaching.ImageCache.java
com.raizlabs.imagecaching.PrefixedImageCacheAdapter.java
com.raizlabs.imagecaching.StubImageCache.java
com.raizlabs.json.JSONArrayParserDelegate.java
com.raizlabs.json.JSONHelper.java
com.raizlabs.synchronization.OneShotLock.java
com.raizlabs.tasks.RZAsyncTaskEvent.java
com.raizlabs.tasks.RZAsyncTaskListener.java
com.raizlabs.tasks.RZAsyncTask.java
com.raizlabs.util.observable.ObservableData.java
com.raizlabs.util.observable.ObservableListAdapter.java
com.raizlabs.util.observable.ObservableList.java
com.raizlabs.view.ViewCompatibility.java
com.raizlabs.view.animation.AnimationListenerWrapper.java
com.raizlabs.view.animation.RelativeLayoutParamsAnimation.java
com.raizlabs.view.animation.ResizeAnimation.java
com.raizlabs.widget.EvenLinearLayout.java
com.raizlabs.widget.ImageMixView.java
com.raizlabs.widget.SlideRevealLayout.java
com.raizlabs.widget.ViewUtils.java
com.raizlabs.widget.adapters.ListBasedAdapter.java
com.raizlabs.widget.adapters.SimpleGenericAdapter.java
com.raizlabs.widget.adapters.ViewGroupAdapter.java
com.raizlabs.widget.adapters.ViewHolderStrategyAdapter.java
com.raizlabs.widget.utils.SimpleViewHolderStrategy.java
com.raizlabs.widget.utils.ViewHolderStrategyConverter.java
com.raizlabs.widget.utils.ViewHolderStrategyUtils.java
com.raizlabs.widget.utils.ViewHolderStrategy.java