Android Open Source - android-placeholder Dimensions Drawable






From Project

Back to project page android-placeholder.

License

The source code is released under:

Apache License

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

package se.lightside.placeholder.graphics.drawable;
//  w  w  w  .  j a  v a 2  s. com
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

import se.lightside.placeholder.util.MetricsConverter;

/**
 * A placeholder that prints the dimensions of the canvas it's drawn on as a
 * centered label on the canvas
 */
public class DimensionsDrawable extends AbstractPlaceholderDrawable {

    private MetricsConverter mConv;

    public DimensionsDrawable(MetricsConverter metricsConverter) {
        super();

        mConv = metricsConverter;

        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setColor(Color.BLACK);

        setPaint(p);
    }

    @Override
    public void draw(Canvas canvas) {
        int height = getBounds().height();
        int width = getBounds().width();
        drawLabel(canvas, height, width);
    }

    private void drawLabel(Canvas canvas, int height, int width) {
        Paint p = getPaint();
        String label = mConv.asString(width, height);
        int targetWidth = width / 3;
        float prelimTextWidth = p.measureText(label);
        float ts = p.getTextSize();
        p.setTextSize(ts * (targetWidth / prelimTextWidth));

        Rect bounds = new Rect();
        p.getTextBounds(label, 0, label.length(), bounds);

        canvas.drawText(label, (width - bounds.width())/2, (height - ((p.descent() + p.ascent()) / 2))/ 2, p);

    }

}




Java Source Code List

se.lightside.placeholder.ApplicationTest.java
se.lightside.placeholder.PlaceholderBuilder.java
se.lightside.placeholder.graphics.drawable.AbstractPlaceholderDrawable.java
se.lightside.placeholder.graphics.drawable.CrossDrawable.java
se.lightside.placeholder.graphics.drawable.DimensionsDrawable.java
se.lightside.placeholder.graphics.drawable.GridDrawable.java
se.lightside.placeholder.sample.DemoFragment.java
se.lightside.placeholder.sample.DemoHostActivity.java
se.lightside.placeholder.text.method.BlockTransformationMethod.java
se.lightside.placeholder.util.MetricsConverter.java
se.lightside.placeholder.util.ViewCompat.java
se.lightside.placeholder.view.RatioLineOverlay.java
se.lightside.placeholder.view.RatioLine.java
se.lightside.placeholder.widget.BlockedOutTextView.java
se.lightside.placeholder.widget.PlaceholderView.java