PowenKo, Android Tutorial 043-1, custom your UI components on XML Layout


res\layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />

 <com.powenko.Tutorial_UI_Cumtom_ShapeDrawable.CustomDrawableView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

CustomDrawableView.java

package com.powenko.Tutorial_UI_Cumtom_ShapeDrawable;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;

public class CustomDrawableView extends View {
    private ShapeDrawable mDrawable;

    public CustomDrawableView(Context context, AttributeSet attrs) {
        super(context, attrs);
        /*
        this.CustomDrawableView(context);
    }

    public CustomDrawableView(Context context) {
        super(context);
*/
        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff4badeb);
        mDrawable.setBounds(x, y, x + width, y + height);

   		Paint paint = new Paint();

    }

    protected void onDraw(Canvas canvas) {
    	if(mDrawable!=null){
    		mDrawable.draw(canvas);

    		////
    		Paint paint = new Paint();
    		paint.setColor(Color.WHITE);
    		paint.setTextSize(20);
    		canvas.drawText("PowenKo.com", 100, 50, paint);

        }
    }
}

sample code:

Tutorial_UI_Cumtom_ShapeDrawable


Leave a Reply