Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2013 serso aka se.solovyev
 *
 * 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.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * Contact details
 *
 * Email: se.solovyev@gmail.com
 * Site:  http://se.solovyev.org
 */

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

import android.widget.TextView;
import javax.annotation.Nonnull;

public class Main {
    public static boolean drawDrawables(Canvas canvas, @Nonnull TextView textView) {
        final int compoundPaddingLeft = textView.getCompoundPaddingLeft();
        final int compoundPaddingTop = textView.getCompoundPaddingTop();
        final int compoundPaddingRight = textView.getCompoundPaddingRight();
        final int compoundPaddingBottom = textView.getCompoundPaddingBottom();

        final int scrollX = textView.getScrollX();
        final int scrollY = textView.getScrollY();

        final int right = textView.getRight();
        final int left = textView.getLeft();
        final int bottom = textView.getBottom();
        final int top = textView.getTop();

        final Drawable[] drawables = textView.getCompoundDrawables();
        if (drawables != null) {

            int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
            int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;

            Drawable topDr = drawables[1];
            // IMPORTANT: The coordinates computed are also used in invalidateDrawable()
            // Make sure to update invalidateDrawable() when changing this code.
            if (topDr != null) {
                canvas.save();
                canvas.translate(scrollX + compoundPaddingLeft + (hspace - topDr.getBounds().width()) / 2,
                        scrollY + textView.getPaddingTop() + vspace / 2);
                topDr.draw(canvas);
                canvas.restore();
                return true;
            }
        }

        return false;
    }
}