Example usage for android.graphics.drawable PaintDrawable setShape

List of usage examples for android.graphics.drawable PaintDrawable setShape

Introduction

In this page you can find the example usage for android.graphics.drawable PaintDrawable setShape.

Prototype

public void setShape(Shape s) 

Source Link

Document

Sets the Shape of this ShapeDrawable.

Usage

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.// ww  w . java2 s  .  co 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:git.egatuts.nxtremotecontroller.activity.ControllerActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setActiveTheme(super.getPreferenceTheme());
    super.setContentView(R.layout.controller_layout);
    toolbar = (Toolbar) this.findViewById(R.id.toolbar);
    super.setSupportToolbar();
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override/* w ww.  j  a  v a2 s.com*/
        public void onClick(View v) {
            ControllerActivity.this.onBackPressed();
        }
    });

    /*
     *  We get the device from the extra data of the intent.
     */
    this.device = this.getIntent().getParcelableExtra("device");

    /*
     *  We create the final variables we will use in the listeners, etc.
     */
    final ControllerActivity self = this;
    final GlobalUtils utils = this.getGlobalUtils();

    /*
     *  We create the AlertDialog.Builder we will show to ask the user to reconnect with the device.
     */
    this.builder = utils
            .createAlertDialog(utils.getStringResource(R.string.connecting_reconnect_title),
                    utils.format(R.string.connecting_reconnect_message, this.device.getName()))
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    self.finish();
                }
            }).setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    self.resume();
                }
            });

    /*
     *  Now we define the progress dialog we will show while we are connecting with the device.
     *  When the progress dialog has been cancelled it means the connection process has been cancelled.
     *  But on dismiss we have to check if the connection failed or it succeed.
     */
    this.progressDialog = this.getLongProgressDialog();
    this.progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            self.aborted = true;
            utils.showToast(R.string.connecting_aborted);
            self.connector.closeConnectThread();
            self.finish();
        }
    });

    /*
     *  Now we declare the handler that will handle (so obviously) the messages
     *  sent by the threads that are in the background connecting with the device.
     */
    this.handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if ((self.connector.getConnectThread() == null || self.aborted)
                    && self.connector.getConnectedThread() == null) {
                return;
            }
            int category = msg.what;
            int state;
            int error;
            if (category == NXTConnector.WHAT_CHANGE_STATE) {
                progressDialog.show();
                state = msg.arg1;
                error = msg.arg2;
                if (NXTConnector.isPreparingConnection(state)) {
                    progressDialog.setText(R.string.connecting_preparing_connection);
                } else if (NXTConnector.isCreatingSocket(state)) {
                    progressDialog.setText(R.string.connecting_creating_socket);
                } else if (NXTConnector.isConnecting(state)) {
                    progressDialog.setText(R.string.connecting_connecting);
                } else if (NXTConnector.isConnected(state)) {
                    progressDialog.dismiss();
                    utils.showToast(R.string.connecting_connected, device.getName());
                    self.connected();
                }
            } else if (category == NXTConnector.WHAT_ERROR_ENCOUNTERED) {
                progressDialog.dismiss();
                self.connector.closeAllThreads();
                error = msg.arg1;
                state = msg.arg2;
                boolean notReconnect = false;
                if (NXTConnector.connectionClosed(state, error)) {
                    utils.showToast(R.string.connecting_connection_closed);
                    notReconnect = true;
                    ControllerActivity.this.finish();
                } else if (NXTConnector.connectionLost(state, error)) {
                    utils.showToast(R.string.connecting_connection_lost);
                    if (!self.connector.getBluetoothUtils().isEnabled()) {
                        notReconnect = true;
                        ControllerActivity.this.finish();
                    }
                } else if (NXTConnector.connectionSocketFailed(state, error)) {
                    utils.showToast(R.string.connecting_socket_error);
                } else if (NXTConnector.connectionRequestFailed(state, error)) {
                    utils.showToast(R.string.connecting_request_failed);
                } else if (NXTConnector.connectionUnexpectedFailed(state, error)) {
                    utils.showToast(R.string.connecting_unexpected_error);
                }
                if (!notReconnect) {
                    self.builder.show();
                }
            }
        }
    };

    /*
     *  Now we create the connector with the device and the handler
     *  which we will use to connect and send messages to the robot.
     */
    this.connector = new NXTConnector(this, this.device, this.handler);

    /*
     *  We set the title with the device name.
     */
    String title = utils.getStringResource(R.string.controller_window_title);
    this.setTitle(title + this.device.getName());

    /*
     *  We set the colors we will use with the drawables used in the tabs.
     */
    final int underlineColor = utils.getAttribute(R.attr.toolbar_color);
    final int backgroundColor = utils.getAttribute(R.attr.toolbar_background);
    final int lightBackgroundColor = GlobalUtils.mixColors(backgroundColor, 0x55FFFFFF);

    /*
     *  Now we create the drawables used in all the states of the tabs and then we assign
     *  them to a StateListDrawable to add it to the tabs.
     */
    ShapeDrawable.ShaderFactory tabSelectedFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(0, 0, /* Origin of the background (top left corner) */
                    0, height, /* End of the background (bottom left corner) */
                    new int[] { backgroundColor,
                            backgroundColor, /* The first gradient doesn't change color so it's like a rectangle shape */
                            underlineColor, underlineColor /* The same for the second one */
            }, new float[] { 0, 51f / 55f, /* The first background covers 51dp out of 55dp */
                    51f / 55f, 1 /* And the second one takes the rest of the space */
            }, Shader.TileMode.REPEAT /* The repeat mode doesn't mind but this would look prettier in case of error */
            );
        }
    };
    PaintDrawable tabSel = new PaintDrawable();
    tabSel.setShape(new RectShape());
    tabSel.setShaderFactory(tabSelectedFactory);

    ShapeDrawable.ShaderFactory tabSelectedAndPressedFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(0, 0, /* Origin of the background (top left corner) */
                    0, height, /* End of the background (bottom left corner) */
                    new int[] { lightBackgroundColor,
                            lightBackgroundColor, /* The first gradient doesn't change color so it's like a rectangle shape */
                            underlineColor, underlineColor /* The same for the second one */
            }, new float[] { 0, 51f / 55f, /* The first background covers 51dp out of 55dp */
                    51f / 55f, 1 /* And the second one takes the rest of the space */
            }, Shader.TileMode.REPEAT /* The repeat mode doesn't mind but this would look prettier in case of error */
            );
        }
    };
    PaintDrawable tabSelAndPress = new PaintDrawable();
    tabSelAndPress.setShape(new RectShape());
    tabSelAndPress.setShaderFactory(tabSelectedAndPressedFactory);

    /*
     *  Now we create the states lists for the drawables and the colors.
     */
    StateListDrawable drawableList = new StateListDrawable();
    drawableList.addState(new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed },
            new ColorDrawable(lightBackgroundColor));
    drawableList.addState(new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed,
            android.R.attr.state_focused }, new ColorDrawable(lightBackgroundColor));
    drawableList.addState(new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed }, tabSel);
    drawableList.addState(new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed,
            -android.R.attr.state_focused }, tabSel);
    drawableList.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_pressed },
            tabSelAndPress);
    drawableList.addState(new int[] { android.R.attr.state_selected, android.R.attr.state_pressed,
            android.R.attr.state_focused }, tabSelAndPress);
    drawableList.addState(new int[] {}, new ColorDrawable(backgroundColor));

    int darkColor = utils.getAttribute(R.attr.toolbar_color);
    int lightColor = Color.argb(0xAA, Color.red(darkColor), Color.green(darkColor), Color.blue(darkColor));
    int[][] states = new int[][] { new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed },
            new int[] { -android.R.attr.state_selected, android.R.attr.state_pressed,
                    android.R.attr.state_focused },
            new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed },
            new int[] { android.R.attr.state_selected, -android.R.attr.state_pressed,
                    -android.R.attr.state_focused },
            new int[] { android.R.attr.state_selected, android.R.attr.state_pressed },
            new int[] { android.R.attr.state_selected, android.R.attr.state_pressed,
                    android.R.attr.state_focused },
            new int[] { -android.R.attr.state_selected, -android.R.attr.state_pressed,
                    -android.R.attr.state_focused },
            new int[] {} };
    int[] colors = new int[] { lightColor, /* Text color when pressed and not selected */
            lightColor, /* Text color when pressed (with focused fallback) */
            darkColor, /* Text color when selected and not pressed */
            darkColor, /* Text color when selected and not pressed (with focused fallback) */
            darkColor, /* Text color when selected and pressed */
            darkColor, /* Text color when selected and pressed (with focused fallback) */
            lightColor, /* Normal color when not pressed, selected or focused */
            lightColor /* Default text color */
    };
    ColorStateList colorList = new ColorStateList(states, colors);

    /*
     *  We assign the drawable and the list to the activity instance to be accessible everywhere.
     */
    this.tabSelected = tabSel;
    this.tabSelectedAndPressed = tabSelAndPress;
    this.tabDrawableList = drawableList;
    this.tabColorList = colorList;

    /*
     *  Now we setup the tab host and add the tabs to the view.
     */
    this.tabHost = (FragmentTabHost) this.findViewById(R.id.tabhost);
    this.tabHost.setup(this, super.fragmentManager, R.id.tabcontent);
    this.tabHost.getTabWidget().setDividerDrawable(null);
    this.addTab(this.createTabView(R.layout.controller_tab, R.string.controller_tab_local_title),
            R.string.controller_tab_local_spec, LocalControllerFragment.class);
    this.addTab(this.createTabView(R.layout.controller_tab, R.string.controller_tab_online_title),
            R.string.controller_tab_online_spec, OnlineControllerFragment.class);
}

From source file:com.shollmann.igcparser.ui.activity.FlightPreviewActivity.java

public PaintDrawable getColorScala() {
    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
        @Override//  w ww . j av  a  2s . c om
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(width, height, 0, 0,
                    new int[] { getResources().getColor(R.color.altitude_0_100),
                            getResources().getColor(R.color.altitude_100_300),
                            getResources().getColor(R.color.altitude_300_500),
                            getResources().getColor(R.color.altitude_500_1000),
                            getResources().getColor(R.color.altitude_1000_1500),
                            getResources().getColor(R.color.altitude_1500_2000),
                            getResources().getColor(R.color.altitude_2000_2500),
                            getResources().getColor(R.color.altitude_more_than_2500) },
                    new float[] { 0, 0.07f, 0.14f, 0.28f, 0.42f, 0.56f, 0.7f, 0.84f }, Shader.TileMode.REPEAT);
            return linearGradient;
        }
    };

    PaintDrawable paint = new PaintDrawable();
    paint.setShape(new RectShape());
    paint.setShaderFactory(shaderFactory);

    return paint;
}