Skip to content

Instantly share code, notes, and snippets.

@lesleh
lesleh / AspectImageView.java
Last active December 26, 2015 02:29
ImageView that maintains a given aspect ratio, for example 6:4 for a standard photograph.
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectImageView extends ImageView {
private static final int DEFAULT_XRATIO = 1;
private static final int DEFAULT_YRATIO = 1;
@lesleh
lesleh / AspectFrameLayout.java
Last active December 26, 2015 02:39
FrameLayout to constrain dimensions to a given aspect ratio.
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;
public class AspectFrameLayout extends FrameLayout {
private static final int DEFAULT_XRATIO = 1;
private static final int DEFAULT_YRATIO = 1;
@lesleh
lesleh / TopCropImageView.java
Last active September 16, 2017 04:15
ImageView that scales like centerCrop, but instead of showing the centre of the image, it shows the top.
import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* ImageView that scales like centerCrop, but instead of showing the centre of the image, it shows the top.
*/
public class TopCropImageView extends ImageView {
public TopCropImageView(Context context) {
@lesleh
lesleh / gist:7268816
Last active December 27, 2015 04:39
Implementation of onMeasure to ensure a square aspect ratio.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width, height;
@lesleh
lesleh / SquareImageView.java
Created November 14, 2013 15:53
Square ImageView implementation for Android.
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SquareImageView extends ImageView {
// Inherited constructors
public SquareImageView(Context context) {
super(context);
@lesleh
lesleh / CustomFontManager.java
Last active December 29, 2015 01:29
Custom font handling for Android, which caches the typefaces. Place the fonts in /assets/fonts/.
import android.content.res.AssetManager;
import android.graphics.Typeface;
import java.util.HashMap;
import java.util.Map;
public class CustomFontManager {
private static Map<String, Typeface> typefaces = new HashMap<String, Typeface>();
@lesleh
lesleh / chunkArray.java
Last active March 10, 2024 20:16
Java function to split an array into chunks of equal size. The last chunk may be smaller than the rest.
// Example usage:
//
// int[] numbers = {1, 2, 3, 4, 5, 6, 7};
// int[][] chunks = chunkArray(numbers, 3);
//
// chunks now contains [
// [1, 2, 3],
// [4, 5, 6],
// [7]
// ]
@lesleh
lesleh / chunk.cs
Last active June 10, 2022 19:40
Java generics are so horribly broken.
// ONE implementation for everything
public static T[][] chunkArray<T>(T[] array, int chunkSize) {
int numOfChunks = (int)Math.Ceiling((double)array.Length / chunkSize);
T[][] output = new T[numOfChunks][];
for(int i = 0; i < numOfChunks; ++i) {
int start = i * chunkSize;
int length = Math.Min(array.Length - start, chunkSize);
T[] temp = new T[length];
@lesleh
lesleh / build.gradle
Last active August 29, 2015 13:57
APK signing configuration for Android Gradle projects
// APK signing
def signingPropFile = new File(project.rootDir, "signing.properties")
if(signingPropFile.exists()) {
Properties props = new Properties()
props.load(new FileInputStream(signingPropFile))
android {
signingConfigs {
release {
storeFile file(props['signing.keystore'])
@lesleh
lesleh / RatioImageView.java
Last active August 29, 2015 14:00
An implementation of ImageView for Android that restricts the image to a given aspect ratio, if possible. This is a more generalised way of doing SquareImageView, just set both the width and height ratios to 1 for a square image.
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ImageView;
public class RatioImageView extends ImageView {
private static final float DEFAULT_WIDTH_RATIO = 1;
private static final float DEFAULT_HEIGHT_RATIO = 1;