Android Open Source - Pimp_my_Z1 Bar Graph






From Project

Back to project page Pimp_my_Z1.

License

The source code is released under:

GNU General Public License

If you think the Android project Pimp_my_Z1 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

/*
 *      Created by Daniel Nadeau//from  w  w w.j  a va 2  s . c  om
 *      daniel.nadeau01@gmail.com
 *      danielnadeau.blogspot.com
 * 
 *      Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.
 */

package com.echo.holographlibrary;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Region;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class BarGraph extends View {

  private ArrayList<Bar> points = new ArrayList<Bar>();
  private Paint p = new Paint();
  private Path path = new Path();
  private Rect r;
  private boolean showBarText = true;
  private int indexSelected = -1;
  private OnBarClickedListener listener;
  private Bitmap fullImage;
  private boolean shouldUpdate = false;
    public static String unit = "$";


  public BarGraph(Context context) {
    super(context);
  }

  public BarGraph(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public void setShowBarText(boolean show) {
    showBarText = show;
  }

  public void setBars(ArrayList<Bar> points) {
    this.points = points;
    postInvalidate();
  }

  public ArrayList<Bar> getBars() {
    return this.points;
  }

    public static String getUnit() {
        return unit;
    }

    public static void setUnit(String unit) {
        unit = unit;
    }

  public void onDraw(Canvas ca) {

    if (fullImage == null || shouldUpdate) {
      fullImage = Bitmap.createBitmap(getWidth(), getHeight(),
          Config.ARGB_8888);
      Canvas canvas = new Canvas(fullImage);
      canvas.drawColor(Color.TRANSPARENT);
      NinePatchDrawable popup = (NinePatchDrawable) this.getResources()
          .getDrawable(R.drawable.popup_black);

      float maxValue = 0;
      float padding = 7;
      int selectPadding = 4;
      float bottomPadding = 40;

      float usableHeight;
      if (showBarText) {
        this.p.setTextSize(40);
        Rect r3 = new Rect();
        this.p.getTextBounds("m", 0, 1, r3);
        usableHeight = getHeight() - bottomPadding
            - Math.abs(r3.top - r3.bottom) - 26;
      } else {
        usableHeight = getHeight() - bottomPadding;
      }

      p.setColor(Color.BLACK);
      p.setStrokeWidth(2);
      p.setAlpha(50);
      p.setAntiAlias(true);

      canvas.drawLine(0, getHeight() - bottomPadding + 10, getWidth(),
          getHeight() - bottomPadding + 10, p);

      float barWidth = (getWidth() - (padding * 2) * points.size())
          / points.size();

      for (Bar p : points) {
        maxValue += p.getValue();
      }

      r = new Rect();

      path.reset();

      int count = 0;
      for (Bar p : points) {
        r.set((int) ((padding * 2) * count + padding + barWidth * count),
            (int) (getHeight() - bottomPadding - (usableHeight * (p
                .getValue() / maxValue))), (int) ((padding * 2)
                * count + padding + barWidth * (count + 1)),
            (int) (getHeight() - bottomPadding));

        path.addRect(new RectF(r.left - selectPadding, r.top
            - selectPadding, r.right + selectPadding, r.bottom
            + selectPadding), Path.Direction.CW);
        p.setPath(path);
        p.setRegion(new Region(r.left - selectPadding, r.top
            - selectPadding, r.right + selectPadding, r.bottom
            + selectPadding));

        this.p.setColor(p.getColor());
        this.p.setAlpha(255);
        canvas.drawRect(r, this.p);
        this.p.setTextSize(20);
        canvas.drawText(p.getName(),
            (int) (((r.left + r.right) / 2) - (this.p.measureText(p
                .getName()) / 2)), getHeight() - 5, this.p);
        if (showBarText) {
          this.p.setTextSize(40);
          this.p.setColor(Color.WHITE);
          Rect r2 = new Rect();
          this.p.getTextBounds(p.getValue() + "m", 0, 1, r2);
          popup.setBounds(
              (int) (((r.left + r.right) / 2) - (this.p
                  .measureText(p.getValue() + "m") / 2)) - 14,
              r.top + (r2.top - r2.bottom) - 26,
              (int) (((r.left + r.right) / 2) + (this.p
                  .measureText(p.getValue() + "m") / 2)) + 14,
              r.top);
          popup.draw(canvas);
          canvas.drawText(p.getValue() + "m",
              (int) (((r.left + r.right) / 2) - (this.p
                  .measureText(p.getValue() + "m") / 2)),
              r.top - 20, this.p);
        }
        if (indexSelected == count && listener != null) {
          this.p.setColor(Color.parseColor("#33B5E5"));
          this.p.setAlpha(100);
          canvas.drawPath(p.getPath(), this.p);
          this.p.setAlpha(255);
        }
        count++;
      }
      shouldUpdate = false;
    }

    ca.drawBitmap(fullImage, 0, 0, null);

  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {

    Point point = new Point();
    point.x = (int) event.getX();
    point.y = (int) event.getY();

    int count = 0;
    for (Bar bar : points) {
      Region r = new Region();
      r.setPath(bar.getPath(), bar.getRegion());
      if (r.contains((int) point.x, (int) point.y)
          && event.getAction() == MotionEvent.ACTION_DOWN) {
        indexSelected = count;
      } else if (event.getAction() == MotionEvent.ACTION_UP) {
        if (r.contains((int) point.x, (int) point.y)
            && listener != null) {
          listener.onClick(indexSelected);
        }
        indexSelected = -1;
      }
      count++;
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN
        || event.getAction() == MotionEvent.ACTION_UP) {
      shouldUpdate = true;
      postInvalidate();
    }

    return true;
  }

  public void setOnBarClickedListener(OnBarClickedListener listener) {
    this.listener = listener;
  }

  public interface OnBarClickedListener {
    abstract void onClick(int index);
  }
}




Java Source Code List

com.androguide.honamicontrol.MainActivity.java
com.androguide.honamicontrol.PresentationActivity.java
com.androguide.honamicontrol.SettingsActivity.java
com.androguide.honamicontrol.bootservice.BootHelper.java
com.androguide.honamicontrol.bootservice.BootReceiver.java
com.androguide.honamicontrol.cards.CardDoubleSeekBarPA.java
com.androguide.honamicontrol.cards.CardDoubleSeekBar.java
com.androguide.honamicontrol.cards.CardImageLocal.java
com.androguide.honamicontrol.cards.CardSeekBarGeneric.java
com.androguide.honamicontrol.cards.CardSeekBarReadahead.java
com.androguide.honamicontrol.cards.CardSeekBarSC.java
com.androguide.honamicontrol.cards.CardSeekBarSysctl.java
com.androguide.honamicontrol.cards.CardSeekBarVibrator.java
com.androguide.honamicontrol.cards.CardSeekBar.java
com.androguide.honamicontrol.cards.CardSpinnerSchedMCDisabled.java
com.androguide.honamicontrol.cards.CardSpinnerSchedMC.java
com.androguide.honamicontrol.cards.CardSpinnerVoltage.java
com.androguide.honamicontrol.cards.CardSpinner.java
com.androguide.honamicontrol.cards.CardSwitchDisabled.java
com.androguide.honamicontrol.cards.CardSwitchPlugin.java
com.androguide.honamicontrol.cards.CardTopImageLocal.java
com.androguide.honamicontrol.fragments.CardsFragment.java
com.androguide.honamicontrol.fragments.HelpFragment.java
com.androguide.honamicontrol.fragments.WelcomeFragment.java
com.androguide.honamicontrol.helpers.CPUHelper.java
com.androguide.honamicontrol.helpers.FileHelper.java
com.androguide.honamicontrol.helpers.Helpers.java
com.androguide.honamicontrol.helpers.NegativeSeekBar.java
com.androguide.honamicontrol.helpers.ShellInterface.java
com.androguide.honamicontrol.helpers.SystemPropertiesReflection.java
com.androguide.honamicontrol.helpers.CMDProcessor.AbstractAsyncSuCMDProcessor.java
com.androguide.honamicontrol.helpers.CMDProcessor.CMDProcessor.java
com.androguide.honamicontrol.helpers.CMDProcessor.ChildProcess.java
com.androguide.honamicontrol.helpers.CMDProcessor.CommandResult.java
com.androguide.honamicontrol.helpers.CMDProcessor.Constants.java
com.androguide.honamicontrol.helpers.CMDProcessor.Shell.java
com.androguide.honamicontrol.kernel.KernelFragment.java
com.androguide.honamicontrol.kernel.colorcontrol.ColorControlActivity.java
com.androguide.honamicontrol.kernel.colorcontrol.ColorControlInterface.java
com.androguide.honamicontrol.kernel.cpucontrol.CPUActivity.java
com.androguide.honamicontrol.kernel.cpucontrol.CPUFragment.java
com.androguide.honamicontrol.kernel.cpucontrol.CPUInterface.java
com.androguide.honamicontrol.kernel.cpucontrol.GovernorDialog.java
com.androguide.honamicontrol.kernel.gpucontrol.GPUActivity.java
com.androguide.honamicontrol.kernel.gpucontrol.GPUInterface.java
com.androguide.honamicontrol.kernel.iotweaks.IOTweaksActivity.java
com.androguide.honamicontrol.kernel.iotweaks.IOTweaksInterface.java
com.androguide.honamicontrol.kernel.memory.MemoryManagementActivity.java
com.androguide.honamicontrol.kernel.memory.MemoryManagementInterface.java
com.androguide.honamicontrol.kernel.misc.MiscActivity.java
com.androguide.honamicontrol.kernel.misc.MiscInterface.java
com.androguide.honamicontrol.kernel.powermanagement.PowerManagementActivity.java
com.androguide.honamicontrol.kernel.powermanagement.PowerManagementInterface.java
com.androguide.honamicontrol.kernel.voltagecontrol.VoltageActivity.java
com.androguide.honamicontrol.kernel.voltagecontrol.VoltageInterface.java
com.androguide.honamicontrol.profiles.BalancedProfile.java
com.androguide.honamicontrol.profiles.BatteryMaxProfile.java
com.androguide.honamicontrol.profiles.BatteryProfile.java
com.androguide.honamicontrol.profiles.BenchmarkProfile.java
com.androguide.honamicontrol.profiles.PerformanceProfile.java
com.androguide.honamicontrol.profiles.ProfileEnabler.java
com.androguide.honamicontrol.profiles.Profile.java
com.androguide.honamicontrol.soundcontrol.SoundControlActivity.java
com.androguide.honamicontrol.soundcontrol.SoundControlFragment.java
com.androguide.honamicontrol.soundcontrol.SoundControlInterface.java
com.androguide.honamicontrol.soundcontrol.SoundFragment.java
com.androguide.honamicontrol.touchscreen.TouchScreenFragment.java
com.androguide.honamicontrol.touchscreen.TouchScreenInterface.java
com.astuetz.viewpager.extensions.PagerSlidingTabStrip.java
com.echo.holographlibrary.BarGraph.java
com.echo.holographlibrary.Bar.java
com.echo.holographlibrary.LineGraph.java
com.echo.holographlibrary.LinePoint.java
com.echo.holographlibrary.Line.java
com.echo.holographlibrary.PieGraph.java
com.echo.holographlibrary.PieSlice.java
com.fima.cardsui.StackAdapter.java
com.fima.cardsui.SwipeDismissTouchListener.java
com.fima.cardsui.Utils.java
com.fima.cardsui.objects.AbstractCard.java
com.fima.cardsui.objects.CardStack.java
com.fima.cardsui.objects.Card.java
com.fima.cardsui.views.CardCategory.java
com.fima.cardsui.views.CardDownload.java
com.fima.cardsui.views.CardEditText.java
com.fima.cardsui.views.CardEdit.java
com.fima.cardsui.views.CardImage.java
com.fima.cardsui.views.CardPresentation.java
com.fima.cardsui.views.CardSeekBar.java
com.fima.cardsui.views.CardSpinner.java
com.fima.cardsui.views.CardTextStripe.java
com.fima.cardsui.views.CardText.java
com.fima.cardsui.views.CardTopImage.java
com.fima.cardsui.views.CardUI.java
com.fima.cardsui.views.QuickReturnListView.java