Android 開発でCustomViewの定義

Android 開発で、CustomViewの定義

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

CustomView_01

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

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

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

1//CustomView
2CustomView customView = (CustomView) findViewById(R.id.N02_view);
3 
4customView.setDrawingCacheEnabled(true);
5Bitmap bmp = customView.getDrawingCache();

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

1public class CustomView extends View {
2 
3    public CustomView(Context context, AttributeSet attrs) {
4 
5        super(context, attrs);
6        paint = new Paint();
Verified by MonsterInsights