Android Open Source - Material Arrow Drawable






From Project

Back to project page Material.

License

The source code is released under:

Apache License

If you think the Android project Material 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.rey.material.drawable;
//from  w w w .  j  a  v a 2s  . com
import com.rey.material.util.ViewUtil;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;

public class ArrowDrawable extends Drawable implements Animatable{
  
  private boolean mRunning = false;
  private long mStartTime;
  private float mAnimProgress;
  private int mAnimDuration;
  
  private Paint mPaint;
  private ColorStateList mColorStateList;
  private int mSize;
  private int mCurColor;
  private int mMode;
  private Interpolator mInterpolator;
  
  private Path mPath;
    
  public static int MODE_DOWN = 0;
  public static int MODE_UP = 1;
  
  private boolean mClockwise = true;
  
  public ArrowDrawable(int mode, int size, ColorStateList colorStateList, int animDuration, Interpolator interpolator, boolean clockwise){
    mSize = size;
    mAnimDuration = animDuration;
    mMode = mode;
    mInterpolator = interpolator;
    if(mInterpolator == null)
      mInterpolator = new DecelerateInterpolator();
    mClockwise = clockwise;
    
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setStyle(Paint.Style.FILL);
    
    mPath = new Path();
    
    setColor(colorStateList);
  }
  
  public void setColor(ColorStateList colorStateList){
    mColorStateList = colorStateList;
    onStateChange(getState());
  }
  
  public void setMode(int mode, boolean animation){
    if(mMode != mode){
      mMode = mode;      
      if(animation && mAnimDuration > 0)
        start();      
      else
        invalidateSelf();
    }
  }  
  
  public int getMode(){
    return mMode;
  }
    
  @Override
  protected void onBoundsChange(Rect bounds) {
    float x = bounds.exactCenterX();
    float y = bounds.exactCenterY();
    
    mPath.reset();
    mPath.moveTo(x, y + mSize / 2f);
    mPath.lineTo(x - mSize, y - mSize / 2f);
    mPath.lineTo(x + mSize, y - mSize / 2f);
    mPath.close();
  }

  @Override
  public void draw(Canvas canvas) {
    int saveCount = canvas.save();
    Rect bounds = getBounds();
    
    if(!isRunning()){
      if(mMode == MODE_UP)
        canvas.rotate(180, bounds.exactCenterX(), bounds.exactCenterY());
    }
    else{
      float value = mInterpolator.getInterpolation(mAnimProgress);
      float degree;
      
      if(mClockwise){
        if(mMode == MODE_UP) // move down > up
          degree = 180 * value;
        else // move up > down
          degree = 180 * (1 + value);
      }
      else{
        if(mMode == MODE_UP) // move down > up
          degree = -180 * value;
        else // move up > down
          degree = -180 * (1 + value);
      }
      
      canvas.rotate(degree, bounds.exactCenterX(), bounds.exactCenterY());
    }
    
    mPaint.setColor(mCurColor);
    canvas.drawPath(mPath, mPaint);
    
    canvas.restoreToCount(saveCount);
  }
      
  @Override
  public void setAlpha(int alpha) {
    mPaint.setAlpha(alpha);      
  }

  @Override
  public void setColorFilter(ColorFilter cf) {
    mPaint.setColorFilter(cf);
  }

  @Override
  public int getOpacity() {
    return PixelFormat.TRANSLUCENT;
  }

  @Override
  public boolean isStateful() {
    return true;
  }

  @Override
  protected boolean onStateChange(int[] state) {
    int color = mColorStateList.getColorForState(state, mCurColor);    
        
    if(mCurColor != color){
      mCurColor = color;  
      return true;
    }
      
    return false;
  }

  private void resetAnimation(){  
    mStartTime = SystemClock.uptimeMillis();
    mAnimProgress = 0f;
  }
  
  @Override
  public void start() {
    resetAnimation();      
    scheduleSelf(mUpdater, SystemClock.uptimeMillis() + ViewUtil.FRAME_DURATION);
      invalidateSelf();  
  }

  @Override
  public void stop() {
    mRunning = false;
    unscheduleSelf(mUpdater);
    invalidateSelf();
  }

  @Override
  public boolean isRunning() {
    return mRunning;
  }
  
  @Override
  public void scheduleSelf(Runnable what, long when) {
    mRunning = true;
      super.scheduleSelf(what, when);
  }
  
  private final Runnable mUpdater = new Runnable() {

      @Override
      public void run() {
        update();
      }
        
  };
    
  private void update(){
    long curTime = SystemClock.uptimeMillis();
    mAnimProgress = Math.min(1f, (float)(curTime - mStartTime) / mAnimDuration);  
    
    if(mAnimProgress == 1f)
      mRunning = false;
        
      if(isRunning())
        scheduleSelf(mUpdater, SystemClock.uptimeMillis() + ViewUtil.FRAME_DURATION);
      
      invalidateSelf();
  }
  
}




Java Source Code List

com.rey.material.ApplicationTest.java
com.rey.material.demo.ButtonFragment.java
com.rey.material.demo.MainActivity.java
com.rey.material.demo.ProgressFragment.java
com.rey.material.demo.SnackbarFragment.java
com.rey.material.demo.SwitchesFragment.java
com.rey.material.demo.TextfieldFragment.java
com.rey.material.drawable.ArrowDrawable.java
com.rey.material.drawable.BlankDrawable.java
com.rey.material.drawable.CheckBoxDrawable.java
com.rey.material.drawable.CircularProgressDrawable.java
com.rey.material.drawable.DividerDrawable.java
com.rey.material.drawable.LineMorphingDrawable.java
com.rey.material.drawable.LinearProgressDrawable.java
com.rey.material.drawable.NavigationDrawerDrawable.java
com.rey.material.drawable.RadioButtonDrawable.java
com.rey.material.drawable.RevealDrawable.java
com.rey.material.drawable.RippleDrawable.java
com.rey.material.drawable.ToolbarRippleDrawable.java
com.rey.material.util.ColorUtil.java
com.rey.material.util.ThemeUtil.java
com.rey.material.util.ViewUtil.java
com.rey.material.view.Button.java
com.rey.material.view.CheckBox.java
com.rey.material.view.CheckedTextView.java
com.rey.material.view.CompoundButton.java
com.rey.material.view.EditText.java
com.rey.material.view.FloatingActionButton.java
com.rey.material.view.ListPopupWindow.java
com.rey.material.view.ListView.java
com.rey.material.view.PopupWindow.java
com.rey.material.view.ProgressView.java
com.rey.material.view.RadioButton.java
com.rey.material.view.RippleManager.java
com.rey.material.view.SnackBar.java
com.rey.material.view.Spinner.java
com.rey.material.view.Switch.java
com.rey.material.view.TabPageIndicator.java
com.rey.material.view.TextView.java