Example usage for android.graphics LinearGradient LinearGradient

List of usage examples for android.graphics LinearGradient LinearGradient

Introduction

In this page you can find the example usage for android.graphics LinearGradient LinearGradient.

Prototype

public LinearGradient(float x0, float y0, float x1, float y1, @ColorInt int color0, @ColorInt int color1,
        @NonNull TileMode tile) 

Source Link

Document

Create a shader that draws a linear gradient along a line.

Usage

From source file:Main.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*from   w w w  .  j  a  v  a2  s  . com*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:com.yoloo.android.util.ScrimUtil.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details./*w  w w .  jav a 2  s  .  c o m*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs,
    // based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    final int red = Color.red(baseColor);
    final int green = Color.green(baseColor);
    final int blue = Color.blue(baseColor);
    final int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtils.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(width * x0, height * y0, width * x1, height * y1, stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:com.example.google.maps.dataviz.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // The gradient for colouring the markers.
    Shader shader = new LinearGradient(0, 0, 0, MARKER_HEIGHT - 1, new int[] { Color.BLUE, Color.RED }, null,
            TileMode.CLAMP);//from   w ww .j a v a 2  s  . c  o m
    mPaint = new Paint();
    mPaint.setShader(shader);

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    if (mMap != null) {
        setUpMap();
    }
}

From source file:org.mariotaku.twidere.view.ProfileBannerImageView.java

@SuppressLint("DrawAllocation")
@Override/*from w w  w.j  a  v a 2 s.com*/
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    final int width = MeasureSpec.getSize(widthMeasureSpec), height = width / 2;
    setMeasuredDimension(width, height);
    if (width > 0) {
        mShader = new LinearGradient(width / 2, 0, width / 2, height, COLORS, POSITIONS, Shader.TileMode.CLAMP);
    }
    super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
}

From source file:org.wikipedia.page.shareafact.SnippetImage.java

private static void drawGradient(@NonNull Canvas canvas) {
    // draw a dark gradient over the image, so that the white text
    // will stand out better against it.
    final int gradientStartColor = 0x60000000;
    final int gradientStopColor = 0xA0000000;
    Shader shader = new LinearGradient(0, 0, 0, canvas.getHeight(), gradientStartColor, gradientStopColor,
            Shader.TileMode.CLAMP);/*w  ww  .  jav  a2  s .  c  o m*/
    Paint paint = new Paint();
    paint.setShader(shader);
    canvas.drawRect(new Rect(0, 0, canvas.getWidth(), canvas.getHeight()), paint);
}

From source file:com.bangalore.barcamp.BCBUtils.java

public static void createActionBarOnActivity(final Activity activity, boolean isHome) {
    // ******** Start of Action Bar configuration
    ActionBar actionbar = (ActionBar) activity.findViewById(R.id.actionBar1);
    actionbar.setHomeLogo(R.drawable.home);
    actionbar.setHomeAction(new Action() {
        @Override//from   w ww.  j ava 2  s . c  o  m
        public void performAction(View view) {
            ((SlidingMenuActivity) activity).toggle();
        }

        @Override
        public int getDrawable() {
            return R.drawable.home;
        }
    });

    actionbar.setTitle(R.string.app_title_text);
    TextView logo = (TextView) activity.findViewById(R.id.actionbar_title);
    Shader textShader = new LinearGradient(0, 0, 0, logo.getHeight(), new int[] { Color.WHITE, 0xff999999 },
            null, TileMode.CLAMP);
    logo.getPaint().setShader(textShader);
    actionbar.setOnTitleClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        }
    });
    // ******** End of Action Bar configuration

}

From source file:com.cmtv.tv.widget.pagerindicator.TabPageIndicator.java

public TabPageIndicator(Context context, AttributeSet attrs) {
    super(context, attrs);
    setHorizontalScrollBarEnabled(false);

    //custom//w  ww.  j av a 2 s .co m
    mContext = context;
    this.mTextFocusColor = context.getResources().getColor(R.color.tab_view_indicator_focus_color);
    this.mFocusedShader = new LinearGradient(0.0f, 0.0f, 0.0f, 50.0f, mTextFocusColor, mTextFocusColor,
            Shader.TileMode.CLAMP);
    this.mNormalShader = new LinearGradient(0.0f, 0.0f, 0.0f, 40.0f, Color.WHITE, Color.GRAY,
            Shader.TileMode.CLAMP);

    mTabLayout = new IcsLinearLayout(context, R.attr.vpiTabPageIndicatorStyle);
    addView(mTabLayout, new ViewGroup.LayoutParams(WRAP_CONTENT, MATCH_PARENT));
}

From source file:org.kei.android.phone.cellhistory.fragments.NetworkFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getBaseContext());
    chartSeparator = (LinearLayout) getView().findViewById(R.id.chartSeparator);
    txtTxBytesSinceAppStart = (TextView) getView().findViewById(R.id.txtTxBytesSinceAppStart);
    txtRxBytesSinceAppStart = (TextView) getView().findViewById(R.id.txtRxBytesSinceAppStart);
    txtDataConnectivity = (TextView) getView().findViewById(R.id.txtDataConnectivity);
    txtDataActivity = (TextView) getView().findViewById(R.id.txtDataActivity);
    txtTheoreticalSpeed = (TextView) getView().findViewById(R.id.txtTheoreticalSpeed);
    txtIp4Address = (TextView) getView().findViewById(R.id.txtIp4Address);
    txtIp6Address = (TextView) getView().findViewById(R.id.txtIp6Address);
    defaultColor = new TextView(getActivity()).getTextColors().getDefaultColor();
    redColor = getResources().getColor(R.color.red);
    greenColor = getResources().getColor(R.color.green);
    orangeColor = getResources().getColor(R.color.orange_dark);
    gradientColor = new LinearGradient(180, 0, 0, 0, new int[] { greenColor, redColor }, new float[] { 0, 1 },
            TileMode.CLAMP);// w  ww  .  java  2 s. c o  m

    chart = new TimeChartHelper();
    chart.setChartContainer((LinearLayout) getView().findViewById(R.id.graph));
    chart.setFrequency(Integer.parseInt(
            prefs.getString(PreferencesTimers.PREFS_KEY_TIMERS_UI, PreferencesTimers.PREFS_DEFAULT_TIMERS_UI)));
    chart.install(getActivity(), txtDataConnectivity.getTextColors().getDefaultColor(), false, 2, true);
    try {
        processUI(CellHistoryApp.getApp(getActivity()).getGlobalTowerInfo());
    } catch (Throwable e) {
        Log.e(getClass().getSimpleName(), "Exception: " + e.getMessage(), e);
    }
}

From source file:de.vanita5.twittnuker.view.ProfileBannerImageView.java

@SuppressLint("DrawAllocation")
@Override/*from   w w w .j  ava  2 s.c  o  m*/
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
    final int width = MeasureSpec.getSize(widthMeasureSpec), height = width / 3;
    setMeasuredDimension(width, height);
    if (width > 0) {
        mShader = new LinearGradient(width / 2, 0, width / 2, height, COLORS, POSITIONS, Shader.TileMode.CLAMP);
    }
    super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
}

From source file:org.zeroxlab.benchmark.Benchmark.java

public static Shader createGradient() {
    Random random = new Random();
    int color1 = (0x33252525 | random.nextInt());
    int color2 = (0x33252525 | random.nextInt());
    int color3 = (0x33252525 | random.nextInt());
    LinearGradient lg = new LinearGradient(0, 0, 100, 100, new int[] { color1, color2, color3 }, null,
            Shader.TileMode.REPEAT);/*from  ww  w.  ja va2  s  .c  o  m*/
    return lg;
}