Newer
Older
Cactus-CleanArchitecture / app / src / main / java / org / ntlab / radishforandroidstudio / framework / RWT / RWTView.java
package org.ntlab.radishforandroidstudio.framework.RWT;

import android.content.Context;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * ゲームに使用するViewの基底クラス
 *
 * @author s.iwatani
 */
public abstract class RWTView extends View {
    private float dp = 1;
    private Point size = null;
    private Point pos = null;

    public RWTView(Context context) {
        super(context);
        init();
    }

    public RWTView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RWTView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init() {
        dp = getResources().getDisplayMetrics().density;
    }

    /**
     * 画面上に表示されるサイズを取得
     * @return Point
     */
    public Point getSize() {
        if (size == null) {
            size = new Point(getWidth(), getHeight());
        }
        return size;
    }

    /**
     * 画面上に表示される左上の座標を取得
     * @return Point
     */
    public Point getPosition() {
        if (pos == null) {
            pos = new Point(getLeft(), getTop());
        }
        return pos;
    }

    /**
     * DPを乗算する
     * @param a
     * @return int
     */
    public int applyDp(int a) {
        return (int)(a * dp);
    }
}