Android Open Source - helsinki-testbed2-android Alpha Pattern Drawable






From Project

Back to project page helsinki-testbed2-android.

License

The source code is released under:

GNU General Public License

If you think the Android project helsinki-testbed2-android 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

/*
 * Copyright (C) 2010 Daniel Nilsson//w  ww.j  a  v a2 s.com
 *
 * Licensed 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 net.margaritov.preference.colorpicker;

import android.graphics.*;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.Drawable;

/**
 * This drawable that draws a simple white and gray chessboard pattern.
 * It's pattern you will often see as a background behind a
 * partly transparent image in many applications.
 * @author Daniel Nilsson
 */
public class AlphaPatternDrawable extends Drawable {

  private int mRectangleSize = 10;

  private Paint mPaint = new Paint();
  private Paint mPaintWhite = new Paint();
  private Paint mPaintGray = new Paint();

  private int numRectanglesHorizontal;
  private int numRectanglesVertical;

  /**
   * Bitmap in which the pattern will be cahched.
   */
  private Bitmap    mBitmap;

  public AlphaPatternDrawable(int rectangleSize) {
    mRectangleSize = rectangleSize;
    mPaintWhite.setColor(0xffffffff);
    mPaintGray.setColor(0xffcbcbcb);
  }

  @Override
  public void draw(Canvas canvas) {
    canvas.drawBitmap(mBitmap, null, getBounds(), mPaint);
  }

  @Override
  public int getOpacity() {
    return 0;
  }

  @Override
  public void setAlpha(int alpha) {
    throw new UnsupportedOperationException("Alpha is not supported by this drawwable.");
  }

  @Override
  public void setColorFilter(ColorFilter cf) {
    throw new UnsupportedOperationException("ColorFilter is not supported by this drawwable.");
  }

  @Override
  protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);

    int height = bounds.height();
    int width = bounds.width();

    numRectanglesHorizontal = (int) Math.ceil((width / mRectangleSize));
    numRectanglesVertical = (int) Math.ceil(height / mRectangleSize);

    generatePatternBitmap();

  }

  /**
   * This will generate a bitmap with the pattern
   * as big as the rectangle we were allow to draw on.
   * We do this to chache the bitmap so we don't need to
   * recreate it each time draw() is called since it
   * takes a few milliseconds.
   */
  private void generatePatternBitmap(){

    if(getBounds().width() <= 0 || getBounds().height() <= 0){
      return;
    }
    
    mBitmap = Bitmap.createBitmap(getBounds().width(), getBounds().height(), Config.ARGB_8888);
    Canvas canvas = new Canvas(mBitmap);

    Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

      boolean isWhite = verticalStartWhite;
      for (int j = 0; j <= numRectanglesHorizontal; j++) {

        r.top = i * mRectangleSize;
        r.left = j * mRectangleSize;
        r.bottom = r.top + mRectangleSize;
        r.right = r.left + mRectangleSize;

        canvas.drawRect(r, isWhite ? mPaintWhite : mPaintGray);

        isWhite = !isWhite;
      }

      verticalStartWhite = !verticalStartWhite;

    }

  }

}




Java Source Code List

com.larvalabs.svgandroid.ParserHelper.java
com.larvalabs.svgandroid.SVGParseException.java
com.larvalabs.svgandroid.SVGParser.java
com.larvalabs.svgandroid.SVG.java
com.robobunny.SeekBarPreference.java
com.threefiftynice.android.preference.ListPreferenceMultiSelect.java
fi.testbed2.MainModule.java
fi.testbed2.android.activity.AbstractActivity.java
fi.testbed2.android.activity.AnimationActivity.java
fi.testbed2.android.activity.MainActivity.java
fi.testbed2.android.activity.ParsingActivity.java
fi.testbed2.android.activity.TestbedPreferenceActivity.java
fi.testbed2.android.app.Logger.java
fi.testbed2.android.app.MainApplication.java
fi.testbed2.android.task.AbstractTask.java
fi.testbed2.android.task.DownloadImagesTask.java
fi.testbed2.android.task.ParseAndInitTask.java
fi.testbed2.android.task.Task.java
fi.testbed2.android.task.exception.DownloadTaskException.java
fi.testbed2.android.task.exception.TaskCancelledException.java
fi.testbed2.android.ui.dialog.AlertDialogBuilder.java
fi.testbed2.android.ui.dialog.DialogBuilder.java
fi.testbed2.android.ui.svg.LocationMarkerSVG.java
fi.testbed2.android.ui.svg.MunicipalityMarkerSVG.java
fi.testbed2.android.ui.view.AnimationViewPlayer.java
fi.testbed2.android.ui.view.AnimationView.java
fi.testbed2.android.ui.view.MapScaleInfo.java
fi.testbed2.android.ui.view.util.AnimationViewBoundsUtil.java
fi.testbed2.android.ui.view.util.AnimationViewCanvasUtil.java
fi.testbed2.android.ui.view.util.AnimationViewScaleAndGestureUtil.java
fi.testbed2.domain.MapLocationGPS.java
fi.testbed2.domain.MapLocationXY.java
fi.testbed2.domain.Municipality.java
fi.testbed2.domain.TestbedMapImage.java
fi.testbed2.domain.TestbedParsedPage.java
fi.testbed2.robotium.MainActivityRobotiumTest.java
fi.testbed2.service.BitmapService.java
fi.testbed2.service.CoordinateService.java
fi.testbed2.service.HttpUrlService.java
fi.testbed2.service.LocationService.java
fi.testbed2.service.MunicipalityService.java
fi.testbed2.service.PageService.java
fi.testbed2.service.SettingsService.java
fi.testbed2.service.impl.ApacheHttpUrlService.java
fi.testbed2.service.impl.InlineMunicipalityService.java
fi.testbed2.service.impl.LruCacheBitmapService.java
fi.testbed2.service.impl.LruCachePageService.java
fi.testbed2.service.impl.MercatorCoordinateService.java
fi.testbed2.service.impl.PreferenceBasedLocationService.java
fi.testbed2.service.impl.SharedPreferenceSettingsService.java
fi.testbed2.util.ColorUtil.java
fi.testbed2.util.MathUtil.java
fi.testbed2.util.SeekBarUtil.java
fi.testbed2.util.TimeUtil.java
net.margaritov.preference.colorpicker.AlphaPatternDrawable.java
net.margaritov.preference.colorpicker.ColorPickerDialog.java
net.margaritov.preference.colorpicker.ColorPickerPanelView.java
net.margaritov.preference.colorpicker.ColorPickerPreference.java
net.margaritov.preference.colorpicker.ColorPickerView.java