Aqlier/ 6月 27, 2016/ Android

Android 開発で、CustomViewの定義

画面表示のXML内に、任意のView(CustomViewで領域を張り付けます。

CustomView_01

XMLの定義、CustomViewで定義します。

        <com.xxxx.yyyy.CustomView
            android:layout_width="160dp"
            android:layout_height="160dp"
            android:id="@+id/N02_view"
            android:layout_gravity="center" />

ソース上の定義は、TextViewと同等です。

        //CustomView
        CustomView customView = (CustomView) findViewById(R.id.N02_view);

        customView.setDrawingCacheEnabled(true);
        Bitmap bmp = customView.getDrawingCache();

CustomView内の定義は、以下の定義”extends View“で作成する。
Viewのデータを扱うとき、bitmapに変換してViewを扱います。

public class CustomView extends View {

    public CustomView(Context context, AttributeSet attrs) {

        super(context, attrs);
        paint = new Paint();