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:com.FluksoViz.FluksoVizActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    Display display = getWindowManager().getDefaultDisplay();
    screen_width = display.getWidth();/*from  www  . ja  v  a 2  s  . c  o  m*/
    if (screen_width == 320) {
        setContentView(R.layout.main_lowres);
    } else
        setContentView(R.layout.main);

    context = getApplicationContext();
    SharedPreferences my_app_prefs = PreferenceManager.getDefaultSharedPreferences(this);

    try {
        versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    sensor_number = Integer.parseInt(my_app_prefs.getString("sensor_number", "1"));
    api_server_ip = my_app_prefs.getString("api_server", "178.79.177.6");
    skip_initial_sensor_checks = (Boolean) my_app_prefs.getBoolean("skip_initial_sensor_checks", false);
    ip_addr = (String) my_app_prefs.getString("flukso_ip_addr", "10.10.10.10");
    api_key_1 = (String) my_app_prefs.getString("sensor_1_api_key", "0.0.0.0");
    api_key_2 = (String) my_app_prefs.getString("sensor_2_api_key", "0.0.0.0");
    api_key_3 = (String) my_app_prefs.getString("sensor_3_api_key", "0.0.0.0");
    api_token_1 = (String) my_app_prefs.getString("sensor_1_token", "0.0.0.0");
    api_token_2 = (String) my_app_prefs.getString("sensor_2_token", "0.0.0.0");
    api_token_3 = (String) my_app_prefs.getString("sensor_3_token", "0.0.0.0");
    cost_fixedpart = Double.parseDouble(my_app_prefs.getString("cost_perkwh", "0"));
    cost_perkwh = Double.parseDouble(my_app_prefs.getString("cost_perkwh", "0"));
    cost_currencycode = (String) my_app_prefs.getString("cost_currencycode", "PLN");

    Napis = (TextView) findViewById(R.id.textView1);
    Napis2 = (TextView) findViewById(R.id.textView2);
    Napis01 = (TextView) findViewById(R.id.textView01);
    Napis3 = (TextView) findViewById(R.id.textView_r1);
    Napis4 = (TextView) findViewById(R.id.textView_rd1);
    tv_p1 = (TextView) findViewById(R.id.textView_p1);
    tv_p1.setTextColor(Color.WHITE);
    tv_p1.setVisibility(TextView.INVISIBLE);
    tv_p2 = (TextView) findViewById(R.id.textView_p2);
    tv_p2.setTextColor(Color.WHITE);
    tv_p2.setVisibility(TextView.INVISIBLE);
    tv_p3 = (TextView) findViewById(R.id.textView_p3);
    tv_p3.setTextColor(Color.WHITE);
    tv_p3.setVisibility(TextView.INVISIBLE);
    tv_today_kwh = (TextView) findViewById(R.id.TextView_r2);
    tv_today_cost = (TextView) findViewById(R.id.TextView_r4);
    tv_today_percent = (TextView) findViewById(R.id.TextView_r6);
    tv_today_avg = (TextView) findViewById(R.id.TextView_r22);
    tv_week_kwh = (TextView) findViewById(R.id.TextView_rd2);
    tv_week_avg = (TextView) findViewById(R.id.TextView_rd22);
    tv_week_cost = (TextView) findViewById(R.id.TextView_rd4);
    tv_week_percent = (TextView) findViewById(R.id.TextView_rd6);

    tv_month_kwh = (TextView) findViewById(R.id.TextView_rt2);
    tv_month_avg = (TextView) findViewById(R.id.TextView_rt22);
    tv_month_cost = (TextView) findViewById(R.id.TextView_rt4);
    tv_month_percent = (TextView) findViewById(R.id.TextView_rt6);

    tv_curr1 = (TextView) findViewById(R.id.TextView_r5);
    tv_curr2 = (TextView) findViewById(R.id.TextView_rd5);
    tv_curr3 = (TextView) findViewById(R.id.TextView_rt5);
    tv_curr1.setText(cost_currencycode);
    tv_curr2.setText(cost_currencycode);
    tv_curr3.setText(cost_currencycode);

    Napis01.setText("" + sensor_number);

    iv1 = (ImageView) findViewById(R.id.arrow_image1);
    iv2 = (ImageView) findViewById(R.id.arrow_image2);
    iv3 = (ImageView) findViewById(R.id.arrow_image3);

    W = (TextView) findViewById(R.id.textView4);
    napis_delta = (TextView) findViewById(R.id.textView_delta);
    napis_delta.setText("" + (char) 0x0394);
    napis_delta.setTextColor(Color.WHITE);
    napis_delta.setVisibility(TextView.INVISIBLE);

    Plot1 = (XYPlot) findViewById(R.id.Plot1);
    Plot2 = (XYPlot) findViewById(R.id.Plot2);

    series1m = new SimpleXYSeries("seria 1m");
    series2m = new SimpleXYSeries("seria 2m");
    series3m = new SimpleXYSeries("seria 3m");

    series_p2_1 = new SimpleXYSeries("plot 2 - 1");

    series1mFormat = new LineAndPointFormatter(Color.rgb(0, 180, 0), // line
            Color.rgb(50, 100, 0), // point color
            null);
    line1mFill = new Paint();
    line1mFill.setAlpha(100);
    line1mFill.setShader(
            new LinearGradient(0, 0, 0, 200, Color.rgb(0, 100, 0), Color.BLACK, Shader.TileMode.MIRROR));
    series1mFormat.getLinePaint().setStrokeWidth(3);
    series1mFormat.getVertexPaint().setStrokeWidth(0);
    series1mFormat.setFillPaint(line1mFill);

    series2mFormat = new LineAndPointFormatter( // FAZA 2 formater
            Color.rgb(0, 200, 0), // line color
            Color.rgb(0, 100, 50), // point color
            null);
    line2mFill = new Paint();
    line2mFill.setAlpha(100);
    line2mFill.setShader(
            new LinearGradient(0, 0, 0, 200, Color.rgb(0, 100, 0), Color.BLACK, Shader.TileMode.MIRROR));
    series2mFormat.getLinePaint().setStrokeWidth(3);
    series2mFormat.getVertexPaint().setStrokeWidth(0);
    series2mFormat.setFillPaint(line2mFill);

    series3mFormat = new LineAndPointFormatter( // FAZA 3 formater
            Color.rgb(0, 220, 0), // line color
            Color.rgb(0, 150, 0), // point color
            null);
    line3mFill = new Paint();
    line3mFill.setAlpha(100);
    line3mFill.setShader(
            new LinearGradient(0, 0, 0, 200, Color.rgb(0, 200, 0), Color.BLACK, Shader.TileMode.MIRROR));
    series3mFormat.getLinePaint().setStrokeWidth(3);
    // series3mFormat.getVertexPaint().setStrokeWidth(0);
    series3mFormat.setFillPaint(line3mFill);

    series4mFormat = new LineAndPointFormatter(Color.rgb(0, 140, 220), // line
            Color.rgb(0, 120, 190), // point color
            null);
    line4mFill = new Paint();
    line4mFill.setAlpha(190);
    line4mFill.setShader(
            new LinearGradient(0, 0, 0, 200, Color.rgb(0, 140, 220), Color.BLACK, Shader.TileMode.MIRROR));
    series4mFormat.getLinePaint().setStrokeWidth(5);
    series4mFormat.setFillPaint(line4mFill);

    make_graph_pretty(Plot1); // All formating of the graph goes into
    // seperate method
    make_graph_pretty(Plot2);

    Napis.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (delta_mode) {
                napis_delta.setVisibility(TextView.INVISIBLE);
                delta_mode = false;
                delta_value = 0;
                // Plot1.removeMarker(marker1);
            } else {
                napis_delta.setVisibility(TextView.VISIBLE);
                delta_mode = true;
                try {
                    delta_value = seriesSUM123linkedlist.getLast().intValue();
                    // marker1 = new YValueMarker(delta_value, "" + (char)
                    // 0x0394, new
                    // XPositionMetric(3,XLayoutStyle.ABSOLUTE_FROM_LEFT),
                    // Color.GREEN, Color.WHITE);
                    // Plot1.addMarker(marker1);
                } catch (NullPointerException e) {
                    delta_value = 0;
                }
            }
            ;

        }
    });

    W.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Napis01.setText("restarted");
            thread1_running = true;
            thread2_running = true;

        }
    });

    Plot1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            /*
             * Change the switch block to an increment and one 
             * if statement. Also, avoid magic numbers
             */
            plot1_mode++;
            if (plot1_mode > MAX_PLOT1_CLICK)
                plot1_mode = 0;

            switch (sensor_number) {
            case 1:
            case 2:
            case 3: {
                switch (plot1_mode) {
                case 0: {
                    Plot1.setTitle("Power (W) - last minute -  stacked");
                    Plot1.removeSeries(series1m);
                    Plot1.addSeries(series2m, series2mFormat);
                    Plot1.addSeries(series3m, series3mFormat);
                    Plot1.addSeries(series1m, series1mFormat);
                    Plot1.redraw();
                    break;
                }
                case 1: {
                    Plot1.setTitle("Power (W) - last minute -  with details");
                    Plot1.redraw(); // update title even if series are not updating (like in set prefs)

                    setDetailsVisibility(true); // show details
                    break;
                }
                case 2: {
                    Plot1.setTitle("Power (W) - last minute -  Total only");
                    Plot1.removeSeries(series2m);
                    Plot1.removeSeries(series1m);
                    Plot1.redraw();

                    setDetailsVisibility(false); // hide details
                    break;
                }
                }
                break;
            }
            case 4: {
                switch (plot1_mode) {
                case 0: {
                    Plot1.setTitle("Power (W) - last minute -  stacked");
                    Plot1.redraw();
                    break;
                }
                case 1: {
                    Plot1.setTitle("Power (W) - last minute -  with details");
                    Plot1.redraw(); // This update plot title even if the series update is stoped

                    setDetailsVisibility(true); // show details
                    break;
                }
                case 2: {
                    Plot1.setTitle("Power (W) - last minute ");
                    // Plot1.removeSeries(series2m);
                    // Plot1.removeSeries(series1m);
                    Plot1.redraw();

                    setDetailsVisibility(false); // hide details
                    break;
                }
                }
                break;
            }
            }

        }
    });

    series1m.setModel(series1linkedlist, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
    series2m.setModel(series2linkedlist, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
    series3m.setModel(series3linkedlist, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);

    Plot1.addSeries(series2m, series2mFormat);
    Plot1.addSeries(series3m, series3mFormat);
    Plot1.addSeries(series1m, series1mFormat);

    series_p2_1.setModel(series_day1_linkedlist, SimpleXYSeries.ArrayFormat.XY_VALS_INTERLEAVED);
    Plot2.addSeries(series_p2_1, series4mFormat);

    if (skip_initial_sensor_checks) {
        thread_updater1s.start();
        thread_updater2.start();
    } else {
        run_network_token_test();
        // Alert dialog when application starts
        new AlertDialog.Builder(this).setTitle(R.string.nw_chk_results).setMessage(network_checks_results)
                .setIcon(android.R.drawable.ic_menu_agenda)
                .setPositiveButton(R.string.run_both_th_local_remote, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        thread_updater1s.start();
                        thread_updater2.start();
                    }
                }).setNeutralButton(R.string.run_just_local_th, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        thread_updater1s.start();
                        Plot2.setTitle(getString(R.string.disabled));
                        tv_today_kwh.setVisibility(TextView.INVISIBLE);
                        tv_today_cost.setVisibility(TextView.INVISIBLE);
                        tv_today_percent.setVisibility(TextView.INVISIBLE);
                        tv_today_avg.setVisibility(TextView.INVISIBLE);
                        tv_week_kwh.setVisibility(TextView.INVISIBLE);
                        tv_week_avg.setVisibility(TextView.INVISIBLE);
                        tv_week_cost.setVisibility(TextView.INVISIBLE);
                        tv_week_percent.setVisibility(TextView.INVISIBLE);
                        tv_month_kwh.setVisibility(TextView.INVISIBLE);
                        tv_month_avg.setVisibility(TextView.INVISIBLE);
                        tv_month_cost.setVisibility(TextView.INVISIBLE);
                        tv_month_percent.setVisibility(TextView.INVISIBLE);
                    }
                })
                .setNegativeButton(R.string.let_me_fix_the_prefs_first, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                    }
                }).show();
    } // end of if for skip initial tests

}

From source file:github.madmarty.madsonic.util.ImageLoader.java

private Bitmap createReflection(Bitmap originalImage) {

    //   int reflectionH = 80;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    // Height of reflection
    int reflectionHeight = height / 2;

    // The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + reflectionHeight),
            Bitmap.Config.ARGB_8888);// w ww .  j av a2  s  .com

    //// ----

    Bitmap reflection = Bitmap.createBitmap(width, reflectionHeight, Bitmap.Config.ARGB_8888);
    Bitmap blurryBitmap = Bitmap.createBitmap(originalImage, 0, height - reflectionHeight, height,
            reflectionHeight);

    // cheap and easy scaling algorithm; down-scale it, then
    // upscale it. The filtering during the scale operations
    // will blur the resulting image
    blurryBitmap = Bitmap
            .createScaledBitmap(
                    Bitmap.createScaledBitmap(blurryBitmap, blurryBitmap.getWidth() / 2,
                            blurryBitmap.getHeight() / 2, true),
                    blurryBitmap.getWidth(), blurryBitmap.getHeight(), true);

    // This shadier will hold a cropped, inverted,
    // blurry version of the original image
    BitmapShader bitmapShader = new BitmapShader(blurryBitmap, TileMode.CLAMP, TileMode.CLAMP);
    Matrix invertMatrix = new Matrix();
    invertMatrix.setScale(1f, -1f);
    invertMatrix.preTranslate(0, -reflectionHeight);
    bitmapShader.setLocalMatrix(invertMatrix);

    // This shader holds an alpha gradient
    Shader alphaGradient = new LinearGradient(0, 0, 0, reflectionHeight, 0x80ffffff, 0x00000000,
            TileMode.CLAMP);

    // This shader combines the previous two, resulting in a
    // blurred, fading reflection
    ComposeShader compositor = new ComposeShader(bitmapShader, alphaGradient, PorterDuff.Mode.DST_IN);

    Paint reflectionPaint = new Paint();
    reflectionPaint.setShader(compositor);

    // Draw the reflection into the bitmap that we will return
    Canvas canvas = new Canvas(reflection);
    canvas.drawRect(0, 0, reflection.getWidth(), reflection.getHeight(), reflectionPaint);

    /// -----

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas finalcanvas = new Canvas(bitmapWithReflection);

    // Draw in the original image
    finalcanvas.drawBitmap(originalImage, 0, 0, null);

    // Draw in the gap
    Paint defaultPaint = new Paint();

    // transparent gap
    defaultPaint.setColor(0);

    finalcanvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

    // Draw in the reflection
    finalcanvas.drawBitmap(reflection, 0, height + reflectionGap, null);

    return bitmapWithReflection;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Drawable createRectGradient(final int direction, final int[] colors) {

    final ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
    final ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {

        @Override// www . j a  v  a2s.  c  om
        public Shader resize(int width, int height) {
            int x0, y0, x1, y1;
            x0 = y0 = x1 = y1 = 0;
            if (direction == GRADIENT_DOWN) {
                y1 = height;
            } else if (direction == GRADIENT_UP) {
                y0 = height;
            } else if (direction == GRADIENT_RIGHT) {
                x1 = width;
            } else if (direction == GRADIENT_LEFT) {
                x0 = width;
            }
            LinearGradient lg = new LinearGradient(x0, y0, x1, y1, colors, new float[] { 0, 0.05f, 0.1f, 1 },
                    Shader.TileMode.REPEAT);
            return lg;
        }
    };
    shapeDrawable.setShaderFactory(shaderFactory);

    return shapeDrawable;
}

From source file:com.richtodd.android.quiltdesign.block.PaperPiecedBlockPiece.java

private Paint createShadowPaint(int left, int top, int width, int height, int shadowStrokeWidth) {
    List<PointF> points = getPoints(width, height);
    PointF fromPoint = new PointF(left + points.get(0).x, top + points.get(0).y);
    PointF toPoint = new PointF(left + points.get(1).x, top + points.get(1).y);

    PointF midpoint = new PointF((fromPoint.x + toPoint.x) * 0.5f, (fromPoint.y + toPoint.y) * 0.5f);

    PointF vector = new PointF(fromPoint.x - midpoint.x, fromPoint.y - midpoint.y);

    PointF rotatedVector = new PointF(vector.y, -vector.x);

    float vectorLength = (float) Math.sqrt((vector.x * vector.x) + (vector.y * vector.y));

    PointF shadowVector = new PointF(rotatedVector.x * (shadowStrokeWidth * 0.5f) / vectorLength,
            rotatedVector.y * (shadowStrokeWidth * 0.5f) / vectorLength);

    LinearGradient gradient = new LinearGradient(midpoint.x, midpoint.y, midpoint.x + shadowVector.x,
            midpoint.y + shadowVector.y, Color.argb(100, 0, 0, 0), Color.argb(0, 0, 0, 0), TileMode.MIRROR);

    Paint paint = new Paint();
    paint.setStyle(Style.STROKE);
    paint.setShader(gradient);/*  w  w w  .j  a va 2 s  .c  om*/
    paint.setStrokeWidth(shadowStrokeWidth);
    paint.setStrokeCap(Cap.SQUARE);

    return paint;
}

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Drawable createLGradient(int shape, final int[] colors, final float[] positions) {

    ShapeDrawable shapeDrawable;/*from w ww  .j a  v  a  2 s  . c  om*/
    if (shape == SHAPE_RECT) {
        shapeDrawable = new ShapeDrawable(new RectShape());
    } else {
        shapeDrawable = new ShapeDrawable(new OvalShape());
    }

    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {

        @Override
        public Shader resize(int width, int height) {
            LinearGradient rg = new LinearGradient(0, 0, 0, height, colors, positions, Shader.TileMode.CLAMP);

            return rg;
        }
    };
    shapeDrawable.setShaderFactory(shaderFactory);

    return shapeDrawable;
}

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/*from  w  ww . j  a  v  a  2  s  . co m*/
        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.yk.notification.util.BitmapUtil.java

/**
 * //  ww  w . j a v  a 2s  .  com
 * 
 * @param bitmap
 *            ?Bitmap
 * @return Bitmap
 */
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

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

private void drawAlphaPanel(final Canvas canvas) {

    if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
        return;//from  w  w w.ja va  2 s .c o  m

    final RectF rect = mAlphaRect;

    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX,
                rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }

    mAlphaPattern.draw(canvas);

    final float[] hsv = new float[] { mHue, mSat, mVal };
    final int color = Color.HSVToColor(hsv);
    final int acolor = Color.HSVToColor(0, hsv);

    mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);

    mAlphaPaint.setShader(mAlphaShader);

    canvas.drawRect(rect, mAlphaPaint);

    if (mAlphaSliderText != null && mAlphaSliderText != "") {
        canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
    }

    final float rectWidth = 4 * mDensity / 2;

    final Point p = alphaToPoint(mAlpha);

    final RectF r = new RectF();
    r.left = p.x - rectWidth;
    r.right = p.x + rectWidth;
    r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
    r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;

    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);

}

From source file:scroll.com.rdemo.scroll.PagerSlidingTabStrip.java

@Override
public void draw(Canvas canvas) {
    super.draw(canvas);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return;/*ww  w . j  a  v a  2  s . c  o  m*/
    }
    int scrollX = getScrollX();
    //        Loger.i(TAG, "draw:getWidth=" + getWidth() + "," + tabsContainer.getWidth() + ",scrollX=" + scrollX);
    int right = getRight();
    if (getWidth() + scrollX != tabsContainer.getWidth()) {
        Shader mShader = new LinearGradient(scrollX + right - SizeUtils.Dp2Px(this.getContext(), 50),
                getHeight() / 2, scrollX + right, getHeight() / 2, 0, 0xFFF3F3F3, Shader.TileMode.CLAMP);
        shaderPaint.setShader(mShader);
        canvas.drawRect(scrollX + (right - SizeUtils.Dp2Px(this.getContext(), 50)), 0, scrollX + right,
                getHeight(), shaderPaint);
    }
    if (scrollX > 0) {
        Shader mShader = new LinearGradient(scrollX, getHeight() / 2,
                scrollX + SizeUtils.Dp2Px(this.getContext(), 50), getHeight() / 2, 0xFFF3F3F3, 0,
                Shader.TileMode.CLAMP);
        shaderPaint.setShader(mShader);
        canvas.drawRect(scrollX, 0, scrollX + SizeUtils.Dp2Px(this.getContext(), 50), getHeight(), shaderPaint);
    }
}

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

private void drawHuePanel(final Canvas canvas) {

    final RectF rect = mHueRect;

    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX,
                rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }//from  w  w  w  .j ava  2s .com

    if (mHueShader == null) {
        mHueShader = new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, buildHueColorArray(), null,
                TileMode.CLAMP);
        mHuePaint.setShader(mHueShader);
    }

    canvas.drawRect(rect, mHuePaint);

    final float rectHeight = 4 * mDensity / 2;

    final Point p = hueToPoint(mHue);

    final RectF r = new RectF();
    r.left = rect.left - RECTANGLE_TRACKER_OFFSET;
    r.right = rect.right + RECTANGLE_TRACKER_OFFSET;
    r.top = p.y - rectHeight;
    r.bottom = p.y + rectHeight;

    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);

}